1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File was created 11.05.2016 10:14 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PeekAndPoke\Component\Slumber\Core\Codec\Property; |
7
|
|
|
|
8
|
|
|
use PeekAndPoke\Component\Collections\Collection; |
9
|
|
|
use PeekAndPoke\Component\Psi\Psi\IsInstanceOf; |
10
|
|
|
use PeekAndPoke\Component\Slumber\Annotation\Slumber\AsCollection; |
11
|
|
|
use PeekAndPoke\Component\Slumber\Core\Codec\Mapper; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Karsten J. Gerber <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractCollectionMapper extends AbstractPropertyMapper implements CollectionMapper |
18
|
|
|
{ |
19
|
|
|
/** @var Mapper */ |
20
|
|
|
protected $nested; |
21
|
|
|
/** @var AsCollection */ |
22
|
|
|
protected $options; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* AbstractCollectionMapper constructor. |
26
|
|
|
* |
27
|
|
|
* @param AsCollection $options |
28
|
|
|
* @param Mapper $nested |
29
|
|
|
*/ |
30
|
119 |
|
public function __construct(AsCollection $options, Mapper $nested) |
31
|
|
|
{ |
32
|
119 |
|
$this->options = $options; |
33
|
119 |
|
$this->nested = $nested; |
34
|
119 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $subject |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
107 |
|
protected function isIterable($subject) |
42
|
|
|
{ |
43
|
107 |
|
return \is_array($subject) || $subject instanceof \Traversable; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param mixed $result |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
60 |
|
protected function createAwakeResult($result) |
52
|
|
|
{ |
53
|
|
|
// do we need to instantiate a collection class ? |
54
|
60 |
|
$collectionCls = $this->options->collection; |
55
|
|
|
|
56
|
60 |
|
if (null === $collectionCls) { |
57
|
30 |
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
30 |
|
return new $collectionCls($result); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return AsCollection |
65
|
|
|
*/ |
66
|
7 |
|
public function getOptions() |
67
|
|
|
{ |
68
|
7 |
|
return $this->options; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Mapper |
73
|
|
|
*/ |
74
|
15 |
|
public function getNested() |
75
|
|
|
{ |
76
|
15 |
|
return $this->nested; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Mapper $nested |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
3 |
|
public function setNested(Mapper $nested) |
85
|
|
|
{ |
86
|
3 |
|
$this->nested = $nested; |
87
|
|
|
|
88
|
3 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $class |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
6 |
|
public function isLeaveOfType($class) |
97
|
|
|
{ |
98
|
6 |
|
$leave = $this->getLeaf(); |
99
|
|
|
|
100
|
6 |
|
return (new IsInstanceOf($class))->__invoke($leave); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Mapper |
105
|
|
|
*/ |
106
|
8 |
|
public function getLeaf() |
107
|
|
|
{ |
108
|
8 |
|
$current = $this; |
109
|
|
|
|
110
|
8 |
|
while ($current instanceof CollectionMapper) { |
111
|
8 |
|
$current = $current->getNested(); |
112
|
|
|
} |
113
|
|
|
|
114
|
8 |
|
return $current; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Mapper $leave |
119
|
|
|
*/ |
120
|
2 |
|
public function setLeaf(Mapper $leave) |
121
|
|
|
{ |
122
|
2 |
|
$current = $this; |
123
|
|
|
|
124
|
2 |
|
while ($current->getNested() instanceof CollectionMapper) { |
125
|
1 |
|
$current = $current->getNested(); |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
$current->setNested($leave); |
129
|
2 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return null|CollectionMapper |
133
|
|
|
*/ |
134
|
4 |
|
public function getLeafParent() |
135
|
|
|
{ |
136
|
4 |
|
$parent = null; |
137
|
4 |
|
$current = $this; |
138
|
|
|
|
139
|
4 |
|
while ($current instanceof CollectionMapper) { |
140
|
4 |
|
$parent = $current; |
141
|
4 |
|
$current = $current->getNested(); |
142
|
|
|
} |
143
|
|
|
|
144
|
4 |
|
return $parent; |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $collectionClass |
150
|
|
|
* |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
3 |
|
public function setLeafParentsCollectionType($collectionClass) |
154
|
|
|
{ |
155
|
3 |
|
if (! is_a($collectionClass, Collection::class, true)) { |
156
|
1 |
|
throw new \LogicException("The given class $collectionClass does not implement " . Collection::class); |
157
|
|
|
} |
158
|
|
|
|
159
|
2 |
|
$parent = $this->getLeafParent(); |
160
|
|
|
|
161
|
2 |
|
if ($parent !== null) { |
162
|
2 |
|
$parent->getOptions()->setCollection($collectionClass); |
163
|
|
|
} |
164
|
|
|
|
165
|
2 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the nesting level. |
170
|
|
|
* |
171
|
|
|
* If there is no nested element the level is 0 (this can never be the case, as the constructor ensures the nested mapper). |
172
|
|
|
* If there is a nested element the level is 1. |
173
|
|
|
* If there is a nested element with a nested element as well the level 2. |
174
|
|
|
* |
175
|
|
|
* ... and so forth |
176
|
|
|
* |
177
|
|
|
* @return mixed |
178
|
|
|
*/ |
179
|
2 |
|
public function getNestingLevel() |
180
|
|
|
{ |
181
|
2 |
|
$nested = $this->getNested(); |
182
|
|
|
|
183
|
|
|
// another collection ... increase the level by 1 |
184
|
2 |
|
if ($nested instanceof CollectionMapper) { |
185
|
1 |
|
return 1 + $nested->getNestingLevel(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// not another collection ... level is 1 |
189
|
2 |
|
return 1; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|