|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the PommProject's ModelManager package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2014 - 2015 Grégoire HUBERT <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace PommProject\ModelManager\Model; |
|
11
|
|
|
|
|
12
|
|
|
use PommProject\Foundation\ResultIterator; |
|
13
|
|
|
use PommProject\Foundation\Session\ResultHandler; |
|
14
|
|
|
use PommProject\Foundation\Session\Session; |
|
15
|
|
|
use PommProject\ModelManager\Converter\PgEntity; |
|
16
|
|
|
use PommProject\ModelManager\Exception\ModelException; |
|
17
|
|
|
use PommProject\ModelManager\Model\FlexibleEntity\FlexibleEntityInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* CollectionIterator |
|
21
|
|
|
* |
|
22
|
|
|
* Iterator for query results. |
|
23
|
|
|
* |
|
24
|
|
|
* @package ModelManager |
|
25
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
|
26
|
|
|
* @author Grégoire HUBERT <[email protected]> |
|
27
|
|
|
* @license MIT/X11 {@link http://opensource.org/licenses/mit-license.php} |
|
28
|
|
|
*/ |
|
29
|
|
|
class CollectionIterator extends ResultIterator |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var Session |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $session; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Projection |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $projection; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $filters = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var HydrationPlan |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $hydration_plan; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var PgEntity |
|
53
|
|
|
*/ |
|
54
|
|
|
private $entity_converter; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* __construct |
|
58
|
|
|
* |
|
59
|
|
|
* Constructor |
|
60
|
|
|
* |
|
61
|
|
|
* @access public |
|
62
|
|
|
* @param ResultHandler $result |
|
63
|
|
|
* @param Session $session |
|
64
|
|
|
* @param Projection $projection |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct(ResultHandler $result, Session $session, Projection $projection) |
|
67
|
|
|
{ |
|
68
|
|
|
parent::__construct($result); |
|
69
|
|
|
$this->projection = $projection; |
|
70
|
|
|
$this->session = $session; |
|
71
|
|
|
$this->hydration_plan = new HydrationPlan($projection, $session); |
|
72
|
|
|
$this->entity_converter = $this |
|
|
|
|
|
|
73
|
|
|
->session |
|
74
|
|
|
->getClientUsingPooler('converter', $this->projection->getFlexibleEntityClass()) |
|
75
|
|
|
->getConverter() |
|
76
|
|
|
; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* get |
|
81
|
|
|
* |
|
82
|
|
|
* @see ResultIterator |
|
83
|
|
|
* @return FlexibleEntityInterface |
|
84
|
|
|
*/ |
|
85
|
|
|
public function get($index) |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->parseRow(parent::get($index)); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* parseRow |
|
92
|
|
|
* |
|
93
|
|
|
* Convert values from Pg. |
|
94
|
|
|
* |
|
95
|
|
|
* @access protected |
|
96
|
|
|
* @param array $values |
|
97
|
|
|
* @return FlexibleEntityInterface |
|
98
|
|
|
* @see ResultIterator |
|
99
|
|
|
*/ |
|
100
|
|
|
public function parseRow(array $values) |
|
101
|
|
|
{ |
|
102
|
|
|
$values = $this->launchFilters($values); |
|
103
|
|
|
$entity = $this->hydration_plan->hydrate($values); |
|
104
|
|
|
|
|
105
|
|
|
return $this->entity_converter->cacheEntity($entity); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* launchFilters |
|
110
|
|
|
* |
|
111
|
|
|
* Launch filters on the given values. |
|
112
|
|
|
* |
|
113
|
|
|
* @access protected |
|
114
|
|
|
* @param array $values |
|
115
|
|
|
* @throws ModelException if return is not an array. |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function launchFilters(array $values) |
|
119
|
|
|
{ |
|
120
|
|
|
foreach ($this->filters as $filter) { |
|
121
|
|
|
$values = call_user_func($filter, $values); |
|
122
|
|
|
|
|
123
|
|
|
if (!is_array($values)) { |
|
124
|
|
|
throw new ModelException(sprintf("Filter error. Filters MUST return an array of values.")); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $values; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* registerFilter |
|
133
|
|
|
* |
|
134
|
|
|
* Register a new callable filter. All filters MUST return an associative |
|
135
|
|
|
* array with field name as key. |
|
136
|
|
|
* |
|
137
|
|
|
* @access public |
|
138
|
|
|
* @param callable $callable the filter. |
|
139
|
|
|
* @return CollectionIterator $this |
|
140
|
|
|
* @throws ModelException |
|
141
|
|
|
*/ |
|
142
|
|
|
public function registerFilter($callable) |
|
143
|
|
|
{ |
|
144
|
|
|
if (!is_callable($callable)) { |
|
145
|
|
|
throw new ModelException(sprintf( |
|
146
|
|
|
"Given filter is not a callable (type '%s').", |
|
147
|
|
|
gettype($callable) |
|
148
|
|
|
)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$this->filters[] = $callable; |
|
152
|
|
|
|
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* clearFilters |
|
158
|
|
|
* |
|
159
|
|
|
* Empty the filter stack. |
|
160
|
|
|
*/ |
|
161
|
|
|
public function clearFilters() |
|
162
|
|
|
{ |
|
163
|
|
|
$this->filters = []; |
|
164
|
|
|
|
|
165
|
|
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* extract |
|
170
|
|
|
* |
|
171
|
|
|
* Return an array of entities extracted as arrays. |
|
172
|
|
|
* |
|
173
|
|
|
* @access public |
|
174
|
|
|
* @return array |
|
175
|
|
|
*/ |
|
176
|
|
|
public function extract() |
|
177
|
|
|
{ |
|
178
|
|
|
$results = []; |
|
179
|
|
|
|
|
180
|
|
|
foreach ($this as $result) { |
|
181
|
|
|
$results[] = $result->extract(); |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $results; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* slice |
|
189
|
|
|
* |
|
190
|
|
|
* see @ResultIterator |
|
191
|
|
|
* |
|
192
|
|
|
* @access public |
|
193
|
|
|
* @param string $name |
|
194
|
|
|
* @return array |
|
195
|
|
|
*/ |
|
196
|
|
|
public function slice($name) |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->convertSlice(parent::slice($name), $name); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* convertSlice |
|
204
|
|
|
* |
|
205
|
|
|
* Convert a slice. |
|
206
|
|
|
* |
|
207
|
|
|
* @access protected |
|
208
|
|
|
* @param array $values |
|
209
|
|
|
* @param string $name |
|
210
|
|
|
* @return array |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function convertSlice(array $values, $name) |
|
213
|
|
|
{ |
|
214
|
|
|
$type = $this->projection->getFieldType($name); |
|
215
|
|
|
$converter = $this->hydration_plan->getConverterForField($name); |
|
216
|
|
|
|
|
217
|
|
|
return array_map( |
|
218
|
|
|
function ($val) use ($converter, $type) { |
|
219
|
|
|
return $converter->fromPg($val, $type, $this->session); |
|
220
|
|
|
}, |
|
221
|
|
|
$values |
|
222
|
|
|
); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: