|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\State; |
| 4 | + |
| 5 | +use ApiPlatform\Metadata\HttpOperation; |
| 6 | +use ApiPlatform\Metadata\Operation; |
| 7 | +use ApiPlatform\State\ApiResource\Error; |
| 8 | +use ApiPlatform\State\ProviderInterface; |
| 9 | +use ApiPlatform\Validator\Exception\ValidationException; |
| 10 | +use App\DTO\ValidationError; |
| 11 | +use App\Serializer\Normalizer\Error\TranslationInfoOfConstraintViolation; |
| 12 | +use App\Service\TranslateToAllLocalesService; |
| 13 | +use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter; |
| 14 | + |
| 15 | +/** |
| 16 | + * @psalm-suppress MissingTemplateParam |
| 17 | + */ |
| 18 | +class ValidationErrorProvider implements ProviderInterface { |
| 19 | + public function __construct( |
| 20 | + private readonly ProviderInterface $decorated, |
| 21 | + private readonly TranslationInfoOfConstraintViolation $translationInfoOfConstraintViolation, |
| 22 | + private readonly TranslateToAllLocalesService $translateToAllLocalesService, |
| 23 | + private readonly MetadataAwareNameConverter $nameConverter |
| 24 | + ) {} |
| 25 | + |
| 26 | + /** |
| 27 | + * @psalm-suppress InvalidReturnStatement |
| 28 | + */ |
| 29 | + public function provide(Operation $operation, array $uriVariables = [], array $context = []): null|array|object { |
| 30 | + $request = $context['request']; |
| 31 | + $exception = $request?->attributes->get('exception'); |
| 32 | + if (!($request ?? null) || !$operation instanceof HttpOperation || null === $exception) { |
| 33 | + throw new \RuntimeException('Not an HTTP request'); |
| 34 | + } |
| 35 | + |
| 36 | + $status = $operation->getStatus() ?? 500; |
| 37 | + $error = Error::createFromException($exception, $status); |
| 38 | + if (!$exception instanceof ValidationException) { |
| 39 | + return $this->decorated->provide($operation, $uriVariables, $context); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ValidationException $exception |
| 44 | + */ |
| 45 | + $violationInfos = []; |
| 46 | + foreach ($exception->getConstraintViolationList() as $violation) { |
| 47 | + $violationInfo = $this->translationInfoOfConstraintViolation->extract($violation); |
| 48 | + $translations = $this->translateToAllLocalesService->translate( |
| 49 | + $violation->getMessageTemplate(), |
| 50 | + array_merge( |
| 51 | + $violation->getPlural() ? ['%count%' => $violation->getPlural()] : [], |
| 52 | + $violation->getParameters() |
| 53 | + ) |
| 54 | + ); |
| 55 | + |
| 56 | + $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath(), $violation->getRoot()::class, 'jsonproblem'); |
| 57 | + |
| 58 | + $violationInfos[] = [ |
| 59 | + 'code' => $violation->getCode(), |
| 60 | + 'propertyPath' => $propertyPath, |
| 61 | + 'message' => $violation->getMessage(), |
| 62 | + 'i18n' => [ |
| 63 | + ...(array) $violationInfo, |
| 64 | + 'translations' => $translations, |
| 65 | + ], |
| 66 | + ]; |
| 67 | + } |
| 68 | + |
| 69 | + return new ValidationError( |
| 70 | + type: $error->getType(), |
| 71 | + title: $error->getTitle(), |
| 72 | + status: $status, |
| 73 | + detail: $error->getDetail(), |
| 74 | + instance: $error->getInstance(), |
| 75 | + violations: $violationInfos |
| 76 | + ); |
| 77 | + } |
| 78 | +} |
0 commit comments