1 | <?php |
||||||
2 | |||||||
3 | declare(strict_types=1); |
||||||
4 | |||||||
5 | namespace JMS\Serializer\Handler; |
||||||
6 | |||||||
7 | use JMS\Serializer\GraphNavigatorInterface; |
||||||
8 | use JMS\Serializer\SerializationContext; |
||||||
9 | use JMS\Serializer\Visitor\SerializationVisitorInterface; |
||||||
10 | use JMS\Serializer\XmlSerializationVisitor; |
||||||
11 | use Symfony\Component\Validator\ConstraintViolation; |
||||||
12 | use Symfony\Component\Validator\ConstraintViolationList; |
||||||
13 | |||||||
14 | final class ConstraintViolationHandler implements SubscribingHandlerInterface |
||||||
15 | { |
||||||
16 | /** |
||||||
17 | 284 | * {@inheritdoc} |
|||||
18 | */ |
||||||
19 | 284 | public static function getSubscribingMethods() |
|||||
20 | 284 | { |
|||||
21 | 284 | $methods = []; |
|||||
22 | $formats = ['xml', 'json']; |
||||||
23 | 284 | $types = [ConstraintViolationList::class => 'serializeList', ConstraintViolation::class => 'serializeViolation']; |
|||||
24 | 284 | ||||||
25 | 284 | foreach ($types as $type => $method) { |
|||||
26 | 284 | foreach ($formats as $format) { |
|||||
27 | 284 | $methods[] = [ |
|||||
28 | 284 | 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|||||
29 | 284 | 'type' => $type, |
|||||
30 | 'format' => $format, |
||||||
31 | 'method' => $method . 'To' . $format, |
||||||
32 | ]; |
||||||
33 | } |
||||||
34 | 284 | } |
|||||
35 | |||||||
36 | return $methods; |
||||||
37 | 1 | } |
|||||
38 | |||||||
39 | 1 | public function serializeListToXml(XmlSerializationVisitor $visitor, ConstraintViolationList $list, array $type): void |
|||||
0 ignored issues
–
show
|
|||||||
40 | 1 | { |
|||||
41 | 1 | $currentNode = $visitor->getCurrentNode(); |
|||||
42 | if (!$currentNode) { |
||||||
0 ignored issues
–
show
|
|||||||
43 | $visitor->createRoot(); |
||||||
44 | 1 | } |
|||||
45 | 1 | ||||||
46 | foreach ($list as $violation) { |
||||||
47 | 1 | $this->serializeViolationToXml($visitor, $violation); |
|||||
48 | } |
||||||
49 | 1 | } |
|||||
50 | |||||||
51 | 1 | /** |
|||||
52 | * @return array|\ArrayObject |
||||||
53 | */ |
||||||
54 | 2 | public function serializeListToJson(SerializationVisitorInterface $visitor, ConstraintViolationList $list, array $type, SerializationContext $context) |
|||||
0 ignored issues
–
show
The parameter
$context is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||||
55 | { |
||||||
56 | 2 | return $visitor->visitArray(iterator_to_array($list), $type); |
|||||
57 | } |
||||||
58 | 2 | ||||||
59 | 2 | public function serializeViolationToXml(XmlSerializationVisitor $visitor, ConstraintViolation $violation, ?array $type = null): void |
|||||
0 ignored issues
–
show
The parameter
$type is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||||
60 | 1 | { |
|||||
61 | $violationNode = $visitor->getDocument()->createElement('violation'); |
||||||
62 | 1 | ||||||
63 | $parent = $visitor->getCurrentNode(); |
||||||
64 | if (!$parent) { |
||||||
0 ignored issues
–
show
|
|||||||
65 | 2 | $visitor->setCurrentAndRootNode($violationNode); |
|||||
66 | 2 | } else { |
|||||
67 | $parent->appendChild($violationNode); |
||||||
68 | 2 | } |
|||||
69 | 2 | ||||||
70 | $violationNode->setAttribute('property_path', $violation->getPropertyPath()); |
||||||
71 | 2 | $violationNode->appendChild($messageNode = $visitor->getDocument()->createElement('message')); |
|||||
72 | |||||||
73 | $messageNode->appendChild($visitor->getDocument()->createCDATASection($violation->getMessage())); |
||||||
74 | 2 | } |
|||||
75 | 2 | ||||||
76 | public function serializeViolationToJson(SerializationVisitorInterface $visitor, ConstraintViolation $violation, ?array $type = null): array |
||||||
0 ignored issues
–
show
The parameter
$visitor is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
The parameter
$type is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||||
77 | { |
||||||
78 | 2 | return [ |
|||||
79 | 'property_path' => $violation->getPropertyPath(), |
||||||
80 | 'message' => $violation->getMessage(), |
||||||
81 | ]; |
||||||
82 | } |
||||||
83 | } |
||||||
84 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.