1
|
|
|
<?php |
2
|
|
|
namespace Workana\AsyncJobs\Normalizer; |
3
|
|
|
|
4
|
|
|
use Bernard\Normalizer\AbstractAggregateNormalizerAware; |
5
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
6
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
7
|
|
|
|
8
|
|
|
class ScalarNormalizer extends AbstractAggregateNormalizerAware implements NormalizerInterface, DenormalizerInterface |
9
|
|
|
{ |
10
|
|
|
private $castFunctionMap = [ |
11
|
|
|
'integer' => 'intval', |
12
|
|
|
'string' => 'strval', |
13
|
|
|
'boolean' => 'boolval', |
14
|
|
|
'float' => 'floatval', |
15
|
|
|
'double' => 'doubleval', |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Normalizes an object into a set of arrays/scalars. |
20
|
|
|
* |
21
|
|
|
* @param object $object object to normalize |
|
|
|
|
22
|
|
|
* @param string $format format the normalization result will be encoded as |
23
|
|
|
* @param array $context Context options for the normalizer |
24
|
|
|
* |
25
|
|
|
* @return array|scalar |
26
|
|
|
*/ |
27
|
|
|
public function normalize($value, $format = null, array $context = array()) |
28
|
|
|
{ |
29
|
|
|
return $value; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Checks whether the given class is supported for normalization by this normalizer. |
34
|
|
|
* |
35
|
|
|
* @param mixed $data Data to normalize |
36
|
|
|
* @param string $format The format being (de-)serialized from or into |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public function supportsNormalization($data, $format = null) |
41
|
|
|
{ |
42
|
|
|
return is_null($data) || is_scalar($data); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Denormalizes data back into an object of the given class. |
47
|
|
|
* |
48
|
|
|
* @param mixed $data data to restore |
49
|
|
|
* @param string $class the expected class to instantiate |
|
|
|
|
50
|
|
|
* @param string $format format the given data was extracted from |
51
|
|
|
* @param array $context options available to the denormalizer |
52
|
|
|
* |
53
|
|
|
* @return object |
54
|
|
|
*/ |
55
|
|
|
public function denormalize($data, $type, $format = null, array $context = array()) |
56
|
|
|
{ |
57
|
|
|
if ($type == 'NULL') { |
58
|
|
|
return null; |
59
|
|
|
} else { |
60
|
|
|
$castFunc = $this->castFunctionMap[$type]; |
61
|
|
|
|
62
|
|
|
return $castFunc($data); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks whether the given class is supported for denormalization by this normalizer. |
68
|
|
|
* |
69
|
|
|
* @param mixed $data Data to denormalize from |
70
|
|
|
* @param string $type The class to which the data should be denormalized |
71
|
|
|
* @param string $format The format being deserialized from |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
76
|
|
|
{ |
77
|
|
|
return ($type == 'NULL') || array_key_exists($type, $this->castFunctionMap); |
78
|
|
|
} |
79
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.