|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: christian |
|
5
|
|
|
* Date: 1/4/17 |
|
6
|
|
|
* Time: 4:20 AM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Veloci\Core\Helper\Serializer; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
|
12
|
|
|
use Veloci\Core\EntityIndex; |
|
13
|
|
|
use Veloci\Core\Model\IntegerIndex; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class EntityIndexNormalizer |
|
18
|
|
|
* |
|
19
|
|
|
* @package Veloci\Core\Helper |
|
20
|
|
|
*/ |
|
21
|
|
|
class EntityIndexNormalizer extends AbstractNormalizer |
|
22
|
|
|
{ |
|
23
|
|
|
protected $indexType = IntegerIndex::class; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $type |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setIndexType(string $type) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->indexType = $type; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Denormalizes data back into an object of the given class. |
|
35
|
|
|
* |
|
36
|
|
|
* @param mixed $data data to restore |
|
37
|
|
|
* @param string $class the expected class to instantiate |
|
38
|
|
|
* @param string $format format the given data was extracted from |
|
|
|
|
|
|
39
|
|
|
* @param array $context options available to the denormalizer |
|
40
|
|
|
* |
|
41
|
|
|
* @return object |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
|
44
|
|
|
{ |
|
45
|
2 |
|
$type = $this->indexType; |
|
46
|
|
|
|
|
47
|
2 |
|
return new $type($data); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Checks whether the given class is supported for denormalization by this normalizer. |
|
52
|
|
|
* |
|
53
|
|
|
* @param mixed $data Data to denormalize from |
|
54
|
|
|
* @param string $type The class to which the data should be denormalized |
|
55
|
|
|
* @param string $format The format being deserialized from |
|
|
|
|
|
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function supportsDenormalization($data, $type, $format = null) |
|
60
|
|
|
{ |
|
61
|
2 |
|
return is_a($type, EntityIndex::class, true); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Normalizes an object into a set of arrays/scalars. |
|
66
|
|
|
* |
|
67
|
|
|
* @param object $object object to normalize |
|
68
|
|
|
* @param string $format format the normalization result will be encoded as |
|
|
|
|
|
|
69
|
|
|
* @param array $context Context options for the normalizer |
|
70
|
|
|
* |
|
71
|
|
|
* @return array|scalar |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function normalize($object, $format = null, array $context = []) |
|
74
|
|
|
{ |
|
75
|
2 |
|
return (string)$object; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Checks whether the given class is supported for normalization by this normalizer. |
|
80
|
|
|
* |
|
81
|
|
|
* @param mixed $data Data to normalize |
|
82
|
|
|
* @param string $format The format being (de-)serialized from or into |
|
|
|
|
|
|
83
|
|
|
* |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function supportsNormalization($data, $format = null) |
|
87
|
|
|
{ |
|
88
|
2 |
|
return $data instanceof EntityIndex; |
|
89
|
|
|
} |
|
90
|
|
|
} |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.