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; } else { $this->status = true; } $this->message = implode(', ', array_merge($this->messages, $this->errors)); if (isset($this->data) && !empty($this->data)) { $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; } }