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 |
||
| 30 | abstract class Model implements ArrayAccess |
||
| 31 | { |
||
| 32 | const TYPE_STRING = 'string'; |
||
| 33 | const TYPE_INTEGER = 'integer'; |
||
| 34 | const TYPE_FLOAT = 'float'; |
||
| 35 | const TYPE_BOOLEAN = 'boolean'; |
||
| 36 | const TYPE_DATE = 'date'; |
||
| 37 | const TYPE_OBJECT = 'object'; |
||
| 38 | const TYPE_ARRAY = 'array'; |
||
| 39 | |||
| 40 | const DEFAULT_ID_PROPERTY = 'id'; |
||
| 41 | |||
| 42 | const DEFAULT_DATE_FORMAT = 'U'; // unix timestamps |
||
| 43 | |||
| 44 | // DEPRECATED |
||
| 45 | const TYPE_NUMBER = 'float'; // DEPRECATED |
||
| 46 | const IMMUTABLE = 0; |
||
| 47 | const MUTABLE_CREATE_ONLY = 1; |
||
| 48 | const MUTABLE = 2; |
||
| 49 | |||
| 50 | ///////////////////////////// |
||
| 51 | // Model visible variables |
||
| 52 | ///////////////////////////// |
||
| 53 | |||
| 54 | /** |
||
| 55 | * List of model ID property names. |
||
| 56 | * |
||
| 57 | * @staticvar array |
||
| 58 | */ |
||
| 59 | protected static $ids = [self::DEFAULT_ID_PROPERTY]; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Validation rules expressed as a key-value map with |
||
| 63 | * property names as the keys. |
||
| 64 | * i.e. ['name' => 'string:2']. |
||
| 65 | * |
||
| 66 | * @staticvar array |
||
| 67 | */ |
||
| 68 | protected static $validations = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @staticvar array |
||
| 72 | */ |
||
| 73 | protected static $relationships = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @staticvar array |
||
| 77 | */ |
||
| 78 | protected static $dates = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @staticvar array |
||
| 82 | */ |
||
| 83 | protected static $dispatchers = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $_values = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $_unsaved = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var bool |
||
| 97 | */ |
||
| 98 | protected $_persisted = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var Errors |
||
| 102 | */ |
||
| 103 | protected $_errors; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | protected $_relationships = []; |
||
| 109 | |||
| 110 | ///////////////////////////// |
||
| 111 | // Base model variables |
||
| 112 | ///////////////////////////// |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @staticvar array |
||
| 116 | */ |
||
| 117 | private static $initialized = []; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @staticvar AdapterInterface |
||
| 121 | */ |
||
| 122 | private static $adapter; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @staticvar Locale |
||
| 126 | */ |
||
| 127 | private static $locale; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @staticvar array |
||
| 131 | */ |
||
| 132 | private static $tablenames = []; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @staticvar array |
||
| 136 | */ |
||
| 137 | private static $accessors = []; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @staticvar array |
||
| 141 | */ |
||
| 142 | private static $mutators = []; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var bool |
||
| 146 | */ |
||
| 147 | private $_ignoreUnsaved; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Creates a new model object. |
||
| 151 | * |
||
| 152 | * @param array $values values to fill model with |
||
| 153 | */ |
||
| 154 | public function __construct(array $values = []) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * The initialize() method is called once per model. It's used |
||
| 170 | * to perform any one-off tasks before the model gets |
||
| 171 | * constructed. This is a great place to add any model |
||
| 172 | * properties. When extending this method be sure to call |
||
| 173 | * parent::initialize() as some important stuff happens here. |
||
| 174 | * If extending this method to add properties then you should |
||
| 175 | * call parent::initialize() after adding any properties. |
||
| 176 | */ |
||
| 177 | protected function initialize() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Installs the automatic timestamp properties, |
||
| 194 | * `created_at` and `updated_at`. |
||
| 195 | */ |
||
| 196 | private function installAutoTimestamps() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Sets the adapter for all models. |
||
| 216 | * |
||
| 217 | * @param AdapterInterface $adapter |
||
| 218 | */ |
||
| 219 | public static function setAdapter(AdapterInterface $adapter) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Gets the adapter for all models. |
||
| 226 | * |
||
| 227 | * @return AdapterInterface |
||
| 228 | * |
||
| 229 | * @throws AdapterMissingException |
||
| 230 | */ |
||
| 231 | public static function getAdapter() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Clears the adapter for all models. |
||
| 242 | */ |
||
| 243 | public static function clearAdapter() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Sets the locale instance for all models. |
||
| 250 | * |
||
| 251 | * @param Locale $locale |
||
| 252 | */ |
||
| 253 | public static function setLocale(Locale $locale) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Gets the locale instance for all models. |
||
| 260 | * |
||
| 261 | * @return Locale |
||
| 262 | */ |
||
| 263 | public static function getLocale() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Clears the locale for all models. |
||
| 270 | */ |
||
| 271 | public static function clearLocale() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Gets the name of the model without namespacing. |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public static function modelName() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Gets the table name of the model. |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function getTablename() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Gets the model ID. |
||
| 305 | * |
||
| 306 | * @return string|number|null ID |
||
| 307 | */ |
||
| 308 | public function id() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Gets a key-value map of the model ID. |
||
| 323 | * |
||
| 324 | * @return array ID map |
||
| 325 | */ |
||
| 326 | public function ids() |
||
| 330 | |||
| 331 | ///////////////////////////// |
||
| 332 | // Magic Methods |
||
| 333 | ///////////////////////////// |
||
| 334 | |||
| 335 | public function __toString() |
||
| 339 | |||
| 340 | public function __get($name) |
||
| 347 | |||
| 348 | public function __set($name, $value) |
||
| 352 | |||
| 353 | public function __isset($name) |
||
| 357 | |||
| 358 | public function __unset($name) |
||
| 374 | |||
| 375 | public static function __callStatic($name, $parameters) |
||
| 382 | |||
| 383 | ///////////////////////////// |
||
| 384 | // ArrayAccess Interface |
||
| 385 | ///////////////////////////// |
||
| 386 | |||
| 387 | public function offsetExists($offset) |
||
| 391 | |||
| 392 | public function offsetGet($offset) |
||
| 396 | |||
| 397 | public function offsetSet($offset, $value) |
||
| 401 | |||
| 402 | public function offsetUnset($offset) |
||
| 406 | |||
| 407 | ///////////////////////////// |
||
| 408 | // Property Definitions |
||
| 409 | ///////////////////////////// |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Gets the names of the model ID properties. |
||
| 413 | * |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | public static function getIdProperties() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Builds an existing model instance given a single ID value or |
||
| 423 | * ordered array of ID values. |
||
| 424 | * |
||
| 425 | * @param mixed $id |
||
| 426 | * |
||
| 427 | * @return Model |
||
| 428 | */ |
||
| 429 | public static function buildFromId($id) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Gets the mutator method name for a given proeprty name. |
||
| 444 | * Looks for methods in the form of `setPropertyValue`. |
||
| 445 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 446 | * |
||
| 447 | * @param string $property |
||
| 448 | * |
||
| 449 | * @return string|false method name if it exists |
||
| 450 | */ |
||
| 451 | View Code Duplication | public static function getMutator($property) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Gets the accessor method name for a given proeprty name. |
||
| 472 | * Looks for methods in the form of `getPropertyValue`. |
||
| 473 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 474 | * |
||
| 475 | * @param string $property |
||
| 476 | * |
||
| 477 | * @return string|false method name if it exists |
||
| 478 | */ |
||
| 479 | View Code Duplication | public static function getAccessor($property) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Checks if a given property is a relationship. |
||
| 500 | * |
||
| 501 | * @param string $property |
||
| 502 | * |
||
| 503 | * @return bool |
||
| 504 | */ |
||
| 505 | public static function isRelationship($property) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Gets the string date format for a property. Defaults to |
||
| 512 | * UNIX timestamps. |
||
| 513 | * |
||
| 514 | * @param string $property |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public static function getDateFormat($property) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Gets the title of a property. |
||
| 529 | * |
||
| 530 | * @param string $name |
||
| 531 | * |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | public static function getPropertyTitle($name) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Gets the type cast for a property. |
||
| 549 | * |
||
| 550 | * @param string $property |
||
| 551 | * |
||
| 552 | * @return string|null |
||
| 553 | */ |
||
| 554 | public static function getPropertyType($property) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Casts a value to a given type. |
||
| 563 | * |
||
| 564 | * @param string|null $type |
||
| 565 | * @param mixed $value |
||
| 566 | * @param string $property optional property name |
||
| 567 | * |
||
| 568 | * @return mixed casted value |
||
| 569 | */ |
||
| 570 | public static function cast($type, $value, $property = null) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Gets the properties of this model. |
||
| 589 | * |
||
| 590 | * @return array |
||
| 591 | */ |
||
| 592 | public function getProperties() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Checks if the model has a property. |
||
| 600 | * |
||
| 601 | * @param string $property |
||
| 602 | * |
||
| 603 | * @return bool has property |
||
| 604 | */ |
||
| 605 | public function hasProperty($property) |
||
| 610 | |||
| 611 | ///////////////////////////// |
||
| 612 | // Values |
||
| 613 | ///////////////////////////// |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Sets an unsaved value. |
||
| 617 | * |
||
| 618 | * @param string $name |
||
| 619 | * @param mixed $value |
||
| 620 | * @param bool $unsaved when true, sets an unsaved value |
||
| 621 | * |
||
| 622 | * @throws BadMethodCallException when setting a relationship |
||
| 623 | * |
||
| 624 | * @return self |
||
| 625 | */ |
||
| 626 | public function setValue($name, $value, $unsaved = true) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Sets a collection values on the model from an untrusted |
||
| 660 | * input. Also known as mass assignment. |
||
| 661 | * |
||
| 662 | * @param array $values |
||
| 663 | * |
||
| 664 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 665 | * |
||
| 666 | * @return self |
||
| 667 | */ |
||
| 668 | public function setValues($values) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Ignores unsaved values when fetching the next value. |
||
| 691 | * |
||
| 692 | * @return self |
||
| 693 | */ |
||
| 694 | public function ignoreUnsaved() |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Gets a list of property values from the model. |
||
| 703 | * |
||
| 704 | * @param array $properties list of property values to fetch |
||
| 705 | * |
||
| 706 | * @return array |
||
| 707 | */ |
||
| 708 | public function getValues(array $properties) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * @deprecated |
||
| 722 | */ |
||
| 723 | public function get(array $properties) |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Gets a property value from the model. |
||
| 730 | * |
||
| 731 | * Values are looked up in this order: |
||
| 732 | * 1. unsaved values |
||
| 733 | * 2. local values |
||
| 734 | * 3. relationships |
||
| 735 | * |
||
| 736 | * @throws InvalidArgumentException when a property was requested not present in the values |
||
| 737 | * |
||
| 738 | * @return mixed |
||
| 739 | */ |
||
| 740 | private function getValue($property) |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Converts the model to an array. |
||
| 773 | * |
||
| 774 | * @return array model array |
||
| 775 | */ |
||
| 776 | public function toArray() |
||
| 807 | |||
| 808 | ///////////////////////////// |
||
| 809 | // Persistence |
||
| 810 | ///////////////////////////// |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Saves the model. |
||
| 814 | * |
||
| 815 | * @return bool |
||
| 816 | */ |
||
| 817 | public function save() |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Creates a new model. |
||
| 828 | * |
||
| 829 | * @param array $data optional key-value properties to set |
||
| 830 | * |
||
| 831 | * @return bool |
||
| 832 | * |
||
| 833 | * @throws BadMethodCallException when called on an existing model |
||
| 834 | */ |
||
| 835 | public function create(array $data = []) |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Gets the IDs for a newly created model. |
||
| 874 | * |
||
| 875 | * @return string |
||
| 876 | */ |
||
| 877 | protected function getNewIds() |
||
| 892 | |||
| 893 | /** |
||
| 894 | * Updates the model. |
||
| 895 | * |
||
| 896 | * @param array $data optional key-value properties to set |
||
| 897 | * |
||
| 898 | * @return bool |
||
| 899 | * |
||
| 900 | * @throws BadMethodCallException when not called on an existing model |
||
| 901 | */ |
||
| 902 | public function set(array $data = []) |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Delete the model. |
||
| 940 | * |
||
| 941 | * @return bool success |
||
| 942 | */ |
||
| 943 | public function delete() |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Tells if the model has been persisted. |
||
| 971 | * |
||
| 972 | * @return bool |
||
| 973 | */ |
||
| 974 | public function persisted() |
||
| 978 | |||
| 979 | /** |
||
| 980 | * Loads the model from the data layer. |
||
| 981 | * |
||
| 982 | * @return self |
||
| 983 | * |
||
| 984 | * @throws NotFoundException |
||
| 985 | */ |
||
| 986 | public function refresh() |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Loads values into the model retrieved from the data layer. |
||
| 1010 | * |
||
| 1011 | * @param array $values values |
||
| 1012 | * |
||
| 1013 | * @return self |
||
| 1014 | */ |
||
| 1015 | public function refreshWith(array $values) |
||
| 1032 | |||
| 1033 | ///////////////////////////// |
||
| 1034 | // Queries |
||
| 1035 | ///////////////////////////// |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Generates a new query instance. |
||
| 1039 | * |
||
| 1040 | * @return Query |
||
| 1041 | */ |
||
| 1042 | public static function query() |
||
| 1051 | |||
| 1052 | /** |
||
| 1053 | * Finds a single instance of a model given it's ID. |
||
| 1054 | * |
||
| 1055 | * @param mixed $id |
||
| 1056 | * |
||
| 1057 | * @return Model|null |
||
| 1058 | */ |
||
| 1059 | public static function find($id) |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1068 | * |
||
| 1069 | * @param mixed $id |
||
| 1070 | * |
||
| 1071 | * @return Model|false |
||
| 1072 | * |
||
| 1073 | * @throws NotFoundException when a model could not be found |
||
| 1074 | */ |
||
| 1075 | public static function findOrFail($id) |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * Gets the toal number of records matching an optional criteria. |
||
| 1087 | * |
||
| 1088 | * @param array $where criteria |
||
| 1089 | * |
||
| 1090 | * @return int total |
||
| 1091 | */ |
||
| 1092 | public static function totalRecords(array $where = []) |
||
| 1099 | |||
| 1100 | ///////////////////////////// |
||
| 1101 | // Relationships |
||
| 1102 | ///////////////////////////// |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Creates the parent side of a One-To-One relationship. |
||
| 1106 | * |
||
| 1107 | * @param string $model foreign model class |
||
| 1108 | * @param string $foreignKey identifying key on foreign model |
||
| 1109 | * @param string $localKey identifying key on local model |
||
| 1110 | * |
||
| 1111 | * @return \Pulsar\Relation\Relation |
||
| 1112 | */ |
||
| 1113 | public function hasOne($model, $foreignKey = '', $localKey = '') |
||
| 1117 | |||
| 1118 | /** |
||
| 1119 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
| 1120 | * |
||
| 1121 | * @param string $model foreign model class |
||
| 1122 | * @param string $foreignKey identifying key on foreign model |
||
| 1123 | * @param string $localKey identifying key on local model |
||
| 1124 | * |
||
| 1125 | * @return \Pulsar\Relation\Relation |
||
| 1126 | */ |
||
| 1127 | public function belongsTo($model, $foreignKey = '', $localKey = '') |
||
| 1131 | |||
| 1132 | /** |
||
| 1133 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
| 1134 | * |
||
| 1135 | * @param string $model foreign model class |
||
| 1136 | * @param string $foreignKey identifying key on foreign model |
||
| 1137 | * @param string $localKey identifying key on local model |
||
| 1138 | * |
||
| 1139 | * @return \Pulsar\Relation\Relation |
||
| 1140 | */ |
||
| 1141 | public function hasMany($model, $foreignKey = '', $localKey = '') |
||
| 1145 | |||
| 1146 | /** |
||
| 1147 | * Creates the child side of a Many-To-Many relationship. |
||
| 1148 | * |
||
| 1149 | * @param string $model foreign model class |
||
| 1150 | * @param string $tablename pivot table name |
||
| 1151 | * @param string $foreignKey identifying key on foreign model |
||
| 1152 | * @param string $localKey identifying key on local model |
||
| 1153 | * |
||
| 1154 | * @return \Pulsar\Relation\Relation |
||
| 1155 | */ |
||
| 1156 | public function belongsToMany($model, $tablename = '', $foreignKey = '', $localKey = '') |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * Loads a given relationship (if not already) and |
||
| 1163 | * returns its results. |
||
| 1164 | * |
||
| 1165 | * @param string $name |
||
| 1166 | * |
||
| 1167 | * @return mixed |
||
| 1168 | */ |
||
| 1169 | protected function loadRelationship($name) |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * @deprecated |
||
| 1181 | * Gets a relationship model with a has one relationship |
||
| 1182 | * |
||
| 1183 | * @param string $k property |
||
| 1184 | * |
||
| 1185 | * @return \Pulsar\Model|null |
||
| 1186 | */ |
||
| 1187 | public function relation($k) |
||
| 1200 | |||
| 1201 | ///////////////////////////// |
||
| 1202 | // Events |
||
| 1203 | ///////////////////////////// |
||
| 1204 | |||
| 1205 | /** |
||
| 1206 | * Gets the event dispatcher. |
||
| 1207 | * |
||
| 1208 | * @return \Symfony\Component\EventDispatcher\EventDispatcher |
||
| 1209 | */ |
||
| 1210 | public static function getDispatcher($ignoreCache = false) |
||
| 1219 | |||
| 1220 | /** |
||
| 1221 | * Subscribes to a listener to an event. |
||
| 1222 | * |
||
| 1223 | * @param string $event event name |
||
| 1224 | * @param callable $listener |
||
| 1225 | * @param int $priority optional priority, higher #s get called first |
||
| 1226 | */ |
||
| 1227 | public static function listen($event, callable $listener, $priority = 0) |
||
| 1231 | |||
| 1232 | /** |
||
| 1233 | * Adds a listener to the model.creating event. |
||
| 1234 | * |
||
| 1235 | * @param callable $listener |
||
| 1236 | * @param int $priority |
||
| 1237 | */ |
||
| 1238 | public static function creating(callable $listener, $priority = 0) |
||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Adds a listener to the model.created event. |
||
| 1245 | * |
||
| 1246 | * @param callable $listener |
||
| 1247 | * @param int $priority |
||
| 1248 | */ |
||
| 1249 | public static function created(callable $listener, $priority = 0) |
||
| 1253 | |||
| 1254 | /** |
||
| 1255 | * Adds a listener to the model.updating event. |
||
| 1256 | * |
||
| 1257 | * @param callable $listener |
||
| 1258 | * @param int $priority |
||
| 1259 | */ |
||
| 1260 | public static function updating(callable $listener, $priority = 0) |
||
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Adds a listener to the model.updated event. |
||
| 1267 | * |
||
| 1268 | * @param callable $listener |
||
| 1269 | * @param int $priority |
||
| 1270 | */ |
||
| 1271 | public static function updated(callable $listener, $priority = 0) |
||
| 1275 | |||
| 1276 | /** |
||
| 1277 | * Adds a listener to the model.creating and model.updating events. |
||
| 1278 | * |
||
| 1279 | * @param callable $listener |
||
| 1280 | * @param int $priority |
||
| 1281 | */ |
||
| 1282 | public static function saving(callable $listener, $priority = 0) |
||
| 1287 | |||
| 1288 | /** |
||
| 1289 | * Adds a listener to the model.created and model.updated events. |
||
| 1290 | * |
||
| 1291 | * @param callable $listener |
||
| 1292 | * @param int $priority |
||
| 1293 | */ |
||
| 1294 | public static function saved(callable $listener, $priority = 0) |
||
| 1299 | |||
| 1300 | /** |
||
| 1301 | * Adds a listener to the model.deleting event. |
||
| 1302 | * |
||
| 1303 | * @param callable $listener |
||
| 1304 | * @param int $priority |
||
| 1305 | */ |
||
| 1306 | public static function deleting(callable $listener, $priority = 0) |
||
| 1310 | |||
| 1311 | /** |
||
| 1312 | * Adds a listener to the model.deleted event. |
||
| 1313 | * |
||
| 1314 | * @param callable $listener |
||
| 1315 | * @param int $priority |
||
| 1316 | */ |
||
| 1317 | public static function deleted(callable $listener, $priority = 0) |
||
| 1321 | |||
| 1322 | /** |
||
| 1323 | * Dispatches an event. |
||
| 1324 | * |
||
| 1325 | * @param string $eventName |
||
| 1326 | * |
||
| 1327 | * @return bool true when the event propagated fully without being stopped |
||
| 1328 | */ |
||
| 1329 | protected function dispatch($eventName) |
||
| 1337 | |||
| 1338 | ///////////////////////////// |
||
| 1339 | // Validation |
||
| 1340 | ///////////////////////////// |
||
| 1341 | |||
| 1342 | /** |
||
| 1343 | * Gets the error stack for this model instance. Used to |
||
| 1344 | * keep track of validation errors. |
||
| 1345 | * |
||
| 1346 | * @return Errors |
||
| 1347 | */ |
||
| 1348 | public function errors() |
||
| 1356 | |||
| 1357 | /** |
||
| 1358 | * Checks if the model is valid in its current state. |
||
| 1359 | * |
||
| 1360 | * @return bool |
||
| 1361 | */ |
||
| 1362 | public function valid() |
||
| 1379 | |||
| 1380 | /** |
||
| 1381 | * Gets a new validator instance for this model. |
||
| 1382 | * |
||
| 1383 | * @return Validator |
||
| 1384 | */ |
||
| 1385 | public function getValidator() |
||
| 1389 | } |
||
| 1390 |
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@propertyannotation 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.