1 | <?php |
||
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) |
||
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) |
||
155 | |||
156 | /** |
||
157 | * clearFilters |
||
158 | * |
||
159 | * Empty the filter stack. |
||
160 | */ |
||
161 | public function clearFilters() |
||
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() |
||
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) |
||
200 | |||
201 | /** |
||
202 | * @return array |
||
203 | */ |
||
204 | public function map(\Closure $callback) |
||
208 | |||
209 | /** |
||
210 | * convertSlice |
||
211 | * |
||
212 | * Convert a slice. |
||
213 | * |
||
214 | * @access protected |
||
215 | * @param array $values |
||
216 | * @param string $name |
||
217 | * @return array |
||
218 | */ |
||
219 | protected function convertSlice(array $values, $name) |
||
231 | } |
||
232 |
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: