1 | <?php |
||
15 | final class EntityHydrator implements Hydrator |
||
16 | { |
||
17 | /** @var EntityMetadata */ |
||
18 | private $metadata; |
||
19 | /** @var EntityManager */ |
||
20 | private $manager; |
||
21 | |||
22 | /** |
||
23 | * EntityHydrator constructor. |
||
24 | * |
||
25 | * @param EntityManager $manager |
||
26 | * @param ApiMetadata $metadata |
||
27 | */ |
||
28 | 18 | public function __construct(EntityManager $manager, ApiMetadata $metadata) |
|
33 | |||
34 | /** {@inheritdoc} */ |
||
35 | 18 | public function hydarate($source, $entity = null) |
|
36 | { |
||
37 | 18 | if (null === $entity) { |
|
38 | 18 | $entity = $this->metadata->getReflectionClass()->newInstance(); |
|
39 | 18 | } |
|
40 | |||
41 | 18 | $acessor = new PropertyAccessor(); |
|
42 | 18 | foreach ($this->metadata->getFieldNames() as $fieldName) { |
|
43 | 18 | $property = $this->metadata->getReflectionProperty($fieldName); |
|
44 | |||
45 | 18 | $apiField = $this->metadata->getApiFieldName($fieldName); |
|
46 | |||
47 | try { |
||
48 | 18 | $value = $acessor->getValue($source, $apiField); |
|
49 | 18 | } catch (NoSuchPropertyException $exception) { |
|
50 | 4 | if (!$this->metadata->getFieldMapping($fieldName)['nullable']) { |
|
51 | throw new HydrationException( |
||
52 | sprintf( |
||
53 | 'Field %s for property %s does not present in dehydrated data', |
||
54 | $apiField, |
||
55 | $fieldName |
||
56 | ) |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | 4 | $property->setValue($entity, null); |
|
61 | |||
62 | 4 | continue; |
|
63 | } |
||
64 | |||
65 | $type = |
||
66 | 18 | $this->manager->getConfiguration()->getTypeRegistry()->get($this->metadata->getTypeOfField($fieldName)); |
|
67 | 18 | $value = $type->fromApiValue($value, $this->metadata->getFieldOptions($fieldName)); |
|
68 | |||
69 | 18 | $property->setValue($entity, $value); |
|
70 | 18 | } |
|
71 | |||
72 | 18 | foreach ($this->metadata->getAssociationNames() as $fieldName) { |
|
73 | 12 | $value = $this->hydrateAssociation($fieldName, $entity, $source); |
|
74 | 12 | $property = $this->metadata->getReflectionProperty($fieldName); |
|
75 | 12 | $property->setValue($entity, $value); |
|
76 | 18 | } |
|
77 | |||
78 | 18 | return $entity; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param string $field |
||
83 | * @param \StdClass $source |
||
84 | * @param object $entity |
||
85 | * |
||
86 | * @return array|Proxy|object |
||
87 | * @throws HydrationException |
||
88 | * @throws MappingException |
||
89 | */ |
||
90 | 12 | private function hydrateAssociation($field, $entity, $source) |
|
190 | } |
||
191 |
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.