Newer
Older
<?php
namespace App\Service\Response\Classes;
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
/**
* @var bool
*/
#[Groups(["message", "data"])]
public bool $status = true;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @var string
*/
#[Groups(["message"])]
public string $message = '';
#[Ignore]
public int $statusCode = 200;
#[Ignore]
private array $messages = [];
#[Ignore]
private array $errors = [];
#[Ignore]
private JsonResponse $response;
public function __construct()
{
$this->response = new JsonResponse();
}
/**
* Группы сериализации
*
* @return array
*/
#[Ignore]
protected function getGroups(): array
{
return [];
}
/**
* Добавление ошибки
*
* @param string $message
*
* @return $this
*
* @throws \JsonException
*/
#[Ignore]
public function addError(string $message): self
{
$this->errors[] = $message;
return $this;
}
/**
* Добавление сообщения
*
* @param string $message
*
* @return $this
*
* @throws \JsonException
*/
#[Ignore]
public function addMessage(string $message): self
{
$this->messages[] = $message;
return $this;
}
#[Ignore]
public function isSuccess(): bool
{
if (!empty($this->errors)) {
$this->status = false;
} else {
$this->status = true;
}
return $this->status;
}
#[Ignore]
public function getResponse(): JsonResponse
{
$this->refresh();
return $this->response;
}
#[Ignore]
public function setStatusCode(int $code): self
{
$this->statusCode = $code;
return $this;
}
#[Ignore]
protected function refresh(): self
{
$groups = ['message'];
if (!empty($this->errors)) {
$this->status = false;
if ($this->statusCode === 200) {
$this->statusCode = \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST;
}
} else {
$this->status = true;
}
$this->message = implode(', ', array_merge($this->messages, $this->errors));
$groups = ['data'];
$groups = array_merge($groups, $this->getGroups());
}
$normalizer = new ObjectNormalizer(
new ClassMetadataFactory(new AttributeLoader()),
new CamelCaseToSnakeCaseNameConverter(),
null,
new ReflectionExtractor()
);
$serializer = new Serializer([new DateTimeNormalizer(), $normalizer], [new JsonEncoder()]);
$dataStr = $serializer->serialize($this, 'json', ['groups' => $groups]);
$dataArray = json_decode($dataStr, true, 512, JSON_THROW_ON_ERROR);
$this->response->setData($dataArray);
$this->response->setStatusCode($this->statusCode);
return $this;
}