-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnsubmittedFormNormalizer.php
49 lines (40 loc) · 1.52 KB
/
UnsubmittedFormNormalizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php declare(strict_types=1);
namespace SBSEDV\Bundle\FormBundle\Serializer\Normalizer;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class UnsubmittedFormNormalizer implements NormalizerInterface
{
/** The value will be used as the normalized "message" key (e.g. "msg"). */
public const CONTEXT_MESSAGE_KEY = 'form_error.message_key';
/** The value that will appear under the "type" key. */
public const CONTEXT_ERROR_TYPE = 'form_error.type';
public function __construct(
private readonly TranslatorInterface $translator,
) {
}
/**
* @param FormInterface $object
*/
public function normalize(mixed $object, ?string $format = null, array $context = []): array
{
return [[
$context[self::CONTEXT_MESSAGE_KEY] ?? 'message' => $this->translator->trans('request_body_is_empty', domain: 'sbsedv_form'),
'type' => $context[self::CONTEXT_ERROR_TYPE] ?? 'invalid_request_error',
]];
}
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof FormInterface && !$data->isSubmitted();
}
public function hasCacheableSupportsMethod(): bool
{
return true;
}
public function getSupportedTypes(?string $format): array
{
return [
FormInterface::class => self::class === static::class,
];
}
}