schmittjoh /
serializer
| 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\Type\Type; |
||||
| 10 | use JMS\Serializer\Visitor\SerializationVisitorInterface; |
||||
| 11 | use JMS\Serializer\XmlSerializationVisitor; |
||||
| 12 | use Symfony\Component\Validator\ConstraintViolation; |
||||
| 13 | use Symfony\Component\Validator\ConstraintViolationList; |
||||
| 14 | |||||
| 15 | /** |
||||
| 16 | * @phpstan-import-type TypeArray from Type |
||||
| 17 | 284 | */ |
|||
| 18 | final class ConstraintViolationHandler implements SubscribingHandlerInterface |
||||
| 19 | 284 | { |
|||
| 20 | 284 | /** |
|||
| 21 | 284 | * {@inheritdoc} |
|||
| 22 | */ |
||||
| 23 | 284 | public static function getSubscribingMethods() |
|||
| 24 | 284 | { |
|||
| 25 | 284 | $methods = []; |
|||
| 26 | 284 | $formats = ['xml', 'json']; |
|||
| 27 | 284 | $types = [ConstraintViolationList::class => 'serializeList', ConstraintViolation::class => 'serializeViolation']; |
|||
| 28 | 284 | ||||
| 29 | 284 | foreach ($types as $type => $method) { |
|||
| 30 | foreach ($formats as $format) { |
||||
| 31 | $methods[] = [ |
||||
| 32 | 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
||||
| 33 | 'type' => $type, |
||||
| 34 | 284 | 'format' => $format, |
|||
| 35 | 'method' => $method . 'To' . $format, |
||||
| 36 | ]; |
||||
| 37 | 1 | } |
|||
| 38 | } |
||||
| 39 | 1 | ||||
| 40 | 1 | return $methods; |
|||
| 41 | 1 | } |
|||
| 42 | |||||
| 43 | public function serializeListToXml(XmlSerializationVisitor $visitor, ConstraintViolationList $list): void |
||||
| 44 | 1 | { |
|||
| 45 | 1 | $currentNode = $visitor->getCurrentNode(); |
|||
| 46 | if (!$currentNode) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 47 | 1 | $visitor->createRoot(); |
|||
| 48 | } |
||||
| 49 | 1 | ||||
| 50 | foreach ($list as $violation) { |
||||
| 51 | 1 | $this->serializeViolationToXml($visitor, $violation); |
|||
| 52 | } |
||||
| 53 | } |
||||
| 54 | 2 | ||||
| 55 | /** |
||||
| 56 | 2 | * @param TypeArray $type |
|||
|
0 ignored issues
–
show
The type
JMS\Serializer\Handler\TypeArray was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 57 | * |
||||
| 58 | 2 | * @return array|\ArrayObject |
|||
| 59 | 2 | */ |
|||
| 60 | 1 | 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...
|
|||||
| 61 | { |
||||
| 62 | 1 | return $visitor->visitArray(iterator_to_array($list), $type); |
|||
| 63 | } |
||||
| 64 | |||||
| 65 | 2 | public function serializeViolationToXml(XmlSerializationVisitor $visitor, ConstraintViolation $violation): void |
|||
| 66 | 2 | { |
|||
| 67 | $violationNode = $visitor->getDocument()->createElement('violation'); |
||||
| 68 | 2 | ||||
| 69 | 2 | $parent = $visitor->getCurrentNode(); |
|||
| 70 | if (!$parent) { |
||||
|
0 ignored issues
–
show
|
|||||
| 71 | 2 | $visitor->setCurrentAndRootNode($violationNode); |
|||
| 72 | } else { |
||||
| 73 | $parent->appendChild($violationNode); |
||||
| 74 | 2 | } |
|||
| 75 | 2 | ||||
| 76 | $violationNode->setAttribute('property_path', $violation->getPropertyPath()); |
||||
| 77 | $violationNode->appendChild($messageNode = $visitor->getDocument()->createElement('message')); |
||||
| 78 | 2 | ||||
| 79 | $messageNode->appendChild($visitor->getDocument()->createCDATASection($violation->getMessage())); |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | public function serializeViolationToJson(SerializationVisitorInterface $visitor, ConstraintViolation $violation): 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...
|
|||||
| 83 | { |
||||
| 84 | return [ |
||||
| 85 | 'property_path' => $violation->getPropertyPath(), |
||||
| 86 | 'message' => $violation->getMessage(), |
||||
| 87 | ]; |
||||
| 88 | } |
||||
| 89 | } |
||||
| 90 |