|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Analogue\ORM\Relationships; |
|
4
|
|
|
|
|
5
|
|
|
class EmbedsOne extends EmbeddedRelationship |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* The relation attribute on the parent object. |
|
9
|
|
|
* |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $relation; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Transform attributes into embedded object(s), and |
|
16
|
|
|
* match it into the given resultset. |
|
17
|
|
|
* |
|
18
|
|
|
* @param array $results |
|
19
|
|
|
* |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
public function match(array $results) : array |
|
23
|
|
|
{ |
|
24
|
|
|
return array_map([$this, 'matchSingleResult'], $results); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Match a single database row's attributes to a single |
|
29
|
|
|
* object, and return the updated attributes. |
|
30
|
|
|
* |
|
31
|
|
|
* @param array $attributes |
|
32
|
|
|
* |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
public function matchSingleResult(array $attributes) : array |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->asArray ? $this->matchAsArray($attributes) : $this->matchAsAttributes($attributes); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Match array attribute from parent to an embedded object, |
|
42
|
|
|
* and return the updated attributes. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $attributes |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
View Code Duplication |
protected function matchAsArray(array $attributes) : array |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
// Extract the attributes with the key of the relation, |
|
51
|
|
|
// which should be an array. |
|
52
|
|
|
$key = $this->relation; |
|
53
|
|
|
|
|
54
|
|
|
if (!array_key_exists($key, $attributes) && !is_array($key)) { |
|
55
|
|
|
throw new MappingException("'$key' column should be an array"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$attributes[$key] = $this->buildEmbeddedObject($attributes[$key]); |
|
59
|
|
|
|
|
60
|
|
|
return $attributes; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Transform attributes from the parent entity result into |
|
65
|
|
|
* an embedded object, and return the updated attributes. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $attributes |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function matchAsAttributes(array $attributes) : array |
|
72
|
|
|
{ |
|
73
|
|
|
$attributesMap = $this->getAttributesDictionnary(); |
|
74
|
|
|
|
|
75
|
|
|
// Get the subset that only the embedded object is concerned with and, convert it back |
|
76
|
|
|
// to embedded object attributes keys |
|
77
|
|
|
$originalAttributes = array_only($attributes, $attributesMap); |
|
78
|
|
|
|
|
79
|
|
|
$embeddedAttributes = []; |
|
80
|
|
|
|
|
81
|
|
|
foreach ($originalAttributes as $key => $value) { |
|
82
|
|
|
$embeddedKey = array_search($key, $attributesMap); |
|
83
|
|
|
$embeddedAttributes[$embeddedKey] = $value; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Unset original attributes before, just in case one of the keys of the |
|
87
|
|
|
// original attributes is equals the relation name. |
|
88
|
|
|
foreach (array_keys($originalAttributes) as $key) { |
|
89
|
|
|
unset($attributes[$key]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Build object |
|
93
|
|
|
$attributes[$this->relation] = $this->buildEmbeddedObject($embeddedAttributes); |
|
94
|
|
|
|
|
95
|
|
|
return $attributes; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return a dictionnary of attributes key on parent Entity. |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getAttributesDictionnary() : array |
|
104
|
|
|
{ |
|
105
|
|
|
// Get attributes that belongs to the embedded object |
|
106
|
|
|
$embeddedAttributeKeys = $this->getEmbeddedObjectAttributes(); |
|
107
|
|
|
|
|
108
|
|
|
$attributesMap = []; |
|
109
|
|
|
|
|
110
|
|
|
// Build a dictionnary for corresponding object attributes => parent attributes |
|
111
|
|
|
foreach ($embeddedAttributeKeys as $key) { |
|
112
|
|
|
$attributesMap[$key] = $this->getParentAttributeKey($key); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $attributesMap; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Transform embedded object into DB column(s). |
|
120
|
|
|
* |
|
121
|
|
|
* @param mixed $object |
|
122
|
|
|
* |
|
123
|
|
|
* @return array $columns |
|
124
|
|
|
*/ |
|
125
|
|
|
public function normalize($object) : array |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->asArray ? $this->normalizeAsArray($object) : $this->normalizeAsAttributes($object); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Normalize object an array containing raw attributes |
|
132
|
|
|
* |
|
133
|
|
|
* @param mixed $object |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function normalizeAsArray($object) : array |
|
137
|
|
|
{ |
|
138
|
|
|
$wrapper = $this->factory->make($object); |
|
139
|
|
|
|
|
140
|
|
|
return [$this->relation => $wrapper->getEntityAttributes()]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Normalize object as parent's attributes |
|
145
|
|
|
* |
|
146
|
|
|
* @param mixed $object |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function normalizeAsAttributes($object) : array |
|
150
|
|
|
{ |
|
151
|
|
|
if (is_null($object)) { |
|
152
|
|
|
return $this->nullObjectAttributes(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$attributesMap = $this->getAttributesDictionnary(); |
|
156
|
|
|
|
|
157
|
|
|
$wrapper = $this->factory->make($object); |
|
158
|
|
|
|
|
159
|
|
|
$normalizedAttributes = []; |
|
160
|
|
|
|
|
161
|
|
|
foreach ($attributesMap as $embedKey => $parentKey) { |
|
162
|
|
|
$normalizedAttributes[$parentKey] = $wrapper->getEntityAttribute($embedKey); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $normalizedAttributes; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Set all object attributes to null. |
|
170
|
|
|
* |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function nullObjectAttributes() : array |
|
174
|
|
|
{ |
|
175
|
|
|
$attributesMap = $this->getAttributesDictionnary(); |
|
176
|
|
|
|
|
177
|
|
|
$normalizedAttributes = []; |
|
178
|
|
|
|
|
179
|
|
|
foreach ($attributesMap as $embedKey => $parentKey) { |
|
180
|
|
|
$normalizedAttributes[$parentKey] = null; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $normalizedAttributes; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.