1 | <?php |
||
26 | class HydrationPlan |
||
27 | { |
||
28 | protected $session; |
||
29 | protected $projection; |
||
30 | protected $converters = []; |
||
31 | protected $field_types = []; |
||
32 | |||
33 | |||
34 | /** |
||
35 | * Construct |
||
36 | * |
||
37 | * @access public |
||
38 | * @param Projection $projection |
||
39 | * @param Session $session |
||
40 | */ |
||
41 | public function __construct(Projection $projection, Session $session) |
||
48 | |||
49 | /** |
||
50 | * loadConverters |
||
51 | * |
||
52 | * Cache converters needed for this result set. |
||
53 | * |
||
54 | * @access protected |
||
55 | * @return HydrationPlan $this |
||
56 | */ |
||
57 | protected function loadConverters() |
||
79 | |||
80 | |||
81 | /** |
||
82 | * getFieldType |
||
83 | * |
||
84 | * Return the type of the given field. Proxy to Projection::getFieldType(). |
||
85 | * |
||
86 | * @access public |
||
87 | * @param string $name |
||
88 | * @return string |
||
89 | */ |
||
90 | public function getFieldType($name) |
||
94 | |||
95 | /** |
||
96 | * isArray |
||
97 | * |
||
98 | * Tell if the given field is an array or not. |
||
99 | * |
||
100 | * @access public |
||
101 | * @param string $name |
||
102 | * @return bool |
||
103 | */ |
||
104 | public function isArray($name) |
||
108 | |||
109 | |||
110 | /** |
||
111 | * hydrate |
||
112 | * |
||
113 | * Take values fetched from the database, launch conversion system and |
||
114 | * hydrate the FlexibleEntityInterface through the mapper. |
||
115 | * |
||
116 | * @access public |
||
117 | * @param array $values |
||
118 | * @return FlexibleEntityInterface |
||
119 | */ |
||
120 | public function hydrate(array $values) |
||
126 | |||
127 | /** |
||
128 | * dry |
||
129 | * |
||
130 | * Return values converted to Pg. |
||
131 | * |
||
132 | * @access public |
||
133 | * @param array $values |
||
134 | * @return array |
||
135 | */ |
||
136 | public function dry(array $values) |
||
140 | |||
141 | /** |
||
142 | * freeze |
||
143 | * |
||
144 | * Return values converted to Pg standard output. |
||
145 | * |
||
146 | * @access public |
||
147 | * @param array $values |
||
148 | * @return array converted values |
||
149 | */ |
||
150 | public function freeze(array $values) |
||
154 | |||
155 | /** |
||
156 | * convert |
||
157 | * |
||
158 | * Convert values from / to postgres. |
||
159 | * |
||
160 | * @access protected |
||
161 | * @param string $from_to |
||
162 | * @param array $values |
||
163 | * @return array |
||
164 | */ |
||
165 | protected function convert($from_to, array $values) |
||
182 | |||
183 | /** |
||
184 | * createEntity |
||
185 | * |
||
186 | * Instantiate FlexibleEntityInterface from converted values. |
||
187 | * |
||
188 | * @access protected |
||
189 | * @param array $values |
||
190 | * @return FlexibleEntityInterface |
||
191 | */ |
||
192 | protected function createEntity(array $values) |
||
200 | |||
201 | /** |
||
202 | * getConverterForField |
||
203 | * |
||
204 | * Return the converter client associated with a field. |
||
205 | * |
||
206 | * @access public |
||
207 | * @param string $field_name |
||
208 | * @return ConverterClient |
||
209 | */ |
||
210 | public function getConverterForField($field_name) |
||
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: