dmt-software /
jms-parameter-bag-handler
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace DMT\Serializer; |
||||
| 4 | |||||
| 5 | use JMS\Serializer\Context; |
||||
| 6 | use JMS\Serializer\Exception\RuntimeException; |
||||
| 7 | use JMS\Serializer\GraphNavigator; |
||||
| 8 | use JMS\Serializer\Handler\SubscribingHandlerInterface; |
||||
| 9 | use JMS\Serializer\JsonDeserializationVisitor; |
||||
| 10 | use JMS\Serializer\JsonSerializationVisitor; |
||||
| 11 | use JMS\Serializer\VisitorInterface; |
||||
| 12 | use phpDocumentor\Reflection\Types\Object_; |
||||
| 13 | |||||
| 14 | /** |
||||
| 15 | * Class ParameterBagHandler |
||||
| 16 | * |
||||
| 17 | * @package DMT\Serializer |
||||
| 18 | */ |
||||
| 19 | class ParameterBagHandler implements SubscribingHandlerInterface |
||||
| 20 | { |
||||
| 21 | 2 | public static function getSubscribingMethods(): array |
|||
| 22 | { |
||||
| 23 | return [ |
||||
| 24 | [ |
||||
| 25 | 2 | 'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
|||
| 26 | 2 | 'format' => 'json', |
|||
| 27 | 2 | 'type' => 'ParameterBag', |
|||
| 28 | 2 | 'method' => 'serializeParameterBag', |
|||
| 29 | ], |
||||
| 30 | [ |
||||
| 31 | 2 | 'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
|||
| 32 | 2 | 'format' => 'json', |
|||
| 33 | 2 | 'type' => 'ParameterBag', |
|||
| 34 | 2 | 'method' => 'deserializeParameterBag', |
|||
| 35 | ], |
||||
| 36 | ]; |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | /** |
||||
| 40 | * @param JsonSerializationVisitor|VisitorInterface $visitor |
||||
| 41 | * @param ParameterBag|null $data |
||||
| 42 | * @param array $type |
||||
| 43 | * @param Context $context |
||||
| 44 | * |
||||
| 45 | * @return array|null |
||||
| 46 | * @throws RuntimeException |
||||
| 47 | */ |
||||
| 48 | 25 | public function serializeParameterBag( |
|||
| 49 | VisitorInterface $visitor, |
||||
|
0 ignored issues
–
show
|
|||||
| 50 | ? ParameterBag $data, |
||||
| 51 | array $type, |
||||
|
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...
|
|||||
| 52 | Context $context |
||||
| 53 | ): ? array { |
||||
| 54 | 25 | $result = []; |
|||
| 55 | 25 | if ($data === null) { |
|||
| 56 | 6 | return $context->shouldSerializeNull() ? $result : null; |
|||
| 57 | } |
||||
| 58 | |||||
| 59 | 19 | foreach ($data as $parameter) { |
|||
| 60 | 13 | if (!$context->shouldSerializeNull() && $parameter->getValue() === null) { |
|||
|
0 ignored issues
–
show
The expression
$context->shouldSerializeNull() of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.
If an expression can have both $a = canBeFalseAndNull();
// Instead of
if ( ! $a) { }
// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
|
|||||
| 61 | 6 | continue; |
|||
| 62 | } |
||||
| 63 | 13 | $result[$parameter->getName()] = $parameter->getValue(); |
|||
| 64 | } |
||||
| 65 | |||||
| 66 | 19 | return count($result) || $context->shouldSerializeNull() ? $result : null; |
|||
| 67 | } |
||||
| 68 | |||||
| 69 | /** |
||||
| 70 | * Deserialize elements into a bag of parameters. |
||||
| 71 | * |
||||
| 72 | * @param JsonDeserializationVisitor|VisitorInterface $visitor |
||||
| 73 | * @param array|null $data |
||||
| 74 | * @param array $type |
||||
| 75 | * @param Context $context |
||||
| 76 | * |
||||
| 77 | * @return ParameterBag |
||||
| 78 | * @throws RuntimeException |
||||
| 79 | */ |
||||
| 80 | 25 | public function deserializeParameterBag( |
|||
| 81 | VisitorInterface $visitor, |
||||
|
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...
|
|||||
| 82 | $data, |
||||
| 83 | array $type, |
||||
| 84 | Context $context |
||||
| 85 | ): ParameterBag { |
||||
| 86 | 25 | $result = new ParameterBag(); |
|||
| 87 | 25 | if (null === $data) { |
|||
| 88 | 6 | return $result; |
|||
| 89 | } |
||||
| 90 | 19 | if (!is_iterable($data)) { |
|||
| 91 | 4 | throw new RuntimeException('ParameterBag only excepts an array of parameters'); |
|||
| 92 | } |
||||
| 93 | |||||
| 94 | /** @var ParameterInterface $parameter */ |
||||
| 95 | 15 | $parameterType = $this->getParameterType($type); |
|||
| 96 | 13 | $parameter = unserialize(sprintf('O:%d:"%s":0:{}', strlen($parameterType), $parameterType)); |
|||
| 97 | |||||
| 98 | 13 | foreach ($data as $key => $value) { |
|||
| 99 | 13 | if (!$context->shouldSerializeNull() && $value === null) { |
|||
|
0 ignored issues
–
show
The expression
$context->shouldSerializeNull() of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.
If an expression can have both $a = canBeFalseAndNull();
// Instead of
if ( ! $a) { }
// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
|
|||||
| 100 | 6 | continue; |
|||
| 101 | } |
||||
| 102 | 13 | $instance = clone $parameter; |
|||
| 103 | 13 | $instance->setName($key); |
|||
| 104 | 13 | $instance->setValue($value); |
|||
| 105 | |||||
| 106 | 13 | $result[] = $instance; |
|||
| 107 | } |
||||
| 108 | |||||
| 109 | 13 | return $result; |
|||
| 110 | } |
||||
| 111 | |||||
| 112 | /** |
||||
| 113 | * Get the class name of the object to store in parameter bag. |
||||
| 114 | * |
||||
| 115 | * @param array $type |
||||
| 116 | * |
||||
| 117 | * @return string The class name for the parameters. |
||||
| 118 | * @throws RuntimeException |
||||
| 119 | */ |
||||
| 120 | 15 | protected function getParameterType(array $type): string |
|||
| 121 | { |
||||
| 122 | 15 | if (empty($type['params'])) { |
|||
| 123 | 5 | return Parameter::class; |
|||
| 124 | } |
||||
| 125 | |||||
| 126 | 10 | $parameterType = $type['params'][0]['name'] ?? $type['params'][0]; |
|||
| 127 | 10 | if (!class_exists($parameterType) || !is_subclass_of($parameterType, ParameterInterface::class)) { |
|||
| 128 | 2 | throw new RuntimeException('Parameter(s) must implement ' . ParameterInterface::class); |
|||
| 129 | } |
||||
| 130 | |||||
| 131 | 8 | return $parameterType; |
|||
| 132 | } |
||||
| 133 | } |
||||
| 134 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.