1 | <?php |
||
14 | class EntityHydrator |
||
15 | { |
||
16 | /** @var EntityMetadata */ |
||
17 | private $metadata; |
||
18 | /** @var EntityManager */ |
||
19 | private $manager; |
||
20 | |||
21 | /** |
||
22 | * EntityHydrator constructor. |
||
23 | * |
||
24 | * @param EntityManager $manager |
||
25 | * @param ApiMetadata $metadata |
||
26 | */ |
||
27 | 12 | public function __construct(EntityManager $manager, ApiMetadata $metadata) |
|
32 | |||
33 | |||
34 | /** |
||
35 | * @param \StdClass $source |
||
36 | * @param object|null $entity |
||
37 | * |
||
38 | * @return object Hydrated object |
||
39 | * @throws HydrationException |
||
40 | */ |
||
41 | 12 | public function hydarate($source, $entity = null) |
|
42 | { |
||
43 | 12 | if (null === $entity) { |
|
44 | 12 | $entity = $this->metadata->getReflectionClass()->newInstance(); |
|
45 | 12 | } |
|
46 | |||
47 | 12 | $acessor = new PropertyAccessor(); |
|
48 | 12 | foreach ($this->metadata->getFieldNames() as $fieldName) { |
|
49 | 12 | $property = $this->metadata->getReflectionProperty($fieldName); |
|
50 | |||
51 | 12 | $apiField = $this->metadata->getApiFieldName($fieldName); |
|
52 | |||
53 | try { |
||
54 | 12 | $value = $acessor->getValue($source, $apiField); |
|
55 | 12 | } catch (NoSuchPropertyException $exception) { |
|
56 | 4 | if (!$this->metadata->getFieldMapping($fieldName)['nullable']) { |
|
57 | throw new HydrationException( |
||
58 | sprintf('Api field %s for property %s does not present in response', $apiField, $fieldName) |
||
59 | ); |
||
60 | |||
61 | } |
||
62 | |||
63 | 4 | $property->setValue($entity, null); |
|
64 | |||
65 | 4 | continue; |
|
66 | } |
||
67 | |||
68 | $type = |
||
69 | 12 | $this->manager->getConfiguration()->getTypeRegistry()->get($this->metadata->getTypeOfField($fieldName)); |
|
70 | 12 | $value = $type->fromApiValue($value); |
|
71 | |||
72 | 12 | $property->setValue($entity, $value); |
|
73 | 12 | } |
|
74 | |||
75 | 12 | foreach ($this->metadata->getAssociationNames() as $fieldName) { |
|
76 | 9 | $value = $this->hydrateAssociation($fieldName, $entity, $source); |
|
77 | 9 | $property = $this->metadata->getReflectionProperty($fieldName); |
|
78 | 9 | $property->setValue($entity, $value); |
|
79 | 12 | } |
|
80 | |||
81 | 12 | return $entity; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param string $field |
||
86 | * @param \StdClass $source |
||
87 | * @param object $entity |
||
88 | * |
||
89 | * @return array|Proxy|object |
||
90 | * @throws HydrationException |
||
91 | * @throws MappingException |
||
92 | */ |
||
93 | 9 | private function hydrateAssociation($field, $entity, $source) |
|
141 | } |
||
142 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.