Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Model, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | abstract class Model implements \ArrayAccess |
||
29 | { |
||
30 | const TYPE_STRING = 'string'; |
||
31 | const TYPE_INTEGER = 'integer'; |
||
32 | const TYPE_FLOAT = 'float'; |
||
33 | const TYPE_BOOLEAN = 'boolean'; |
||
34 | const TYPE_DATE = 'date'; |
||
35 | const TYPE_OBJECT = 'object'; |
||
36 | const TYPE_ARRAY = 'array'; |
||
37 | |||
38 | const DEFAULT_ID_PROPERTY = 'id'; |
||
39 | |||
40 | const DEFAULT_DATE_FORMAT = 'U'; // unix timestamps |
||
41 | |||
42 | ///////////////////////////// |
||
43 | // Model visible variables |
||
44 | ///////////////////////////// |
||
45 | |||
46 | /** |
||
47 | * List of model ID property names. |
||
48 | * |
||
49 | * @staticvar array |
||
50 | */ |
||
51 | protected static $ids = [self::DEFAULT_ID_PROPERTY]; |
||
52 | |||
53 | /** |
||
54 | * Validation rules expressed as a key-value map with |
||
55 | * property names as the keys. |
||
56 | * i.e. ['name' => 'string:2']. |
||
57 | * |
||
58 | * @staticvar array |
||
59 | */ |
||
60 | protected static $validations = []; |
||
61 | |||
62 | /** |
||
63 | * @staticvar array |
||
64 | */ |
||
65 | protected static $relationships = []; |
||
66 | |||
67 | /** |
||
68 | * @staticvar array |
||
69 | */ |
||
70 | protected static $dates = []; |
||
71 | |||
72 | /** |
||
73 | * @staticvar array |
||
74 | */ |
||
75 | protected static $dispatchers; |
||
76 | |||
77 | /** |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $_values = []; |
||
81 | |||
82 | /** |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $_unsaved = []; |
||
86 | |||
87 | /** |
||
88 | * @var bool |
||
89 | */ |
||
90 | protected $_persisted = false; |
||
91 | |||
92 | /** |
||
93 | * @var Errors |
||
94 | */ |
||
95 | protected $_errors; |
||
96 | |||
97 | ///////////////////////////// |
||
98 | // Base model variables |
||
99 | ///////////////////////////// |
||
100 | |||
101 | /** |
||
102 | * @staticvar array |
||
103 | */ |
||
104 | private static $initialized = []; |
||
105 | |||
106 | /** |
||
107 | * @staticvar DriverInterface |
||
108 | */ |
||
109 | private static $driver; |
||
110 | |||
111 | /** |
||
112 | * @staticvar Locale |
||
113 | */ |
||
114 | private static $locale; |
||
115 | |||
116 | /** |
||
117 | * @staticvar array |
||
118 | */ |
||
119 | private static $accessors = []; |
||
120 | |||
121 | /** |
||
122 | * @staticvar array |
||
123 | */ |
||
124 | private static $mutators = []; |
||
125 | |||
126 | /** |
||
127 | * @var bool |
||
128 | */ |
||
129 | private $_ignoreUnsaved; |
||
130 | |||
131 | /** |
||
132 | * Creates a new model object. |
||
133 | * |
||
134 | * @param array $values values to fill model with |
||
135 | */ |
||
136 | public function __construct(array $values = []) |
||
137 | { |
||
138 | foreach ($values as $k => $v) { |
||
139 | $this->setValue($k, $v, false); |
||
140 | } |
||
141 | |||
142 | // ensure the initialize function is called only once |
||
143 | $k = get_called_class(); |
||
144 | if (!isset(self::$initialized[$k])) { |
||
145 | $this->initialize(); |
||
146 | self::$initialized[$k] = true; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * The initialize() method is called once per model. It's used |
||
152 | * to perform any one-off tasks before the model gets |
||
153 | * constructed. This is a great place to add any model |
||
154 | * properties. When extending this method be sure to call |
||
155 | * parent::initialize() as some important stuff happens here. |
||
156 | * If extending this method to add properties then you should |
||
157 | * call parent::initialize() after adding any properties. |
||
158 | */ |
||
159 | protected function initialize() |
||
160 | { |
||
161 | // add in the default ID property |
||
162 | if (static::$ids == [self::DEFAULT_ID_PROPERTY]) { |
||
163 | if (property_exists($this, 'casts') && !isset(static::$casts[self::DEFAULT_ID_PROPERTY])) { |
||
164 | static::$casts[self::DEFAULT_ID_PROPERTY] = self::TYPE_INTEGER; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | // generates created_at and updated_at timestamps |
||
169 | if (property_exists($this, 'autoTimestamps')) { |
||
170 | $this->installAutoTimestamps(); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | private function installAutoTimestamps() |
||
175 | { |
||
176 | if (property_exists($this, 'casts')) { |
||
177 | static::$casts['created_at'] = self::TYPE_DATE; |
||
178 | static::$casts['updated_at'] = self::TYPE_DATE; |
||
179 | } |
||
180 | |||
181 | self::creating(function (ModelEvent $event) { |
||
182 | $model = $event->getModel(); |
||
183 | $model->created_at = Carbon::now(); |
||
|
|||
184 | $model->updated_at = Carbon::now(); |
||
185 | }); |
||
186 | |||
187 | self::updating(function (ModelEvent $event) { |
||
188 | $event->getModel()->updated_at = Carbon::now(); |
||
189 | }); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Sets the driver for all models. |
||
194 | * |
||
195 | * @param DriverInterface $driver |
||
196 | */ |
||
197 | public static function setDriver(DriverInterface $driver) |
||
198 | { |
||
199 | self::$driver = $driver; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Gets the driver for all models. |
||
204 | * |
||
205 | * @return DriverInterface |
||
206 | * |
||
207 | * @throws DriverMissingException |
||
208 | */ |
||
209 | public static function getDriver() |
||
210 | { |
||
211 | if (!self::$driver) { |
||
212 | throw new DriverMissingException('A model driver has not been set yet.'); |
||
213 | } |
||
214 | |||
215 | return self::$driver; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Clears the driver for all models. |
||
220 | */ |
||
221 | public static function clearDriver() |
||
222 | { |
||
223 | self::$driver = null; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Sets the locale instance for all models. |
||
228 | * |
||
229 | * @param Locale $locale |
||
230 | */ |
||
231 | public static function setLocale(Locale $locale) |
||
232 | { |
||
233 | self::$locale = $locale; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Gets the locale instance for all models. |
||
238 | * |
||
239 | * @return Locale |
||
240 | */ |
||
241 | public static function getLocale() |
||
242 | { |
||
243 | return self::$locale; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Clears the locale for all models. |
||
248 | */ |
||
249 | public static function clearLocale() |
||
250 | { |
||
251 | self::$locale = null; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Gets the name of the model without namespacing. |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | public static function modelName() |
||
260 | { |
||
261 | return explode('\\', get_called_class())[0]; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Gets the model ID. |
||
266 | * |
||
267 | * @return string|number|null ID |
||
268 | */ |
||
269 | public function id() |
||
270 | { |
||
271 | $ids = $this->ids(); |
||
272 | |||
273 | // if a single ID then return it |
||
274 | if (count($ids) === 1) { |
||
275 | return reset($ids); |
||
276 | } |
||
277 | |||
278 | // if multiple IDs then return a comma-separated list |
||
279 | return implode(',', $ids); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Gets a key-value map of the model ID. |
||
284 | * |
||
285 | * @return array ID map |
||
286 | */ |
||
287 | public function ids() |
||
288 | { |
||
289 | return $this->get(static::$ids); |
||
290 | } |
||
291 | |||
292 | ///////////////////////////// |
||
293 | // Magic Methods |
||
294 | ///////////////////////////// |
||
295 | |||
296 | public function __toString() |
||
297 | { |
||
298 | return get_called_class().'('.$this->id().')'; |
||
299 | } |
||
300 | |||
301 | public function __get($name) |
||
302 | { |
||
303 | return array_values($this->get([$name]))[0]; |
||
304 | } |
||
305 | |||
306 | public function __set($name, $value) |
||
307 | { |
||
308 | $this->setValue($name, $value); |
||
309 | } |
||
310 | |||
311 | public function __isset($name) |
||
312 | { |
||
313 | return array_key_exists($name, $this->_unsaved) || $this->hasProperty($name); |
||
314 | } |
||
315 | |||
316 | public function __unset($name) |
||
317 | { |
||
318 | if (static::isRelationship($name)) { |
||
319 | throw new BadMethodCallException("Cannot unset the `$name` property because it is a relationship"); |
||
320 | } |
||
321 | |||
322 | if (array_key_exists($name, $this->_unsaved)) { |
||
323 | unset($this->_unsaved[$name]); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | public static function __callStatic($name, $parameters) |
||
328 | { |
||
329 | // Any calls to unkown static methods should be deferred to |
||
330 | // the query. This allows calls like User::where() |
||
331 | // to replace User::query()->where(). |
||
332 | return call_user_func_array([static::query(), $name], $parameters); |
||
333 | } |
||
334 | |||
335 | ///////////////////////////// |
||
336 | // ArrayAccess Interface |
||
337 | ///////////////////////////// |
||
338 | |||
339 | public function offsetExists($offset) |
||
340 | { |
||
341 | return isset($this->$offset); |
||
342 | } |
||
343 | |||
344 | public function offsetGet($offset) |
||
345 | { |
||
346 | return $this->$offset; |
||
347 | } |
||
348 | |||
349 | public function offsetSet($offset, $value) |
||
350 | { |
||
351 | $this->$offset = $value; |
||
352 | } |
||
353 | |||
354 | public function offsetUnset($offset) |
||
355 | { |
||
356 | unset($this->$offset); |
||
357 | } |
||
358 | |||
359 | ///////////////////////////// |
||
360 | // Property Definitions |
||
361 | ///////////////////////////// |
||
362 | |||
363 | /** |
||
364 | * Gets the names of the model ID properties. |
||
365 | * |
||
366 | * @return array |
||
367 | */ |
||
368 | public static function getIdProperties() |
||
369 | { |
||
370 | return static::$ids; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Builds an existing model instance given a single ID value or |
||
375 | * ordered array of ID values. |
||
376 | * |
||
377 | * @param mixed $id |
||
378 | * |
||
379 | * @return Model |
||
380 | */ |
||
381 | public static function buildFromId($id) |
||
382 | { |
||
383 | $ids = []; |
||
384 | $id = (array) $id; |
||
385 | foreach (static::$ids as $j => $k) { |
||
386 | $ids[$k] = $id[$j]; |
||
387 | } |
||
388 | |||
389 | $model = new static($ids); |
||
390 | |||
391 | return $model; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Gets the mutator method name for a given proeprty name. |
||
396 | * Looks for methods in the form of `setPropertyValue`. |
||
397 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
398 | * |
||
399 | * @param string $property |
||
400 | * |
||
401 | * @return string|false method name if it exists |
||
402 | */ |
||
403 | View Code Duplication | public static function getMutator($property) |
|
404 | { |
||
405 | $class = get_called_class(); |
||
406 | |||
407 | $k = $class.':'.$property; |
||
408 | if (!array_key_exists($k, self::$mutators)) { |
||
409 | $inflector = Inflector::get(); |
||
410 | $method = 'set'.$inflector->camelize($property).'Value'; |
||
411 | |||
412 | if (!method_exists($class, $method)) { |
||
413 | $method = false; |
||
414 | } |
||
415 | |||
416 | self::$mutators[$k] = $method; |
||
417 | } |
||
418 | |||
419 | return self::$mutators[$k]; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Gets the accessor method name for a given proeprty name. |
||
424 | * Looks for methods in the form of `getPropertyValue`. |
||
425 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
426 | * |
||
427 | * @param string $property |
||
428 | * |
||
429 | * @return string|false method name if it exists |
||
430 | */ |
||
431 | View Code Duplication | public static function getAccessor($property) |
|
432 | { |
||
433 | $class = get_called_class(); |
||
434 | |||
435 | $k = $class.':'.$property; |
||
436 | if (!array_key_exists($k, self::$accessors)) { |
||
437 | $inflector = Inflector::get(); |
||
438 | $method = 'get'.$inflector->camelize($property).'Value'; |
||
439 | |||
440 | if (!method_exists($class, $method)) { |
||
441 | $method = false; |
||
442 | } |
||
443 | |||
444 | self::$accessors[$k] = $method; |
||
445 | } |
||
446 | |||
447 | return self::$accessors[$k]; |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * Checks if a given property is a relationship. |
||
452 | * |
||
453 | * @param string $property |
||
454 | * |
||
455 | * @return bool |
||
456 | */ |
||
457 | public static function isRelationship($property) |
||
458 | { |
||
459 | return in_array($property, static::$relationships); |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * Gets the string date format for a property. Defaults to |
||
464 | * UNIX timestamps. |
||
465 | * |
||
466 | * @param string $property |
||
467 | * |
||
468 | * @return string |
||
469 | */ |
||
470 | public static function getDateFormat($property) |
||
471 | { |
||
472 | if (isset(static::$dates[$property])) { |
||
473 | return static::$dates[$property]; |
||
474 | } |
||
475 | |||
476 | return self::DEFAULT_DATE_FORMAT; |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Gets the title of a property. |
||
481 | * |
||
482 | * @param string $name |
||
483 | * |
||
484 | * @return string |
||
485 | */ |
||
486 | public static function getPropertyTitle($name) |
||
487 | { |
||
488 | // attmept to fetch the title from the Locale service |
||
489 | $k = 'pulsar.properties.'.static::modelName().'.'.$name; |
||
490 | if (self::$locale && $title = self::$locale->t($k)) { |
||
491 | if ($title != $k) { |
||
492 | return $title; |
||
493 | } |
||
494 | } |
||
495 | |||
496 | return Inflector::get()->humanize($name); |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Gets the type cast for a property. |
||
501 | * |
||
502 | * @param string $property |
||
503 | * |
||
504 | * @return string|null |
||
505 | */ |
||
506 | public static function getPropertyType($property) |
||
507 | { |
||
508 | if (property_exists(get_called_class(), 'casts')) { |
||
509 | return array_value(static::$casts, $property); |
||
510 | } |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Casts a value to a given type. |
||
515 | * |
||
516 | * @param string|null $type |
||
517 | * @param mixed $value |
||
518 | * @param string $property optional property name |
||
519 | * |
||
520 | * @return mixed casted value |
||
521 | */ |
||
522 | public static function cast($type, $value, $property = null) |
||
523 | { |
||
524 | if ($value === null) { |
||
525 | return; |
||
526 | } |
||
527 | |||
528 | switch ($type) { |
||
529 | case self::TYPE_STRING: |
||
530 | return (string) $value; |
||
531 | |||
532 | case self::TYPE_INTEGER: |
||
533 | return (int) $value; |
||
534 | |||
535 | case self::TYPE_FLOAT: |
||
536 | return (float) $value; |
||
537 | |||
538 | case self::TYPE_BOOLEAN: |
||
539 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
||
540 | |||
541 | case self::TYPE_DATE: |
||
542 | // cast dates into Carbon objects |
||
543 | if ($value instanceof Carbon) { |
||
544 | return $value; |
||
545 | } else { |
||
546 | $format = self::getDateFormat($property); |
||
547 | |||
548 | return Carbon::createFromFormat($format, $value); |
||
549 | } |
||
550 | |||
551 | View Code Duplication | case self::TYPE_ARRAY: |
|
552 | // decode JSON into an array |
||
553 | if (is_string($value)) { |
||
554 | return json_decode($value, true); |
||
555 | } else { |
||
556 | return (array) $value; |
||
557 | } |
||
558 | |||
559 | View Code Duplication | case self::TYPE_OBJECT: |
|
560 | // decode JSON into an object |
||
561 | if (is_string($value)) { |
||
562 | return (object) json_decode($value); |
||
563 | } else { |
||
564 | return (object) $value; |
||
565 | } |
||
566 | |||
567 | default: |
||
568 | return $value; |
||
569 | } |
||
570 | } |
||
571 | |||
572 | /** |
||
573 | * Gets the properties of this model. |
||
574 | * |
||
575 | * @return array |
||
576 | */ |
||
577 | public function getProperties() |
||
578 | { |
||
579 | return array_unique(array_merge( |
||
580 | static::$ids, array_keys($this->_values))); |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Checks if the model has a property. |
||
585 | * |
||
586 | * @param string $property |
||
587 | * |
||
588 | * @return bool has property |
||
589 | */ |
||
590 | public function hasProperty($property) |
||
591 | { |
||
592 | return array_key_exists($property, $this->_values) || |
||
593 | in_array($property, static::$ids); |
||
594 | } |
||
595 | |||
596 | ///////////////////////////// |
||
597 | // Values |
||
598 | ///////////////////////////// |
||
599 | |||
600 | /** |
||
601 | * Sets an unsaved value. |
||
602 | * |
||
603 | * @param string $name |
||
604 | * @param mixed $value |
||
605 | * @param bool $unsaved when true, sets an unsaved value |
||
606 | * |
||
607 | * @throws BadMethodCallException when setting a relationship |
||
608 | * |
||
609 | * @return self |
||
610 | */ |
||
611 | public function setValue($name, $value, $unsaved = true) |
||
612 | { |
||
613 | if (static::isRelationship($name)) { |
||
614 | throw new BadMethodCallException("Cannot set the `$name` property because it is a relationship"); |
||
615 | } |
||
616 | |||
617 | // cast the value |
||
618 | if ($type = static::getPropertyType($name)) { |
||
619 | $value = static::cast($type, $value, $name); |
||
620 | } |
||
621 | |||
622 | // apply any mutators |
||
623 | if ($mutator = self::getMutator($name)) { |
||
624 | $value = $this->$mutator($value); |
||
625 | } |
||
626 | |||
627 | // save the value on the model property |
||
628 | if ($unsaved) { |
||
629 | $this->_unsaved[$name] = $value; |
||
630 | } else { |
||
631 | $this->_values[$name] = $value; |
||
632 | } |
||
633 | |||
634 | return $this; |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * Sets a collection values on the model from an untrusted |
||
639 | * input. Also known as mass assignment. |
||
640 | * |
||
641 | * @param array $values |
||
642 | * |
||
643 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
644 | * |
||
645 | * @return self |
||
646 | */ |
||
647 | public function setValues($values) |
||
667 | |||
668 | /** |
||
669 | * Ignores unsaved values when fetching the next value. |
||
670 | * |
||
671 | * @return self |
||
672 | */ |
||
673 | public function ignoreUnsaved() |
||
679 | |||
680 | /** |
||
681 | * Gets property values from the model. |
||
682 | * |
||
683 | * This method looks up values from these locations in this |
||
684 | * precedence order (least important to most important): |
||
685 | * 1. local values |
||
686 | * 2. unsaved values |
||
687 | * |
||
688 | * @param array $properties list of property names to fetch values of |
||
689 | * |
||
690 | * @return array |
||
691 | * |
||
692 | * @throws InvalidArgumentException when a property was requested not present in the values |
||
693 | */ |
||
694 | public function get(array $properties) |
||
737 | |||
738 | /** |
||
739 | * Converts the model to an array. |
||
740 | * |
||
741 | * @return array model array |
||
742 | */ |
||
743 | public function toArray() |
||
744 | { |
||
745 | // build the list of properties to retrieve |
||
746 | $properties = $this->getProperties(); |
||
747 | |||
748 | // remove any hidden properties |
||
749 | if (property_exists($this, 'hidden')) { |
||
750 | $properties = array_diff($properties, static::$hidden); |
||
751 | } |
||
752 | |||
753 | // include any appended properties |
||
754 | if (property_exists($this, 'appended')) { |
||
755 | $properties = array_unique(array_merge($properties, static::$appended)); |
||
756 | } |
||
757 | |||
758 | // get the values for the properties |
||
759 | $result = $this->get($properties); |
||
760 | |||
761 | foreach ($result as $k => &$value) { |
||
762 | // convert any models to arrays |
||
763 | if ($value instanceof self) { |
||
764 | $value = $value->toArray(); |
||
765 | // convert any Carbon objects to date strings |
||
766 | } elseif ($value instanceof Carbon) { |
||
767 | $format = self::getDateFormat($k); |
||
768 | $value = $value->format($format); |
||
769 | } |
||
770 | } |
||
771 | |||
774 | |||
775 | ///////////////////////////// |
||
776 | // Persistence |
||
777 | ///////////////////////////// |
||
778 | |||
779 | /** |
||
780 | * Saves the model. |
||
781 | * |
||
782 | * @return bool |
||
783 | */ |
||
784 | public function save() |
||
792 | |||
793 | /** |
||
794 | * Creates a new model. |
||
795 | * |
||
796 | * @param array $data optional key-value properties to set |
||
797 | * |
||
798 | * @return bool |
||
799 | * |
||
800 | * @throws BadMethodCallException when called on an existing model |
||
801 | */ |
||
802 | public function create(array $data = []) |
||
841 | |||
842 | /** |
||
843 | * Gets the IDs for a newly created model. |
||
844 | * |
||
845 | * @return string |
||
846 | */ |
||
847 | protected function getNewIds() |
||
862 | |||
863 | /** |
||
864 | * Updates the model. |
||
865 | * |
||
866 | * @param array $data optional key-value properties to set |
||
867 | * |
||
868 | * @return bool |
||
869 | * |
||
870 | * @throws BadMethodCallException when not called on an existing model |
||
871 | */ |
||
872 | public function set(array $data = []) |
||
910 | |||
911 | /** |
||
912 | * Delete the model. |
||
913 | * |
||
914 | * @return bool success |
||
915 | */ |
||
916 | public function delete() |
||
942 | |||
943 | /** |
||
944 | * Tells if the model has been persisted. |
||
945 | * |
||
946 | * @return bool |
||
947 | */ |
||
948 | public function persisted() |
||
952 | |||
953 | /** |
||
954 | * Loads the model from the data layer. |
||
955 | * |
||
956 | * @return self |
||
957 | * |
||
958 | * @throws NotFoundException |
||
959 | */ |
||
960 | public function refresh() |
||
977 | |||
978 | /** |
||
979 | * Loads values into the model retrieved from the data layer. |
||
980 | * |
||
981 | * @param array $values values |
||
982 | * |
||
983 | * @return self |
||
984 | */ |
||
985 | public function refreshWith(array $values) |
||
1002 | |||
1003 | ///////////////////////////// |
||
1004 | // Queries |
||
1005 | ///////////////////////////// |
||
1006 | |||
1007 | /** |
||
1008 | * Generates a new query instance. |
||
1009 | * |
||
1010 | * @return Query |
||
1011 | */ |
||
1012 | public static function query() |
||
1021 | |||
1022 | /** |
||
1023 | * Finds a single instance of a model given it's ID. |
||
1024 | * |
||
1025 | * @param mixed $id |
||
1026 | * |
||
1027 | * @return Model|null |
||
1028 | */ |
||
1029 | public static function find($id) |
||
1035 | |||
1036 | /** |
||
1037 | * Finds a single instance of a model given it's ID or throws an exception. |
||
1038 | * |
||
1039 | * @param mixed $id |
||
1040 | * |
||
1041 | * @return Model|false |
||
1042 | * |
||
1043 | * @throws NotFoundException when a model could not be found |
||
1044 | */ |
||
1045 | public static function findOrFail($id) |
||
1054 | |||
1055 | /** |
||
1056 | * Gets the toal number of records matching an optional criteria. |
||
1057 | * |
||
1058 | * @param array $where criteria |
||
1059 | * |
||
1060 | * @return int total |
||
1061 | */ |
||
1062 | public static function totalRecords(array $where = []) |
||
1069 | |||
1070 | ///////////////////////////// |
||
1071 | // Relationships |
||
1072 | ///////////////////////////// |
||
1073 | |||
1074 | /** |
||
1075 | * Creates the parent side of a One-To-One relationship. |
||
1076 | * |
||
1077 | * @param string $model foreign model class |
||
1078 | * @param string $foreignKey identifying key on foreign model |
||
1079 | * @param string $localKey identifying key on local model |
||
1080 | * |
||
1081 | * @return \Pulsar\Relation\Relation |
||
1082 | */ |
||
1083 | View Code Duplication | public function hasOne($model, $foreignKey = '', $localKey = '') |
|
1098 | |||
1099 | /** |
||
1100 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
1101 | * |
||
1102 | * @param string $model foreign model class |
||
1103 | * @param string $foreignKey identifying key on foreign model |
||
1104 | * @param string $localKey identifying key on local model |
||
1105 | * |
||
1106 | * @return \Pulsar\Relation\Relation |
||
1107 | */ |
||
1108 | View Code Duplication | public function belongsTo($model, $foreignKey = '', $localKey = '') |
|
1123 | |||
1124 | /** |
||
1125 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
1126 | * |
||
1127 | * @param string $model foreign model class |
||
1128 | * @param string $foreignKey identifying key on foreign model |
||
1129 | * @param string $localKey identifying key on local model |
||
1130 | * |
||
1131 | * @return \Pulsar\Relation\Relation |
||
1132 | */ |
||
1133 | View Code Duplication | public function hasMany($model, $foreignKey = '', $localKey = '') |
|
1148 | |||
1149 | /** |
||
1150 | * Creates the child side of a Many-To-Many relationship. |
||
1151 | * |
||
1152 | * @param string $model foreign model class |
||
1153 | * @param string $foreignKey identifying key on foreign model |
||
1154 | * @param string $localKey identifying key on local model |
||
1155 | * |
||
1156 | * @return \Pulsar\Relation\Relation |
||
1157 | */ |
||
1158 | View Code Duplication | public function belongsToMany($model, $foreignKey = '', $localKey = '') |
|
1173 | |||
1174 | /** |
||
1175 | * Loads a given relationship (if not already) and returns |
||
1176 | * its results. |
||
1177 | * |
||
1178 | * @param string $name |
||
1179 | * |
||
1180 | * @return mixed |
||
1181 | */ |
||
1182 | protected function loadRelationship($name) |
||
1191 | |||
1192 | ///////////////////////////// |
||
1193 | // Events |
||
1194 | ///////////////////////////// |
||
1195 | |||
1196 | /** |
||
1197 | * Gets the event dispatcher. |
||
1198 | * |
||
1199 | * @return \Symfony\Component\EventDispatcher\EventDispatcher |
||
1200 | */ |
||
1201 | public static function getDispatcher($ignoreCache = false) |
||
1210 | |||
1211 | /** |
||
1212 | * Subscribes to a listener to an event. |
||
1213 | * |
||
1214 | * @param string $event event name |
||
1215 | * @param callable $listener |
||
1216 | * @param int $priority optional priority, higher #s get called first |
||
1217 | */ |
||
1218 | public static function listen($event, callable $listener, $priority = 0) |
||
1222 | |||
1223 | /** |
||
1224 | * Adds a listener to the model.creating event. |
||
1225 | * |
||
1226 | * @param callable $listener |
||
1227 | * @param int $priority |
||
1228 | */ |
||
1229 | public static function creating(callable $listener, $priority = 0) |
||
1233 | |||
1234 | /** |
||
1235 | * Adds a listener to the model.created event. |
||
1236 | * |
||
1237 | * @param callable $listener |
||
1238 | * @param int $priority |
||
1239 | */ |
||
1240 | public static function created(callable $listener, $priority = 0) |
||
1244 | |||
1245 | /** |
||
1246 | * Adds a listener to the model.updating event. |
||
1247 | * |
||
1248 | * @param callable $listener |
||
1249 | * @param int $priority |
||
1250 | */ |
||
1251 | public static function updating(callable $listener, $priority = 0) |
||
1255 | |||
1256 | /** |
||
1257 | * Adds a listener to the model.updated event. |
||
1258 | * |
||
1259 | * @param callable $listener |
||
1260 | * @param int $priority |
||
1261 | */ |
||
1262 | public static function updated(callable $listener, $priority = 0) |
||
1266 | |||
1267 | /** |
||
1268 | * Adds a listener to the model.deleting event. |
||
1269 | * |
||
1270 | * @param callable $listener |
||
1271 | * @param int $priority |
||
1272 | */ |
||
1273 | public static function deleting(callable $listener, $priority = 0) |
||
1277 | |||
1278 | /** |
||
1279 | * Adds a listener to the model.deleted event. |
||
1280 | * |
||
1281 | * @param callable $listener |
||
1282 | * @param int $priority |
||
1283 | */ |
||
1284 | public static function deleted(callable $listener, $priority = 0) |
||
1288 | |||
1289 | /** |
||
1290 | * Dispatches an event. |
||
1291 | * |
||
1292 | * @param string $eventName |
||
1293 | * |
||
1294 | * @return ModelEvent |
||
1295 | */ |
||
1296 | protected function dispatch($eventName) |
||
1302 | |||
1303 | ///////////////////////////// |
||
1304 | // Validation |
||
1305 | ///////////////////////////// |
||
1306 | |||
1307 | /** |
||
1308 | * Gets the error stack for this model instance. Used to |
||
1309 | * keep track of validation errors. |
||
1310 | * |
||
1311 | * @return Errors |
||
1312 | */ |
||
1313 | public function errors() |
||
1321 | |||
1322 | /** |
||
1323 | * Checks if the model is valid in its current state. |
||
1324 | * |
||
1325 | * @return bool |
||
1326 | */ |
||
1327 | public function valid() |
||
1344 | |||
1345 | /** |
||
1346 | * Gets a new validator instance for this model. |
||
1347 | * |
||
1348 | * @return Validator |
||
1349 | */ |
||
1350 | public function getValidator() |
||
1354 | } |
||
1355 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.