|
1
|
|
|
<?php namespace Pz\Doctrine\Rest\Traits; |
|
2
|
|
|
|
|
3
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
4
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
5
|
|
|
use Pz\Doctrine\Rest\Exceptions\RestException; |
|
6
|
|
|
use Pz\Doctrine\Rest\RestRepository; |
|
7
|
|
|
|
|
8
|
|
|
trait CanHydrate |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @return RestRepository |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract public function repository(); |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param string|object $entity |
|
17
|
|
|
* @param array $data |
|
18
|
|
|
* @param string $scope |
|
19
|
|
|
* |
|
20
|
|
|
* @return object |
|
21
|
|
|
* @throws RestException |
|
22
|
|
|
*/ |
|
23
|
3 |
|
protected function hydrateData($entity, array $data, $scope = 'root') |
|
24
|
|
|
{ |
|
25
|
3 |
|
$entity = is_object($entity) ? $entity : new $entity; |
|
26
|
|
|
|
|
27
|
3 |
|
if (!isset($data['attributes']) || !is_array($data['attributes'])) { |
|
28
|
|
|
throw RestException::missingAttributes($scope); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
$entity = $this->hydrateAttributes($entity, $data['attributes'], $scope); |
|
32
|
|
|
|
|
33
|
3 |
|
if (isset($data['relationships']) && is_array($data['relationships'])) { |
|
34
|
1 |
|
$entity = $this->hydrateRelationships($entity, $data['relationships'], $scope); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
return $entity; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param $entity |
|
42
|
|
|
* @param array $attributes |
|
43
|
|
|
* @param string $scope |
|
44
|
|
|
* |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
* @throws RestException |
|
47
|
|
|
*/ |
|
48
|
3 |
|
protected function hydrateAttributes($entity, array $attributes, $scope = 'root') |
|
49
|
|
|
{ |
|
50
|
3 |
|
$metadata = $this->repository()->getClassMetadata(); |
|
51
|
3 |
|
foreach ($attributes as $name => $value) { |
|
52
|
3 |
|
if (!isset($metadata->columnNames[$name])) { |
|
53
|
|
|
throw RestException::unknownAttribute(sprintf('%s.attribute.%s', $scope, $name)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
$metadata->reflFields[$name]->setValue($entity, $value); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
return $entity; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $entity |
|
64
|
|
|
* @param array $relationships |
|
65
|
|
|
* @param $scope |
|
66
|
|
|
* |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
* @throws RestException |
|
69
|
|
|
*/ |
|
70
|
1 |
|
protected function hydrateRelationships($entity, array $relationships, $scope) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$metadata = $this->repository()->getClassMetadata(); |
|
73
|
|
|
|
|
74
|
1 |
|
foreach ($relationships as $name => $data) { |
|
75
|
1 |
|
$relationScope = sprintf('%s.relation.%s', $scope, $name); |
|
76
|
|
|
|
|
77
|
1 |
|
if (!isset($metadata->associationMappings[$name])) { |
|
78
|
|
|
throw RestException::unknownRelation($relationScope); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$mapping = $metadata->associationMappings[$name]; |
|
82
|
1 |
|
$mappingClass = $mapping['targetEntity']; |
|
83
|
|
|
|
|
84
|
1 |
|
if (!isset($data['data'])) { |
|
85
|
|
|
throw RestException::missingData($scope); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
if (in_array($mapping['type'], [ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE])) { |
|
89
|
1 |
|
$metadata->reflFields[$name]->setValue($entity, |
|
90
|
1 |
|
$this->hydrateRelationData($mappingClass, $data['data'], $relationScope) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
if (in_array($mapping['type'], [ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY])) { |
|
95
|
|
|
if (!is_array($data['data'])) { |
|
96
|
|
|
throw RestException::missingData($scope); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$metadata->reflFields[$name]->setValue($entity, |
|
100
|
|
|
new ArrayCollection(array_map( |
|
101
|
|
|
function($data, $index) use ($mappingClass, $scope) { |
|
102
|
|
|
return $this->hydrateRelationData($mappingClass, $data, sprintf('%s.%s', $scope, $index)); |
|
103
|
|
|
}, |
|
104
|
1 |
|
$data['data'] |
|
105
|
|
|
)) |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
return $entity; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $class |
|
115
|
|
|
* @param $data |
|
116
|
|
|
* @param string $scope |
|
117
|
|
|
* |
|
118
|
|
|
* @return object |
|
119
|
|
|
* @throws RestException |
|
120
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
121
|
|
|
*/ |
|
122
|
2 |
|
protected function hydrateRelationData($class, $data, $scope = 'root') |
|
123
|
|
|
{ |
|
124
|
2 |
|
if (is_scalar($data)) { |
|
125
|
|
|
return $this->repository()->getEntityManager()->getReference($class, $data); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
2 |
|
if (!is_array($data)) { |
|
129
|
|
|
throw RestException::missingData($scope); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
if (isset($data['id']) && isset($data['type'])) { |
|
133
|
1 |
|
return $this->repository()->getEntityManager()->getReference($class, $data['id']); |
|
134
|
|
|
} else { |
|
135
|
1 |
|
return $this->hydrateData($class, $data, $scope); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|