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($relationScope); |
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($relationScope); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$metadata->reflFields[$name]->setValue($entity, |
100
|
|
|
new ArrayCollection(array_map( |
101
|
|
|
function($data, $index) use ($mappingClass, $relationScope) { |
102
|
|
|
return $this->hydrateRelationData( |
103
|
|
|
$mappingClass, $data, sprintf('%s.%s', $relationScope, $index) |
104
|
|
|
); |
105
|
|
|
}, |
106
|
1 |
|
$data['data'] |
107
|
|
|
)) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return $entity; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $class |
117
|
|
|
* @param $data |
118
|
|
|
* @param string $scope |
119
|
|
|
* |
120
|
|
|
* @return object |
121
|
|
|
* @throws RestException |
122
|
|
|
* @throws \Doctrine\ORM\ORMException |
123
|
|
|
*/ |
124
|
2 |
|
protected function hydrateRelationData($class, $data, $scope = 'root') |
125
|
|
|
{ |
126
|
2 |
|
if (is_scalar($data)) { |
127
|
|
|
return $this->repository()->getEntityManager()->getReference($class, $data); |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
if (!is_array($data)) { |
131
|
|
|
throw RestException::missingData($scope); |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
if (isset($data['id']) && isset($data['type'])) { |
135
|
1 |
|
return $this->repository()->getEntityManager()->getReference($class, $data['id']); |
136
|
|
|
} else { |
137
|
1 |
|
return $this->hydrateData($class, $data, $scope); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|