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'; |
||
| 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 $relationshipsDeprecated = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @staticvar array |
||
| 82 | */ |
||
| 83 | protected static $dates = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @staticvar array |
||
| 87 | */ |
||
| 88 | protected static $dispatchers = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $_values = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $_unsaved = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var bool |
||
| 102 | */ |
||
| 103 | protected $_persisted = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var Errors |
||
| 107 | */ |
||
| 108 | protected $_errors; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @deprecated |
||
| 112 | * |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | protected $_relationships = []; |
||
| 116 | |||
| 117 | ///////////////////////////// |
||
| 118 | // Base model variables |
||
| 119 | ///////////////////////////// |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @staticvar array |
||
| 123 | */ |
||
| 124 | private static $initialized = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @staticvar AdapterInterface |
||
| 128 | */ |
||
| 129 | private static $adapter; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @staticvar Locale |
||
| 133 | */ |
||
| 134 | private static $locale; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @staticvar array |
||
| 138 | */ |
||
| 139 | private static $tablenames = []; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @staticvar array |
||
| 143 | */ |
||
| 144 | private static $accessors = []; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @staticvar array |
||
| 148 | */ |
||
| 149 | private static $mutators = []; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var bool |
||
| 153 | */ |
||
| 154 | private $_ignoreUnsaved; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Creates a new model object. |
||
| 158 | * |
||
| 159 | * @param array $values values to fill model with |
||
| 160 | */ |
||
| 161 | public function __construct(array $values = []) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @deprecated |
||
| 184 | * Sets the default values from a deprecated $properties format |
||
| 185 | * |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | private function defaultValuesDeprecated() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * The initialize() method is called once per model. It's used |
||
| 200 | * to perform any one-off tasks before the model gets |
||
| 201 | * constructed. This is a great place to add any model |
||
| 202 | * properties. When extending this method be sure to call |
||
| 203 | * parent::initialize() as some important stuff happens here. |
||
| 204 | * If extending this method to add properties then you should |
||
| 205 | * call parent::initialize() after adding any properties. |
||
| 206 | */ |
||
| 207 | protected function initialize() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @deprecated |
||
| 229 | * Parses a deprecated $properties format |
||
| 230 | * |
||
| 231 | * @return self |
||
| 232 | */ |
||
| 233 | private function initializeDeprecated() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Installs the automatic timestamp properties, |
||
| 300 | * `created_at` and `updated_at`. |
||
| 301 | */ |
||
| 302 | private function installAutoTimestamps() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Sets the adapter for all models. |
||
| 322 | * |
||
| 323 | * @param AdapterInterface $adapter |
||
| 324 | */ |
||
| 325 | public static function setAdapter(AdapterInterface $adapter) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Gets the adapter for all models. |
||
| 332 | * |
||
| 333 | * @return AdapterInterface |
||
| 334 | * |
||
| 335 | * @throws AdapterMissingException |
||
| 336 | */ |
||
| 337 | public static function getAdapter() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Clears the adapter for all models. |
||
| 348 | */ |
||
| 349 | public static function clearAdapter() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @deprecated |
||
| 356 | */ |
||
| 357 | public static function setDriver(AdapterInterface $adapter) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @deprecated |
||
| 364 | */ |
||
| 365 | public static function getDriver() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @deprecated |
||
| 376 | */ |
||
| 377 | public static function clearDriver() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Sets the locale instance for all models. |
||
| 384 | * |
||
| 385 | * @param Locale $locale |
||
| 386 | */ |
||
| 387 | public static function setLocale(Locale $locale) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Gets the locale instance for all models. |
||
| 394 | * |
||
| 395 | * @return Locale |
||
| 396 | */ |
||
| 397 | public static function getLocale() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Clears the locale for all models. |
||
| 404 | */ |
||
| 405 | public static function clearLocale() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Gets the name of the model without namespacing. |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | public static function modelName() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Gets the table name of the model. |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public function getTablename() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Gets the model ID. |
||
| 441 | * |
||
| 442 | * @return string|number|null ID |
||
| 443 | */ |
||
| 444 | public function id() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Gets a key-value map of the model ID. |
||
| 459 | * |
||
| 460 | * @return array ID map |
||
| 461 | */ |
||
| 462 | public function ids() |
||
| 466 | |||
| 467 | ///////////////////////////// |
||
| 468 | // Magic Methods |
||
| 469 | ///////////////////////////// |
||
| 470 | |||
| 471 | public function __toString() |
||
| 475 | |||
| 476 | public function __get($name) |
||
| 483 | |||
| 484 | public function __set($name, $value) |
||
| 488 | |||
| 489 | public function __isset($name) |
||
| 493 | |||
| 494 | public function __unset($name) |
||
| 510 | |||
| 511 | public static function __callStatic($name, $parameters) |
||
| 518 | |||
| 519 | ///////////////////////////// |
||
| 520 | // ArrayAccess Interface |
||
| 521 | ///////////////////////////// |
||
| 522 | |||
| 523 | public function offsetExists($offset) |
||
| 527 | |||
| 528 | public function offsetGet($offset) |
||
| 532 | |||
| 533 | public function offsetSet($offset, $value) |
||
| 537 | |||
| 538 | public function offsetUnset($offset) |
||
| 542 | |||
| 543 | ///////////////////////////// |
||
| 544 | // Property Definitions |
||
| 545 | ///////////////////////////// |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Gets the names of the model ID properties. |
||
| 549 | * |
||
| 550 | * @return array |
||
| 551 | */ |
||
| 552 | public static function getIdProperties() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Builds an existing model instance given a single ID value or |
||
| 559 | * ordered array of ID values. |
||
| 560 | * |
||
| 561 | * @param mixed $id |
||
| 562 | * |
||
| 563 | * @return Model |
||
| 564 | */ |
||
| 565 | public static function buildFromId($id) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Gets the mutator method name for a given proeprty name. |
||
| 580 | * Looks for methods in the form of `setPropertyValue`. |
||
| 581 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 582 | * |
||
| 583 | * @param string $property |
||
| 584 | * |
||
| 585 | * @return string|false method name if it exists |
||
| 586 | */ |
||
| 587 | View Code Duplication | public static function getMutator($property) |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Gets the accessor method name for a given proeprty name. |
||
| 608 | * Looks for methods in the form of `getPropertyValue`. |
||
| 609 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 610 | * |
||
| 611 | * @param string $property |
||
| 612 | * |
||
| 613 | * @return string|false method name if it exists |
||
| 614 | */ |
||
| 615 | View Code Duplication | public static function getAccessor($property) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Checks if a given property is a relationship. |
||
| 636 | * |
||
| 637 | * @param string $property |
||
| 638 | * |
||
| 639 | * @return bool |
||
| 640 | */ |
||
| 641 | public static function isRelationship($property) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Gets the string date format for a property. Defaults to |
||
| 648 | * UNIX timestamps. |
||
| 649 | * |
||
| 650 | * @param string $property |
||
| 651 | * |
||
| 652 | * @return string |
||
| 653 | */ |
||
| 654 | public static function getDateFormat($property) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Gets the title of a property. |
||
| 665 | * |
||
| 666 | * @param string $name |
||
| 667 | * |
||
| 668 | * @return string |
||
| 669 | */ |
||
| 670 | public static function getPropertyTitle($name) |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Gets the type cast for a property. |
||
| 685 | * |
||
| 686 | * @param string $property |
||
| 687 | * |
||
| 688 | * @return string|null |
||
| 689 | */ |
||
| 690 | public static function getPropertyType($property) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Casts a value to a given type. |
||
| 699 | * |
||
| 700 | * @param string|null $type |
||
| 701 | * @param mixed $value |
||
| 702 | * @param string $property optional property name |
||
| 703 | * |
||
| 704 | * @return mixed casted value |
||
| 705 | */ |
||
| 706 | public static function cast($type, $value, $property = null) |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Gets the properties of this model. |
||
| 725 | * |
||
| 726 | * @return array |
||
| 727 | */ |
||
| 728 | public function getProperties() |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Checks if the model has a property. |
||
| 736 | * |
||
| 737 | * @param string $property |
||
| 738 | * |
||
| 739 | * @return bool has property |
||
| 740 | */ |
||
| 741 | public function hasProperty($property) |
||
| 746 | |||
| 747 | ///////////////////////////// |
||
| 748 | // Values |
||
| 749 | ///////////////////////////// |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Sets an unsaved value. |
||
| 753 | * |
||
| 754 | * @param string $name |
||
| 755 | * @param mixed $value |
||
| 756 | * @param bool $unsaved when true, sets an unsaved value |
||
| 757 | * |
||
| 758 | * @throws BadMethodCallException when setting a relationship |
||
| 759 | * |
||
| 760 | * @return self |
||
| 761 | */ |
||
| 762 | public function setValue($name, $value, $unsaved = true) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Sets a collection values on the model from an untrusted |
||
| 796 | * input. Also known as mass assignment. |
||
| 797 | * |
||
| 798 | * @param array $values |
||
| 799 | * |
||
| 800 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 801 | * |
||
| 802 | * @return self |
||
| 803 | */ |
||
| 804 | public function setValues($values) |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Ignores unsaved values when fetching the next value. |
||
| 827 | * |
||
| 828 | * @return self |
||
| 829 | */ |
||
| 830 | public function ignoreUnsaved() |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Gets a list of property values from the model. |
||
| 839 | * |
||
| 840 | * @param array $properties list of property values to fetch |
||
| 841 | * |
||
| 842 | * @return array |
||
| 843 | */ |
||
| 844 | public function getValues(array $properties) |
||
| 855 | |||
| 856 | /** |
||
| 857 | * @deprecated |
||
| 858 | */ |
||
| 859 | public function get(array $properties) |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Gets a property value from the model. |
||
| 866 | * |
||
| 867 | * Values are looked up in this order: |
||
| 868 | * 1. unsaved values |
||
| 869 | * 2. local values |
||
| 870 | * 3. relationships |
||
| 871 | * |
||
| 872 | * @throws InvalidArgumentException when a property was requested not present in the values |
||
| 873 | * |
||
| 874 | * @return mixed |
||
| 875 | */ |
||
| 876 | private function getValue($property) |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Converts the model to an array. |
||
| 909 | * |
||
| 910 | * @return array model array |
||
| 911 | */ |
||
| 912 | public function toArray() |
||
| 943 | |||
| 944 | ///////////////////////////// |
||
| 945 | // Persistence |
||
| 946 | ///////////////////////////// |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Saves the model. |
||
| 950 | * |
||
| 951 | * @return bool |
||
| 952 | */ |
||
| 953 | public function save() |
||
| 961 | |||
| 962 | /** |
||
| 963 | * Creates a new model. |
||
| 964 | * |
||
| 965 | * @param array $data optional key-value properties to set |
||
| 966 | * |
||
| 967 | * @return bool |
||
| 968 | * |
||
| 969 | * @throws BadMethodCallException when called on an existing model |
||
| 970 | */ |
||
| 971 | public function create(array $data = []) |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Gets the IDs for a newly created model. |
||
| 1010 | * |
||
| 1011 | * @return string |
||
| 1012 | */ |
||
| 1013 | protected function getNewIds() |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Updates the model. |
||
| 1031 | * |
||
| 1032 | * @param array $data optional key-value properties to set |
||
| 1033 | * |
||
| 1034 | * @return bool |
||
| 1035 | * |
||
| 1036 | * @throws BadMethodCallException when not called on an existing model |
||
| 1037 | */ |
||
| 1038 | public function set(array $data = []) |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * Delete the model. |
||
| 1081 | * |
||
| 1082 | * @return bool success |
||
| 1083 | */ |
||
| 1084 | public function delete() |
||
| 1109 | |||
| 1110 | /** |
||
| 1111 | * Tells if the model has been persisted. |
||
| 1112 | * |
||
| 1113 | * @return bool |
||
| 1114 | */ |
||
| 1115 | public function persisted() |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Loads the model from the data layer. |
||
| 1122 | * |
||
| 1123 | * @return self |
||
| 1124 | * |
||
| 1125 | * @throws NotFoundException |
||
| 1126 | */ |
||
| 1127 | public function refresh() |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Loads values into the model retrieved from the data layer. |
||
| 1151 | * |
||
| 1152 | * @param array $values values |
||
| 1153 | * |
||
| 1154 | * @return self |
||
| 1155 | */ |
||
| 1156 | public function refreshWith(array $values) |
||
| 1173 | |||
| 1174 | ///////////////////////////// |
||
| 1175 | // Queries |
||
| 1176 | ///////////////////////////// |
||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * Generates a new query instance. |
||
| 1180 | * |
||
| 1181 | * @return Query |
||
| 1182 | */ |
||
| 1183 | public static function query() |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Finds a single instance of a model given it's ID. |
||
| 1195 | * |
||
| 1196 | * @param mixed $id |
||
| 1197 | * |
||
| 1198 | * @return Model|null |
||
| 1199 | */ |
||
| 1200 | public static function find($id) |
||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1209 | * |
||
| 1210 | * @param mixed $id |
||
| 1211 | * |
||
| 1212 | * @return Model|false |
||
| 1213 | * |
||
| 1214 | * @throws NotFoundException when a model could not be found |
||
| 1215 | */ |
||
| 1216 | public static function findOrFail($id) |
||
| 1225 | |||
| 1226 | /** |
||
| 1227 | * Gets the toal number of records matching an optional criteria. |
||
| 1228 | * |
||
| 1229 | * @param array $where criteria |
||
| 1230 | * |
||
| 1231 | * @return int total |
||
| 1232 | */ |
||
| 1233 | public static function totalRecords(array $where = []) |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * @deprecated |
||
| 1243 | * Checks if the model exists in the database |
||
| 1244 | * |
||
| 1245 | * @return bool |
||
| 1246 | */ |
||
| 1247 | public function exists() |
||
| 1251 | |||
| 1252 | ///////////////////////////// |
||
| 1253 | // Relationships |
||
| 1254 | ///////////////////////////// |
||
| 1255 | |||
| 1256 | /** |
||
| 1257 | * Creates the parent side of a One-To-One relationship. |
||
| 1258 | * |
||
| 1259 | * @param string $model foreign model class |
||
| 1260 | * @param string $foreignKey identifying key on foreign model |
||
| 1261 | * @param string $localKey identifying key on local model |
||
| 1262 | * |
||
| 1263 | * @return \Pulsar\Relation\Relation |
||
| 1264 | */ |
||
| 1265 | public function hasOne($model, $foreignKey = '', $localKey = '') |
||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
| 1272 | * |
||
| 1273 | * @param string $model foreign model class |
||
| 1274 | * @param string $foreignKey identifying key on foreign model |
||
| 1275 | * @param string $localKey identifying key on local model |
||
| 1276 | * |
||
| 1277 | * @return \Pulsar\Relation\Relation |
||
| 1278 | */ |
||
| 1279 | public function belongsTo($model, $foreignKey = '', $localKey = '') |
||
| 1283 | |||
| 1284 | /** |
||
| 1285 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
| 1286 | * |
||
| 1287 | * @param string $model foreign model class |
||
| 1288 | * @param string $foreignKey identifying key on foreign model |
||
| 1289 | * @param string $localKey identifying key on local model |
||
| 1290 | * |
||
| 1291 | * @return \Pulsar\Relation\Relation |
||
| 1292 | */ |
||
| 1293 | public function hasMany($model, $foreignKey = '', $localKey = '') |
||
| 1297 | |||
| 1298 | /** |
||
| 1299 | * Creates the child side of a Many-To-Many relationship. |
||
| 1300 | * |
||
| 1301 | * @param string $model foreign model class |
||
| 1302 | * @param string $tablename pivot table name |
||
| 1303 | * @param string $foreignKey identifying key on foreign model |
||
| 1304 | * @param string $localKey identifying key on local model |
||
| 1305 | * |
||
| 1306 | * @return \Pulsar\Relation\Relation |
||
| 1307 | */ |
||
| 1308 | public function belongsToMany($model, $tablename = '', $foreignKey = '', $localKey = '') |
||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * Loads a given relationship (if not already) and |
||
| 1315 | * returns its results. |
||
| 1316 | * |
||
| 1317 | * @param string $name |
||
| 1318 | * |
||
| 1319 | * @return mixed |
||
| 1320 | */ |
||
| 1321 | protected function loadRelationship($name) |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * @deprecated |
||
| 1333 | * Gets a relationship model with a has one relationship |
||
| 1334 | * |
||
| 1335 | * @param string $k property |
||
| 1336 | * |
||
| 1337 | * @return \Pulsar\Model|null |
||
| 1338 | */ |
||
| 1339 | public function relation($k) |
||
| 1352 | |||
| 1353 | ///////////////////////////// |
||
| 1354 | // Events |
||
| 1355 | ///////////////////////////// |
||
| 1356 | |||
| 1357 | /** |
||
| 1358 | * Gets the event dispatcher. |
||
| 1359 | * |
||
| 1360 | * @return \Symfony\Component\EventDispatcher\EventDispatcher |
||
| 1361 | */ |
||
| 1362 | public static function getDispatcher($ignoreCache = false) |
||
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Subscribes to a listener to an event. |
||
| 1374 | * |
||
| 1375 | * @param string $event event name |
||
| 1376 | * @param callable $listener |
||
| 1377 | * @param int $priority optional priority, higher #s get called first |
||
| 1378 | */ |
||
| 1379 | public static function listen($event, callable $listener, $priority = 0) |
||
| 1383 | |||
| 1384 | /** |
||
| 1385 | * Adds a listener to the model.creating event. |
||
| 1386 | * |
||
| 1387 | * @param callable $listener |
||
| 1388 | * @param int $priority |
||
| 1389 | */ |
||
| 1390 | public static function creating(callable $listener, $priority = 0) |
||
| 1394 | |||
| 1395 | /** |
||
| 1396 | * Adds a listener to the model.created event. |
||
| 1397 | * |
||
| 1398 | * @param callable $listener |
||
| 1399 | * @param int $priority |
||
| 1400 | */ |
||
| 1401 | public static function created(callable $listener, $priority = 0) |
||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * Adds a listener to the model.updating event. |
||
| 1408 | * |
||
| 1409 | * @param callable $listener |
||
| 1410 | * @param int $priority |
||
| 1411 | */ |
||
| 1412 | public static function updating(callable $listener, $priority = 0) |
||
| 1416 | |||
| 1417 | /** |
||
| 1418 | * Adds a listener to the model.updated event. |
||
| 1419 | * |
||
| 1420 | * @param callable $listener |
||
| 1421 | * @param int $priority |
||
| 1422 | */ |
||
| 1423 | public static function updated(callable $listener, $priority = 0) |
||
| 1427 | |||
| 1428 | /** |
||
| 1429 | * Adds a listener to the model.creating and model.updating events. |
||
| 1430 | * |
||
| 1431 | * @param callable $listener |
||
| 1432 | * @param int $priority |
||
| 1433 | */ |
||
| 1434 | public static function saving(callable $listener, $priority = 0) |
||
| 1439 | |||
| 1440 | /** |
||
| 1441 | * Adds a listener to the model.created and model.updated events. |
||
| 1442 | * |
||
| 1443 | * @param callable $listener |
||
| 1444 | * @param int $priority |
||
| 1445 | */ |
||
| 1446 | public static function saved(callable $listener, $priority = 0) |
||
| 1451 | |||
| 1452 | /** |
||
| 1453 | * Adds a listener to the model.deleting event. |
||
| 1454 | * |
||
| 1455 | * @param callable $listener |
||
| 1456 | * @param int $priority |
||
| 1457 | */ |
||
| 1458 | public static function deleting(callable $listener, $priority = 0) |
||
| 1462 | |||
| 1463 | /** |
||
| 1464 | * Adds a listener to the model.deleted event. |
||
| 1465 | * |
||
| 1466 | * @param callable $listener |
||
| 1467 | * @param int $priority |
||
| 1468 | */ |
||
| 1469 | public static function deleted(callable $listener, $priority = 0) |
||
| 1473 | |||
| 1474 | /** |
||
| 1475 | * Dispatches an event. |
||
| 1476 | * |
||
| 1477 | * @param string $eventName |
||
| 1478 | * |
||
| 1479 | * @return bool true when the event propagated fully without being stopped |
||
| 1480 | */ |
||
| 1481 | protected function dispatch($eventName) |
||
| 1489 | |||
| 1490 | ///////////////////////////// |
||
| 1491 | // Validation |
||
| 1492 | ///////////////////////////// |
||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * Gets the error stack for this model instance. Used to |
||
| 1496 | * keep track of validation errors. |
||
| 1497 | * |
||
| 1498 | * @return Errors |
||
| 1499 | */ |
||
| 1500 | public function errors() |
||
| 1508 | |||
| 1509 | /** |
||
| 1510 | * Checks if the model is valid in its current state. |
||
| 1511 | * |
||
| 1512 | * @return bool |
||
| 1513 | */ |
||
| 1514 | public function valid() |
||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * Gets a new validator instance for this model. |
||
| 1534 | * |
||
| 1535 | * @return Validator |
||
| 1536 | */ |
||
| 1537 | public function getValidator() |
||
| 1541 | } |
||
| 1542 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.