HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //home/timetracker.panomity.com/src/API/Serializer/ValidationFailedExceptionErrorHandler.php
<?php

/*
 * This file is part of the Kimai time-tracking app.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\API\Serializer;

use App\Entity\User;
use App\Validator\ValidationFailedException;
use FOS\RestBundle\Serializer\Normalizer\FlattenExceptionHandler;
use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigatorInterface;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\JsonSerializationVisitor;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class ValidationFailedExceptionErrorHandler implements SubscribingHandlerInterface
{
    public function __construct(private TranslatorInterface $translator, private FlattenExceptionHandler $exceptionHandler, private Security $security)
    {
    }

    public static function getSubscribingMethods(): array
    {
        return [[
            'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
            'type' => FlattenException::class,
            'format' => 'json',
            'method' => 'serializeExceptionToJson',
            'priority' => -1
        ], [
            'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
            'type' => ValidationFailedException::class,
            'format' => 'json',
            'method' => 'serializeValidationExceptionToJson',
            'priority' => -1
        ]];
    }

    public function serializeExceptionToJson(JsonSerializationVisitor $visitor, FlattenException $exception, array $type, Context $context)
    {
        if ($exception->getClass() !== ValidationFailedException::class) {
            return $this->exceptionHandler->serializeToJson($visitor, $exception, $type, $context);
        }

        $original = $context->getAttribute('exception');
        if ($original instanceof ValidationFailedException) {
            return $this->serializeValidationExceptionToJson($visitor, $original, $type, $context);
        }

        return $this->exceptionHandler->serializeToJson($visitor, $exception, $type, $context);
    }

    public function serializeValidationExceptionToJson(JsonSerializationVisitor $visitor, ValidationFailedException $exception, array $type, Context $context)
    {
        $errors = [];

        /** @var ConstraintViolationInterface $error */
        foreach (iterator_to_array($exception->getViolations()) as $error) {
            $errors[$error->getPropertyPath()]['errors'][] = $this->getErrorMessage($error);
        }

        return [
            'code' => '400',
            'message' => $this->translator->trans($exception->getMessage(), [], 'validators'),
            'errors' => [
                'children' => $errors
            ],
        ];
    }

    private function getErrorMessage(ConstraintViolationInterface $error): string
    {
        $locale = \Locale::getDefault();
        /** @var User $user */
        $user = $this->security->getUser();

        if ($user !== null) {
            $locale = $user->getLocale();
        }

        if (null !== $error->getPlural()) {
            return $this->translator->trans($error->getMessageTemplate(), ['%count%' => $error->getPlural()] + $error->getParameters(), 'validators', $locale);
        }

        return $this->translator->trans($error->getMessageTemplate(), $error->getParameters(), 'validators', $locale);
    }
}