|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Analogue\ORM\Relationships; |
|
4
|
|
|
|
|
5
|
|
|
use Analogue\ORM\Exceptions\MappingException; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
|
|
8
|
|
|
class EmbedsMany extends EmbedsOne |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Match a single database row's attributes to a single |
|
12
|
|
|
* object, and return the updated attributes. |
|
13
|
|
|
* |
|
14
|
|
|
* @param array $attributes |
|
15
|
|
|
* |
|
16
|
|
|
* @return array |
|
17
|
|
|
*/ |
|
18
|
|
|
public function matchSingleResult(array $attributes) : array |
|
19
|
|
|
{ |
|
20
|
|
|
$column = $this->relation; |
|
21
|
|
|
|
|
22
|
|
|
if (!$this->asArray) { |
|
23
|
|
|
throw new MappingException("column '$column' should be of type array or json"); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return $this->matchAsArray($attributes); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Match array attribute from parent to an embedded object, |
|
31
|
|
|
* and return the updated attributes. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $attributes |
|
34
|
|
|
* |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
View Code Duplication |
protected function matchAsArray(array $attributes) : array |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
// Extract the attributes with the key of the relation, |
|
40
|
|
|
// which should be an array. |
|
41
|
|
|
$key = $this->relation; |
|
42
|
|
|
|
|
43
|
|
|
if (!array_key_exists($key, $attributes) && !is_array($key)) { |
|
44
|
|
|
throw new MappingException("'$key' column should be an array"); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$attributes[$key] = $this->buildEmbeddedCollection($attributes[$key]); |
|
48
|
|
|
|
|
49
|
|
|
return $attributes; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Build an embedded collection and returns it. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $rows |
|
56
|
|
|
* |
|
57
|
|
|
* @return Collection |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function buildEmbeddedCollection($rows) : Collection |
|
60
|
|
|
{ |
|
61
|
|
|
$items = []; |
|
62
|
|
|
|
|
63
|
|
|
foreach ($rows as $attributes) { |
|
64
|
|
|
$items[] = $this->buildEmbeddedObject($attributes); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return collect($items); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Transform embedded object into db column(s). |
|
72
|
|
|
* |
|
73
|
|
|
* @param mixed $object |
|
|
|
|
|
|
74
|
|
|
* |
|
75
|
|
|
* @return array $columns |
|
76
|
|
|
*/ |
|
77
|
|
|
public function normalize($objects) : array |
|
78
|
|
|
{ |
|
79
|
|
|
if(! $this->asArray) { |
|
80
|
|
|
throw new MappingException("Cannot normalize an embedsMany relation as row columns"); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->normalizeAsArray($objects); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Normalize object an array containing raw attributes |
|
88
|
|
|
* |
|
89
|
|
|
* @param mixed $object |
|
|
|
|
|
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function normalizeAsArray($objects) : array |
|
93
|
|
|
{ |
|
94
|
|
|
$key = $this->relation; |
|
95
|
|
|
|
|
96
|
|
|
if(! is_array($objects) && ! $objects instanceof Collection) { |
|
97
|
|
|
throw new MappingException("column '$key' should be of type array or collection"); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if($objects instanceof Collection ) { |
|
101
|
|
|
$objects = $objects->all(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$normalizedObjects = []; |
|
105
|
|
|
|
|
106
|
|
|
foreach($objects as $object) { |
|
107
|
|
|
$wrapper = $this->factory->make($object); |
|
108
|
|
|
$normalizedObjects[] = $wrapper->getEntityAttributes(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
return [$key => $normalizedObjects]; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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.