1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Serializer bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\SerializerBundle\Type; |
13
|
|
|
|
14
|
|
|
use Ivory\Serializer\Context\ContextInterface; |
15
|
|
|
use Ivory\Serializer\Direction; |
16
|
|
|
use Ivory\Serializer\Mapping\TypeMetadataInterface; |
17
|
|
|
use Ivory\Serializer\Type\TypeInterface; |
18
|
|
|
use Symfony\Component\Form\FormError; |
19
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author GeLo <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class FormErrorType implements TypeInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var TranslatorInterface|null |
28
|
|
|
*/ |
29
|
|
|
private $translator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param TranslatorInterface|null $translator |
33
|
|
|
*/ |
34
|
351 |
|
public function __construct(TranslatorInterface $translator = null) |
35
|
|
|
{ |
36
|
351 |
|
$this->translator = $translator; |
37
|
351 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
288 |
|
public function convert($data, TypeMetadataInterface $type, ContextInterface $context) |
43
|
|
|
{ |
44
|
288 |
|
if ($context->getDirection() === Direction::DESERIALIZATION) { |
45
|
36 |
|
throw new \RuntimeException(sprintf('Deserializing a "%s" is not supported.', FormError::class)); |
46
|
|
|
} |
47
|
|
|
|
48
|
252 |
|
return $context->getVisitor()->visitString($this->translateError($data), $type, $context); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param FormError $error |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
252 |
|
private function translateError(FormError $error) |
57
|
|
|
{ |
58
|
252 |
|
if ($this->translator === null) { |
59
|
108 |
|
return $error->getMessage(); |
60
|
|
|
} |
61
|
|
|
|
62
|
144 |
|
if ($error->getMessagePluralization() !== null) { |
63
|
36 |
|
return $this->translator->transChoice( |
64
|
36 |
|
$error->getMessageTemplate(), |
65
|
36 |
|
$error->getMessagePluralization(), |
66
|
36 |
|
$error->getMessageParameters(), |
67
|
8 |
|
'validators' |
68
|
28 |
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
108 |
|
return $this->translator->trans( |
72
|
108 |
|
$error->getMessageTemplate(), |
73
|
108 |
|
$error->getMessageParameters(), |
74
|
24 |
|
'validators' |
75
|
84 |
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|