1 | <?php |
||
9 | final class AssociationNormalizer |
||
10 | { |
||
11 | /** @var ManagerRegistry */ |
||
12 | private $registry; |
||
13 | |||
14 | /** |
||
15 | * AssociationAccessor constructor. |
||
16 | * |
||
17 | * @param ManagerRegistry $registry |
||
18 | */ |
||
19 | 13 | public function __construct(ManagerRegistry $registry) |
|
20 | { |
||
21 | 13 | $this->registry = $registry; |
|
22 | 13 | } |
|
23 | |||
24 | 8 | public function normalize($rawValue, $object, $attribute) |
|
25 | { |
||
26 | 8 | $normalizer = new EntityToIdNormalizer($this->registry); |
|
27 | |||
28 | 8 | if (null === $rawValue) { |
|
29 | 6 | return null; |
|
30 | } |
||
31 | |||
32 | 8 | $metadata = $this->getObjectMetadata($object, $attribute); |
|
33 | 8 | if (null === $metadata) { |
|
34 | 8 | return $rawValue; |
|
35 | } |
||
36 | |||
37 | 8 | if ($metadata->isSingleValuedAssociation($attribute)) { |
|
38 | 4 | return $normalizer->normalize($rawValue); |
|
39 | } |
||
40 | |||
41 | 8 | if ($rawValue instanceof \Traversable) { |
|
42 | 8 | $rawValue = iterator_to_array($rawValue); |
|
43 | } |
||
44 | |||
45 | 8 | return array_map([$normalizer, 'normalize'], $rawValue); |
|
46 | } |
||
47 | |||
48 | public function denormalize($object, $attribute, $value) |
||
72 | |||
73 | /**] |
||
74 | * @param $object |
||
75 | * @param $attribute |
||
76 | * |
||
77 | * @return ClassMetadata|null |
||
78 | */ |
||
79 | 8 | private function getObjectMetadata($object, $attribute) |
|
95 | } |
||
96 |