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 |
||
| 45 | abstract class Model implements \ArrayAccess |
||
| 46 | { |
||
| 47 | const IMMUTABLE = 0; |
||
| 48 | const MUTABLE_CREATE_ONLY = 1; |
||
| 49 | const MUTABLE = 2; |
||
| 50 | |||
| 51 | const TYPE_STRING = 'string'; |
||
| 52 | const TYPE_INTEGER = 'integer'; |
||
| 53 | const TYPE_FLOAT = 'float'; |
||
| 54 | const TYPE_BOOLEAN = 'boolean'; |
||
| 55 | const TYPE_DATE = 'date'; |
||
| 56 | const TYPE_OBJECT = 'object'; |
||
| 57 | const TYPE_ARRAY = 'array'; |
||
| 58 | |||
| 59 | const RELATIONSHIP_HAS_ONE = 'has_one'; |
||
| 60 | const RELATIONSHIP_HAS_MANY = 'has_many'; |
||
| 61 | const RELATIONSHIP_BELONGS_TO = 'belongs_to'; |
||
| 62 | const RELATIONSHIP_BELONGS_TO_MANY = 'belongs_to_many'; |
||
| 63 | |||
| 64 | const DEFAULT_ID_PROPERTY = 'id'; |
||
| 65 | |||
| 66 | ///////////////////////////// |
||
| 67 | // Model visible variables |
||
| 68 | ///////////////////////////// |
||
| 69 | |||
| 70 | /** |
||
| 71 | * List of model ID property names. |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected static $ids = [self::DEFAULT_ID_PROPERTY]; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Property definitions expressed as a key-value map with |
||
| 79 | * property names as the keys. |
||
| 80 | * i.e. ['enabled' => ['type' => Model::TYPE_BOOLEAN]]. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected static $properties = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected static $dispatchers; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var number|string|false |
||
| 93 | */ |
||
| 94 | protected $_id; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | protected $_ids; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | protected $_values = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | protected $_unsaved = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var bool |
||
| 113 | */ |
||
| 114 | protected $_persisted = false; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var bool |
||
| 118 | */ |
||
| 119 | protected $_loaded = false; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | protected $_relationships = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var Errors |
||
| 128 | */ |
||
| 129 | protected $_errors; |
||
| 130 | |||
| 131 | ///////////////////////////// |
||
| 132 | // Base model variables |
||
| 133 | ///////////////////////////// |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var array |
||
| 137 | */ |
||
| 138 | private static $propertyDefinitionBase = [ |
||
| 139 | 'type' => null, |
||
| 140 | 'mutable' => self::MUTABLE, |
||
| 141 | 'null' => false, |
||
| 142 | 'unique' => false, |
||
| 143 | 'required' => false, |
||
| 144 | ]; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var array |
||
| 148 | */ |
||
| 149 | private static $defaultIDProperty = [ |
||
| 150 | 'type' => self::TYPE_INTEGER, |
||
| 151 | 'mutable' => self::IMMUTABLE, |
||
| 152 | ]; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var array |
||
| 156 | */ |
||
| 157 | private static $timestampProperties = [ |
||
| 158 | 'created_at' => [ |
||
| 159 | 'type' => self::TYPE_DATE, |
||
| 160 | 'validate' => 'timestamp|db_timestamp', |
||
| 161 | ], |
||
| 162 | 'updated_at' => [ |
||
| 163 | 'type' => self::TYPE_DATE, |
||
| 164 | 'validate' => 'timestamp|db_timestamp', |
||
| 165 | ], |
||
| 166 | ]; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var array |
||
| 170 | */ |
||
| 171 | private static $softDeleteProperties = [ |
||
| 172 | 'deleted_at' => [ |
||
| 173 | 'type' => self::TYPE_DATE, |
||
| 174 | 'validate' => 'timestamp|db_timestamp', |
||
| 175 | 'null' => true, |
||
| 176 | ], |
||
| 177 | ]; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var array |
||
| 181 | */ |
||
| 182 | private static $initialized = []; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @var DriverInterface |
||
| 186 | */ |
||
| 187 | private static $driver; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var array |
||
| 191 | */ |
||
| 192 | private static $accessors = []; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @var array |
||
| 196 | */ |
||
| 197 | private static $mutators = []; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @var bool |
||
| 201 | */ |
||
| 202 | private $_ignoreUnsaved; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Creates a new model object. |
||
| 206 | * |
||
| 207 | * @param array|string|Model|false $id ordered array of ids or comma-separated id string |
||
| 208 | * @param array $values optional key-value map to pre-seed model |
||
| 209 | */ |
||
| 210 | public function __construct($id = false, array $values = []) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Performs initialization on this model. |
||
| 226 | */ |
||
| 227 | private function init() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * The initialize() method is called once per model. It's used |
||
| 239 | * to perform any one-off tasks before the model gets |
||
| 240 | * constructed. This is a great place to add any model |
||
| 241 | * properties. When extending this method be sure to call |
||
| 242 | * parent::initialize() as some important stuff happens here. |
||
| 243 | * If extending this method to add properties then you should |
||
| 244 | * call parent::initialize() after adding any properties. |
||
| 245 | */ |
||
| 246 | protected function initialize() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Installs the `created_at` and `updated_at` properties. |
||
| 302 | */ |
||
| 303 | private function installAutoTimestamps() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Installs the `deleted_at` properties. |
||
| 320 | */ |
||
| 321 | private function installSoftDelete() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Parses the given ID, which can be a single or composite primary key. |
||
| 328 | * |
||
| 329 | * @param mixed $id |
||
| 330 | */ |
||
| 331 | private function parseId($id) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Sets the driver for all models. |
||
| 377 | * |
||
| 378 | * @param DriverInterface $driver |
||
| 379 | */ |
||
| 380 | public static function setDriver(DriverInterface $driver) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Gets the driver for all models. |
||
| 387 | * |
||
| 388 | * @return DriverInterface |
||
| 389 | * |
||
| 390 | * @throws DriverMissingException when a driver has not been set yet |
||
| 391 | */ |
||
| 392 | public static function getDriver(): DriverInterface |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Clears the driver for all models. |
||
| 403 | */ |
||
| 404 | public static function clearDriver() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Gets the name of the model, i.e. User. |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public static function modelName(): string |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Gets the model ID. |
||
| 424 | * |
||
| 425 | * @return string|number|false ID |
||
| 426 | */ |
||
| 427 | public function id() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Gets a key-value map of the model ID. |
||
| 434 | * |
||
| 435 | * @return array ID map |
||
| 436 | */ |
||
| 437 | public function ids(): array |
||
| 441 | |||
| 442 | ///////////////////////////// |
||
| 443 | // Magic Methods |
||
| 444 | ///////////////////////////// |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Converts the model into a string. |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public function __toString() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Shortcut to a get() call for a given property. |
||
| 461 | * |
||
| 462 | * @param string $name |
||
| 463 | * |
||
| 464 | * @return mixed |
||
| 465 | */ |
||
| 466 | public function __get($name) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Sets an unsaved value. |
||
| 475 | * |
||
| 476 | * @param string $name |
||
| 477 | * @param mixed $value |
||
| 478 | */ |
||
| 479 | public function __set($name, $value) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Checks if an unsaved value or property exists by this name. |
||
| 497 | * |
||
| 498 | * @param string $name |
||
| 499 | * |
||
| 500 | * @return bool |
||
| 501 | */ |
||
| 502 | public function __isset($name) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Unsets an unsaved value. |
||
| 509 | * |
||
| 510 | * @param string $name |
||
| 511 | */ |
||
| 512 | public function __unset($name) |
||
| 523 | |||
| 524 | ///////////////////////////// |
||
| 525 | // ArrayAccess Interface |
||
| 526 | ///////////////////////////// |
||
| 527 | |||
| 528 | public function offsetExists($offset) |
||
| 532 | |||
| 533 | public function offsetGet($offset) |
||
| 537 | |||
| 538 | public function offsetSet($offset, $value) |
||
| 542 | |||
| 543 | public function offsetUnset($offset) |
||
| 547 | |||
| 548 | public static function __callStatic($name, $parameters) |
||
| 555 | |||
| 556 | ///////////////////////////// |
||
| 557 | // Property Definitions |
||
| 558 | ///////////////////////////// |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Gets all the property definitions for the model. |
||
| 562 | * |
||
| 563 | * @return array key-value map of properties |
||
| 564 | */ |
||
| 565 | public static function getProperties(): array |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Gets a property defition for the model. |
||
| 572 | * |
||
| 573 | * @param string $property property to lookup |
||
| 574 | * |
||
| 575 | * @return array|null property |
||
| 576 | */ |
||
| 577 | public static function getProperty(string $property): ?array |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Gets the names of the model ID properties. |
||
| 584 | * |
||
| 585 | * @return array |
||
| 586 | */ |
||
| 587 | public static function getIDProperties(): array |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Checks if the model has a property. |
||
| 594 | * |
||
| 595 | * @param string $property property |
||
| 596 | * |
||
| 597 | * @return bool has property |
||
| 598 | */ |
||
| 599 | public static function hasProperty(string $property): bool |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Gets the mutator method name for a given proeprty name. |
||
| 606 | * Looks for methods in the form of `setPropertyValue`. |
||
| 607 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 608 | * |
||
| 609 | * @param string $property property |
||
| 610 | * |
||
| 611 | * @return string|null method name if it exists |
||
| 612 | */ |
||
| 613 | public static function getMutator(string $property): ?string |
||
| 614 | { |
||
| 615 | $class = get_called_class(); |
||
| 616 | |||
| 617 | $k = $class.':'.$property; |
||
| 618 | if (!array_key_exists($k, self::$mutators)) { |
||
| 619 | $inflector = Inflector::get(); |
||
| 620 | $method = 'set'.$inflector->camelize($property).'Value'; |
||
| 621 | |||
| 622 | if (!method_exists($class, $method)) { |
||
| 623 | $method = null; |
||
| 624 | } |
||
| 625 | |||
| 626 | self::$mutators[$k] = $method; |
||
| 627 | } |
||
| 628 | |||
| 629 | return self::$mutators[$k]; |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Gets the accessor method name for a given proeprty name. |
||
| 634 | * Looks for methods in the form of `getPropertyValue`. |
||
| 635 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 636 | * |
||
| 637 | * @param string $property property |
||
| 638 | * |
||
| 639 | * @return string|null method name if it exists |
||
| 640 | */ |
||
| 641 | public static function getAccessor(string $property): ?string |
||
| 642 | { |
||
| 643 | $class = get_called_class(); |
||
| 644 | |||
| 645 | $k = $class.':'.$property; |
||
| 646 | if (!array_key_exists($k, self::$accessors)) { |
||
| 647 | $inflector = Inflector::get(); |
||
| 648 | $method = 'get'.$inflector->camelize($property).'Value'; |
||
| 649 | |||
| 650 | if (!method_exists($class, $method)) { |
||
| 651 | $method = null; |
||
| 652 | } |
||
| 653 | |||
| 654 | self::$accessors[$k] = $method; |
||
| 655 | } |
||
| 656 | |||
| 657 | return self::$accessors[$k]; |
||
| 658 | } |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Marshals a value for a given property from storage. |
||
| 662 | * |
||
| 663 | * @param array $property |
||
| 664 | * @param mixed $value |
||
| 665 | * |
||
| 666 | * @return mixed type-casted value |
||
| 667 | */ |
||
| 668 | public static function cast(array $property, $value) |
||
| 688 | |||
| 689 | ///////////////////////////// |
||
| 690 | // CRUD Operations |
||
| 691 | ///////////////////////////// |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Gets the tablename for storing this model. |
||
| 695 | * |
||
| 696 | * @return string |
||
| 697 | */ |
||
| 698 | public function getTablename(): string |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Gets the ID of the connection in the connection manager |
||
| 707 | * that stores this model. |
||
| 708 | * |
||
| 709 | * @return string|null |
||
| 710 | */ |
||
| 711 | public function getConnection(): ?string |
||
| 715 | |||
| 716 | protected function usesTransactions(): bool |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Saves the model. |
||
| 723 | * |
||
| 724 | * @return bool true when the operation was successful |
||
| 725 | */ |
||
| 726 | public function save(): bool |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Saves the model. Throws an exception when the operation fails. |
||
| 737 | * |
||
| 738 | * @throws ModelException when the model cannot be saved |
||
| 739 | */ |
||
| 740 | public function saveOrFail() |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Creates a new model. |
||
| 754 | * |
||
| 755 | * @param array $data optional key-value properties to set |
||
| 756 | * |
||
| 757 | * @return bool true when the operation was successful |
||
| 758 | * |
||
| 759 | * @throws BadMethodCallException when called on an existing model |
||
| 760 | */ |
||
| 761 | public function create(array $data = []): bool |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Ignores unsaved values when fetching the next value. |
||
| 866 | * |
||
| 867 | * @return $this |
||
| 868 | */ |
||
| 869 | public function ignoreUnsaved() |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Fetches property values from the model. |
||
| 878 | * |
||
| 879 | * This method looks up values in this order: |
||
| 880 | * IDs, local cache, unsaved values, storage layer, defaults |
||
| 881 | * |
||
| 882 | * @param array $properties list of property names to fetch values of |
||
| 883 | * |
||
| 884 | * @return array |
||
| 885 | */ |
||
| 886 | public function get(array $properties): array |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Gets a property value from the model. |
||
| 927 | * |
||
| 928 | * Values are looked up in this order: |
||
| 929 | * 1. unsaved values |
||
| 930 | * 2. local values |
||
| 931 | * 3. default value |
||
| 932 | * 4. null |
||
| 933 | * |
||
| 934 | * @param string $property |
||
| 935 | * @param array $values |
||
| 936 | * |
||
| 937 | * @return mixed |
||
| 938 | */ |
||
| 939 | protected function getValue($property, array $values) |
||
| 956 | |||
| 957 | /** |
||
| 958 | * Populates a newly created model with its ID. |
||
| 959 | */ |
||
| 960 | protected function getNewID() |
||
| 980 | |||
| 981 | /** |
||
| 982 | * Sets a collection values on the model from an untrusted input. |
||
| 983 | * |
||
| 984 | * @param array $values |
||
| 985 | * |
||
| 986 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 987 | * |
||
| 988 | * @return $this |
||
| 989 | */ |
||
| 990 | public function setValues($values) |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * Converts the model to an array. |
||
| 1013 | * |
||
| 1014 | * @return array |
||
| 1015 | */ |
||
| 1016 | public function toArray(): array |
||
| 1047 | |||
| 1048 | /** |
||
| 1049 | * Updates the model. |
||
| 1050 | * |
||
| 1051 | * @param array $data optional key-value properties to set |
||
| 1052 | * |
||
| 1053 | * @return bool true when the operation was successful |
||
| 1054 | * |
||
| 1055 | * @throws BadMethodCallException when not called on an existing model |
||
| 1056 | */ |
||
| 1057 | public function set(array $data = []): bool |
||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * Delete the model. |
||
| 1137 | * |
||
| 1138 | * @return bool true when the operation was successful |
||
| 1139 | */ |
||
| 1140 | public function delete(): bool |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Restores a soft-deleted model. |
||
| 1193 | * |
||
| 1194 | * @return bool |
||
| 1195 | */ |
||
| 1196 | public function restore(): bool |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Checks if the model has been deleted. |
||
| 1233 | * |
||
| 1234 | * @return bool |
||
| 1235 | */ |
||
| 1236 | public function isDeleted(): bool |
||
| 1244 | |||
| 1245 | ///////////////////////////// |
||
| 1246 | // Queries |
||
| 1247 | ///////////////////////////// |
||
| 1248 | |||
| 1249 | /** |
||
| 1250 | * Generates a new query instance. |
||
| 1251 | * |
||
| 1252 | * @return Query |
||
| 1253 | */ |
||
| 1254 | public static function query(): Query |
||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * Generates a new query instance that includes soft-deleted models. |
||
| 1272 | * |
||
| 1273 | * @return Query |
||
| 1274 | */ |
||
| 1275 | public static function withDeleted(): Query |
||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Finds a single instance of a model given it's ID. |
||
| 1287 | * |
||
| 1288 | * @param mixed $id |
||
| 1289 | * |
||
| 1290 | * @return static|null |
||
| 1291 | */ |
||
| 1292 | public static function find($id): ?self |
||
| 1309 | |||
| 1310 | /** |
||
| 1311 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1312 | * |
||
| 1313 | * @param mixed $id |
||
| 1314 | * |
||
| 1315 | * @return static |
||
| 1316 | * |
||
| 1317 | * @throws ModelNotFoundException when a model could not be found |
||
| 1318 | */ |
||
| 1319 | public static function findOrFail($id): self |
||
| 1328 | |||
| 1329 | /** |
||
| 1330 | * Tells if this model instance has been persisted to the data layer. |
||
| 1331 | * |
||
| 1332 | * NOTE: this does not actually perform a check with the data layer |
||
| 1333 | * |
||
| 1334 | * @return bool |
||
| 1335 | */ |
||
| 1336 | public function persisted(): bool |
||
| 1340 | |||
| 1341 | /** |
||
| 1342 | * Loads the model from the storage layer. |
||
| 1343 | * |
||
| 1344 | * @return $this |
||
| 1345 | */ |
||
| 1346 | public function refresh() |
||
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Loads values into the model. |
||
| 1366 | * |
||
| 1367 | * @param array $values values |
||
| 1368 | * |
||
| 1369 | * @return $this |
||
| 1370 | */ |
||
| 1371 | public function refreshWith(array $values) |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Clears the cache for this model. |
||
| 1389 | * |
||
| 1390 | * @return $this |
||
| 1391 | */ |
||
| 1392 | public function clearCache() |
||
| 1401 | |||
| 1402 | ///////////////////////////// |
||
| 1403 | // Relationships |
||
| 1404 | ///////////////////////////// |
||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * @deprecated |
||
| 1408 | * |
||
| 1409 | * Gets the model(s) for a relationship |
||
| 1410 | * |
||
| 1411 | * @param string $k property |
||
| 1412 | * |
||
| 1413 | * @throws \InvalidArgumentException when the relationship manager cannot be created |
||
| 1414 | * |
||
| 1415 | * @return Model|array|null |
||
| 1416 | */ |
||
| 1417 | public function relation(string $k) |
||
| 1426 | |||
| 1427 | /** |
||
| 1428 | * @deprecated |
||
| 1429 | * |
||
| 1430 | * Sets the model for a one-to-one relationship (has-one or belongs-to) |
||
| 1431 | * |
||
| 1432 | * @param string $k |
||
| 1433 | * @param Model $model |
||
| 1434 | * |
||
| 1435 | * @return $this |
||
| 1436 | */ |
||
| 1437 | public function setRelation(string $k, Model $model) |
||
| 1444 | |||
| 1445 | /** |
||
| 1446 | * @deprecated |
||
| 1447 | * |
||
| 1448 | * Sets the model for a one-to-many relationship |
||
| 1449 | * |
||
| 1450 | * @param string $k |
||
| 1451 | * @param iterable $models |
||
| 1452 | * |
||
| 1453 | * @return $this |
||
| 1454 | */ |
||
| 1455 | public function setRelationCollection(string $k, iterable $models) |
||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Sets the model for a one-to-one relationship (has-one or belongs-to) as null. |
||
| 1464 | * |
||
| 1465 | * @param string $k |
||
| 1466 | * |
||
| 1467 | * @return $this |
||
| 1468 | */ |
||
| 1469 | public function clearRelation(string $k) |
||
| 1476 | |||
| 1477 | /** |
||
| 1478 | * Builds a relationship manager object for a given property. |
||
| 1479 | * |
||
| 1480 | * @param array $k |
||
| 1481 | * |
||
| 1482 | * @throws \InvalidArgumentException when the relationship manager cannot be created |
||
| 1483 | * |
||
| 1484 | * @return Relation |
||
| 1485 | */ |
||
| 1486 | public function getRelationshipManager(string $k): Relation |
||
| 1517 | |||
| 1518 | /** |
||
| 1519 | * Creates the parent side of a One-To-One relationship. |
||
| 1520 | * |
||
| 1521 | * @param string $model foreign model class |
||
| 1522 | * @param string $foreignKey identifying key on foreign model |
||
| 1523 | * @param string $localKey identifying key on local model |
||
| 1524 | * |
||
| 1525 | * @return HasOne |
||
| 1526 | */ |
||
| 1527 | public function hasOne($model, $foreignKey = '', $localKey = ''): HasOne |
||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
| 1534 | * |
||
| 1535 | * @param string $model foreign model class |
||
| 1536 | * @param string $foreignKey identifying key on foreign model |
||
| 1537 | * @param string $localKey identifying key on local model |
||
| 1538 | * |
||
| 1539 | * @return BelongsTo |
||
| 1540 | */ |
||
| 1541 | public function belongsTo($model, $foreignKey = '', $localKey = ''): BelongsTo |
||
| 1545 | |||
| 1546 | /** |
||
| 1547 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
| 1548 | * |
||
| 1549 | * @param string $model foreign model class |
||
| 1550 | * @param string $foreignKey identifying key on foreign model |
||
| 1551 | * @param string $localKey identifying key on local model |
||
| 1552 | * |
||
| 1553 | * @return HasMany |
||
| 1554 | */ |
||
| 1555 | public function hasMany($model, $foreignKey = '', $localKey = ''): HasMany |
||
| 1559 | |||
| 1560 | /** |
||
| 1561 | * Creates the child side of a Many-To-Many relationship. |
||
| 1562 | * |
||
| 1563 | * @param string $model foreign model class |
||
| 1564 | * @param string $tablename pivot table name |
||
| 1565 | * @param string $foreignKey identifying key on foreign model |
||
| 1566 | * @param string $localKey identifying key on local model |
||
| 1567 | * |
||
| 1568 | * @return BelongsToMany |
||
| 1569 | */ |
||
| 1570 | public function belongsToMany($model, $tablename = '', $foreignKey = '', $localKey = ''): BelongsToMany |
||
| 1574 | |||
| 1575 | ///////////////////////////// |
||
| 1576 | // Events |
||
| 1577 | ///////////////////////////// |
||
| 1578 | |||
| 1579 | /** |
||
| 1580 | * Gets the event dispatcher. |
||
| 1581 | * |
||
| 1582 | * @return EventDispatcher |
||
| 1583 | */ |
||
| 1584 | public static function getDispatcher($ignoreCache = false): EventDispatcher |
||
| 1593 | |||
| 1594 | /** |
||
| 1595 | * Subscribes to a listener to an event. |
||
| 1596 | * |
||
| 1597 | * @param string $event event name |
||
| 1598 | * @param callable $listener |
||
| 1599 | * @param int $priority optional priority, higher #s get called first |
||
| 1600 | */ |
||
| 1601 | public static function listen(string $event, callable $listener, int $priority = 0) |
||
| 1605 | |||
| 1606 | /** |
||
| 1607 | * Adds a listener to the model.creating and model.updating events. |
||
| 1608 | * |
||
| 1609 | * @param callable $listener |
||
| 1610 | * @param int $priority |
||
| 1611 | */ |
||
| 1612 | public static function saving(callable $listener, int $priority = 0) |
||
| 1617 | |||
| 1618 | /** |
||
| 1619 | * Adds a listener to the model.created and model.updated events. |
||
| 1620 | * |
||
| 1621 | * @param callable $listener |
||
| 1622 | * @param int $priority |
||
| 1623 | */ |
||
| 1624 | public static function saved(callable $listener, int $priority = 0) |
||
| 1629 | |||
| 1630 | /** |
||
| 1631 | * Adds a listener to the model.creating event. |
||
| 1632 | * |
||
| 1633 | * @param callable $listener |
||
| 1634 | * @param int $priority |
||
| 1635 | */ |
||
| 1636 | public static function creating(callable $listener, int $priority = 0) |
||
| 1640 | |||
| 1641 | /** |
||
| 1642 | * Adds a listener to the model.created event. |
||
| 1643 | * |
||
| 1644 | * @param callable $listener |
||
| 1645 | * @param int $priority |
||
| 1646 | */ |
||
| 1647 | public static function created(callable $listener, int $priority = 0) |
||
| 1651 | |||
| 1652 | /** |
||
| 1653 | * Adds a listener to the model.updating event. |
||
| 1654 | * |
||
| 1655 | * @param callable $listener |
||
| 1656 | * @param int $priority |
||
| 1657 | */ |
||
| 1658 | public static function updating(callable $listener, int $priority = 0) |
||
| 1662 | |||
| 1663 | /** |
||
| 1664 | * Adds a listener to the model.updated event. |
||
| 1665 | * |
||
| 1666 | * @param callable $listener |
||
| 1667 | * @param int $priority |
||
| 1668 | */ |
||
| 1669 | public static function updated(callable $listener, int $priority = 0) |
||
| 1673 | |||
| 1674 | /** |
||
| 1675 | * Adds a listener to the model.deleting event. |
||
| 1676 | * |
||
| 1677 | * @param callable $listener |
||
| 1678 | * @param int $priority |
||
| 1679 | */ |
||
| 1680 | public static function deleting(callable $listener, int $priority = 0) |
||
| 1684 | |||
| 1685 | /** |
||
| 1686 | * Adds a listener to the model.deleted event. |
||
| 1687 | * |
||
| 1688 | * @param callable $listener |
||
| 1689 | * @param int $priority |
||
| 1690 | */ |
||
| 1691 | public static function deleted(callable $listener, int $priority = 0) |
||
| 1695 | |||
| 1696 | /** |
||
| 1697 | * Dispatches the given event and checks if it was successful. |
||
| 1698 | * |
||
| 1699 | * @return bool true if the events were successfully propagated |
||
| 1700 | */ |
||
| 1701 | private function performDispatch(string $eventName, bool $usesTransactions): bool |
||
| 1717 | |||
| 1718 | ///////////////////////////// |
||
| 1719 | // Validation |
||
| 1720 | ///////////////////////////// |
||
| 1721 | |||
| 1722 | /** |
||
| 1723 | * Gets the error stack for this model. |
||
| 1724 | * |
||
| 1725 | * @return Errors |
||
| 1726 | */ |
||
| 1727 | public function getErrors(): Errors |
||
| 1735 | |||
| 1736 | /** |
||
| 1737 | * Checks if the model in its current state is valid. |
||
| 1738 | * |
||
| 1739 | * @return bool |
||
| 1740 | */ |
||
| 1741 | public function valid(): bool |
||
| 1762 | |||
| 1763 | /** |
||
| 1764 | * Validates and marshals a value to storage. |
||
| 1765 | * |
||
| 1766 | * @param array $property property definition |
||
| 1767 | * @param string $name property name |
||
| 1768 | * @param mixed $value |
||
| 1769 | * |
||
| 1770 | * @return bool |
||
| 1771 | */ |
||
| 1772 | private function filterAndValidate(array $property, string $name, &$value): bool |
||
| 1792 | |||
| 1793 | /** |
||
| 1794 | * Validates a value for a property. |
||
| 1795 | * |
||
| 1796 | * @param array $property property definition |
||
| 1797 | * @param string $name property name |
||
| 1798 | * @param mixed $value |
||
| 1799 | * |
||
| 1800 | * @return array |
||
| 1801 | */ |
||
| 1802 | private function validateValue(array $property, string $name, $value): array |
||
| 1825 | |||
| 1826 | /** |
||
| 1827 | * Checks if a value is unique for a property. |
||
| 1828 | * |
||
| 1829 | * @param string $name property name |
||
| 1830 | * @param mixed $value |
||
| 1831 | * |
||
| 1832 | * @return bool |
||
| 1833 | */ |
||
| 1834 | private function checkUniqueness(string $name, $value): bool |
||
| 1849 | |||
| 1850 | /** |
||
| 1851 | * Gets the marshaled default value for a property (if set). |
||
| 1852 | * |
||
| 1853 | * @param array $property |
||
| 1854 | * |
||
| 1855 | * @return mixed |
||
| 1856 | */ |
||
| 1857 | private function getPropertyDefault(array $property) |
||
| 1861 | |||
| 1862 | /** |
||
| 1863 | * Gets the humanized name of a property. |
||
| 1864 | * |
||
| 1865 | * @param string $name property name |
||
| 1866 | * |
||
| 1867 | * @return string |
||
| 1868 | */ |
||
| 1869 | private function getPropertyTitle(string $name): string |
||
| 1882 | } |
||
| 1883 |
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.