1 | <?php |
||
9 | abstract class BaseBitrixModel extends ArrayableModel |
||
10 | { |
||
11 | use ModelEventsTrait; |
||
12 | |||
13 | /** |
||
14 | * Array of model fields keys that needs to be saved with next save(). |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $fieldsSelectedForSave = []; |
||
19 | |||
20 | /** |
||
21 | * Array of errors that are passed to model events. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $eventErrors = []; |
||
26 | |||
27 | /** |
||
28 | * Have fields been already fetched from DB? |
||
29 | * |
||
30 | * @var bool |
||
31 | */ |
||
32 | protected $fieldsAreFetched = false; |
||
33 | |||
34 | /** |
||
35 | * Internal part of create to avoid problems with static and inheritance |
||
36 | * |
||
37 | * @param $fields |
||
38 | * |
||
39 | * @throws LogicException |
||
40 | * |
||
41 | * @return static|bool |
||
42 | */ |
||
43 | protected static function internalCreate($fields) |
||
47 | |||
48 | /** |
||
49 | * Save model to database. |
||
50 | * |
||
51 | * @param array $selectedFields save only these fields instead of all. |
||
52 | * |
||
53 | * @return bool |
||
54 | */ |
||
55 | abstract public function save($selectedFields = []); |
||
56 | |||
57 | /** |
||
58 | * Determine whether the field should be stopped from passing to "update". |
||
59 | * |
||
60 | * @param string $field |
||
61 | * @param mixed $value |
||
62 | * @param array $selectedFields |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | abstract protected function fieldShouldNotBeSaved($field, $value, $selectedFields); |
||
67 | |||
68 | /** |
||
69 | * Get all model attributes from cache or database. |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | public function get() |
||
79 | |||
80 | /** |
||
81 | * Load model fields from database if they are not loaded yet. |
||
82 | * |
||
83 | * @return $this |
||
84 | */ |
||
85 | public function load() |
||
93 | |||
94 | /** |
||
95 | * Get model fields from cache or database. |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | public function getFields() |
||
107 | |||
108 | /** |
||
109 | * Refresh model from database and place data to $this->fields. |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | public function refresh() |
||
117 | |||
118 | /** |
||
119 | * Refresh model fields and save them to a class field. |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | public function refreshFields() |
||
135 | |||
136 | /** |
||
137 | * Fill model fields if they are already known. |
||
138 | * Saves DB queries. |
||
139 | * |
||
140 | * @param array $fields |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | public function fill($fields) |
||
162 | |||
163 | /** |
||
164 | * Set current model id. |
||
165 | * |
||
166 | * @param $id |
||
167 | */ |
||
168 | protected function setId($id) |
||
173 | |||
174 | /** |
||
175 | * Create new item in database. |
||
176 | * |
||
177 | * @param $fields |
||
178 | * |
||
179 | * @throws LogicException |
||
180 | * |
||
181 | * @return static|bool |
||
182 | */ |
||
183 | public static function create($fields) |
||
187 | |||
188 | /** |
||
189 | * Get count of items that match $filter. |
||
190 | * |
||
191 | * @param array $filter |
||
192 | * |
||
193 | * @return int |
||
194 | */ |
||
195 | public static function count(array $filter = []) |
||
199 | |||
200 | /** |
||
201 | * Get item by its id. |
||
202 | * |
||
203 | * @param int $id |
||
204 | * |
||
205 | * @return static|bool |
||
206 | */ |
||
207 | public static function find($id) |
||
211 | |||
212 | /** |
||
213 | * Update model. |
||
214 | * |
||
215 | * @param array $fields |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function update(array $fields = []) |
||
229 | |||
230 | /** |
||
231 | * Create an array of fields that will be saved to database. |
||
232 | * |
||
233 | * @param $selectedFields |
||
234 | * |
||
235 | * @return array |
||
236 | */ |
||
237 | protected function normalizeFieldsForSave($selectedFields) |
||
252 | |||
253 | /** |
||
254 | * Instantiate a query object for the model. |
||
255 | * |
||
256 | * @throws LogicException |
||
257 | * |
||
258 | * @return BaseQuery |
||
259 | */ |
||
260 | public static function query() |
||
264 | |||
265 | /** |
||
266 | * Handle dynamic static method calls into a new query. |
||
267 | * |
||
268 | * @param string $method |
||
269 | * @param array $parameters |
||
270 | * @return mixed |
||
271 | */ |
||
272 | public static function __callStatic($method, $parameters) |
||
276 | |||
277 | /** |
||
278 | * Reset event errors back to default. |
||
279 | */ |
||
280 | protected function resetEventErrors() |
||
284 | } |
||
285 |
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 sub-classes 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 parent class: