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 | public function __construct(ManagerRegistry $registry) |
||
20 | { |
||
21 | $this->registry = $registry; |
||
22 | } |
||
23 | |||
24 | public function normalize($rawValue, $object, $attribute) |
||
25 | { |
||
26 | $normalizer = new EntityToIdNormalizer($this->registry); |
||
27 | |||
28 | if (null === $rawValue) { |
||
29 | return null; |
||
30 | } |
||
31 | |||
32 | $metadata = $this->getObjectMetadata($object, $attribute); |
||
33 | if (null === $metadata) { |
||
34 | return $rawValue; |
||
35 | } |
||
36 | |||
37 | if ($metadata->isSingleValuedAssociation($attribute)) { |
||
38 | return $normalizer->normalize($rawValue); |
||
39 | } |
||
40 | |||
41 | if ($rawValue instanceof \Traversable) { |
||
42 | $rawValue = iterator_to_array($rawValue); |
||
43 | } |
||
44 | |||
45 | 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 | private function getObjectMetadata($object, $attribute) |
||
95 | } |
||
96 |