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 |
||
| 47 | abstract class Model implements ArrayAccess |
||
| 48 | { |
||
| 49 | const IMMUTABLE = 0; |
||
| 50 | const MUTABLE_CREATE_ONLY = 1; |
||
| 51 | const MUTABLE = 2; |
||
| 52 | |||
| 53 | const TYPE_STRING = 'string'; |
||
| 54 | const TYPE_INTEGER = 'integer'; |
||
| 55 | const TYPE_FLOAT = 'float'; |
||
| 56 | const TYPE_BOOLEAN = 'boolean'; |
||
| 57 | const TYPE_DATE = 'date'; |
||
| 58 | const TYPE_OBJECT = 'object'; |
||
| 59 | const TYPE_ARRAY = 'array'; |
||
| 60 | |||
| 61 | const RELATIONSHIP_HAS_ONE = 'has_one'; |
||
| 62 | const RELATIONSHIP_HAS_MANY = 'has_many'; |
||
| 63 | const RELATIONSHIP_BELONGS_TO = 'belongs_to'; |
||
| 64 | const RELATIONSHIP_BELONGS_TO_MANY = 'belongs_to_many'; |
||
| 65 | |||
| 66 | const DEFAULT_ID_PROPERTY = 'id'; |
||
| 67 | |||
| 68 | ///////////////////////////// |
||
| 69 | // Model visible variables |
||
| 70 | ///////////////////////////// |
||
| 71 | |||
| 72 | /** |
||
| 73 | * List of model ID property names. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected static $ids = [self::DEFAULT_ID_PROPERTY]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Property definitions expressed as a key-value map with |
||
| 81 | * property names as the keys. |
||
| 82 | * i.e. ['enabled' => ['type' => Model::TYPE_BOOLEAN]]. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected static $properties = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private static $dispatchers; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | private $hasId; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | private $idValues; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | protected $_values = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $_unsaved = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | protected $_persisted = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | protected $_relationships = []; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | private $loaded = false; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var Errors |
||
| 130 | */ |
||
| 131 | private $errors; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var bool |
||
| 135 | */ |
||
| 136 | private $ignoreUnsaved; |
||
| 137 | |||
| 138 | ///////////////////////////// |
||
| 139 | // Base model variables |
||
| 140 | ///////////////////////////// |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var array |
||
| 144 | */ |
||
| 145 | private static $propertyDefinitionBase = [ |
||
| 146 | 'type' => null, |
||
| 147 | 'mutable' => self::MUTABLE, |
||
| 148 | 'null' => false, |
||
| 149 | 'unique' => false, |
||
| 150 | 'required' => false, |
||
| 151 | ]; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var array |
||
| 155 | */ |
||
| 156 | private static $defaultIDProperty = [ |
||
| 157 | 'type' => self::TYPE_INTEGER, |
||
| 158 | 'mutable' => self::IMMUTABLE, |
||
| 159 | ]; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var array |
||
| 163 | */ |
||
| 164 | private static $timestampProperties = [ |
||
| 165 | 'created_at' => [ |
||
| 166 | 'type' => self::TYPE_DATE, |
||
| 167 | 'validate' => 'timestamp|db_timestamp', |
||
| 168 | ], |
||
| 169 | 'updated_at' => [ |
||
| 170 | 'type' => self::TYPE_DATE, |
||
| 171 | 'validate' => 'timestamp|db_timestamp', |
||
| 172 | ], |
||
| 173 | ]; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var array |
||
| 177 | */ |
||
| 178 | private static $softDeleteProperties = [ |
||
| 179 | 'deleted_at' => [ |
||
| 180 | 'type' => self::TYPE_DATE, |
||
| 181 | 'validate' => 'timestamp|db_timestamp', |
||
| 182 | 'null' => true, |
||
| 183 | ], |
||
| 184 | ]; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @var array |
||
| 188 | */ |
||
| 189 | private static $initialized = []; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @var DriverInterface |
||
| 193 | */ |
||
| 194 | private static $driver; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @var array |
||
| 198 | */ |
||
| 199 | private static $accessors = []; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @var array |
||
| 203 | */ |
||
| 204 | private static $mutators = []; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Creates a new model object. |
||
| 208 | * |
||
| 209 | * @param array|string|Model|false $id ordered array of ids or comma-separated id string |
||
| 210 | * @param array $values optional key-value map to pre-seed model |
||
| 211 | */ |
||
| 212 | public function __construct($id = false, array $values = []) |
||
| 213 | { |
||
| 214 | // initialize the model |
||
| 215 | $this->init(); |
||
| 216 | |||
| 217 | // parse the supplied model ID |
||
| 218 | $this->parseId($id); |
||
| 219 | |||
| 220 | // load any given values |
||
| 221 | if (count($values) > 0) { |
||
| 222 | $this->refreshWith($values); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Performs initialization on this model. |
||
| 228 | */ |
||
| 229 | private function init() |
||
| 230 | { |
||
| 231 | // ensure the initialize function is called only once |
||
| 232 | $k = get_called_class(); |
||
| 233 | if (!isset(self::$initialized[$k])) { |
||
| 234 | $this->initialize(); |
||
| 235 | self::$initialized[$k] = true; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * The initialize() method is called once per model. It's used |
||
| 241 | * to perform any one-off tasks before the model gets |
||
| 242 | * constructed. This is a great place to add any model |
||
| 243 | * properties. When extending this method be sure to call |
||
| 244 | * parent::initialize() as some important stuff happens here. |
||
| 245 | * If extending this method to add properties then you should |
||
| 246 | * call parent::initialize() after adding any properties. |
||
| 247 | */ |
||
| 248 | protected function initialize() |
||
| 249 | { |
||
| 250 | // load the driver |
||
| 251 | static::getDriver(); |
||
| 252 | |||
| 253 | // add in the default ID property |
||
| 254 | if (static::$ids == [self::DEFAULT_ID_PROPERTY] && !isset(static::$properties[self::DEFAULT_ID_PROPERTY])) { |
||
| 255 | static::$properties[self::DEFAULT_ID_PROPERTY] = self::$defaultIDProperty; |
||
| 256 | } |
||
| 257 | |||
| 258 | // generates created_at and updated_at timestamps |
||
| 259 | if (property_exists($this, 'autoTimestamps')) { |
||
| 260 | $this->installAutoTimestamps(); |
||
| 261 | } |
||
| 262 | |||
| 263 | // generates deleted_at timestamps |
||
| 264 | if (property_exists($this, 'softDelete')) { |
||
| 265 | $this->installSoftDelete(); |
||
| 266 | } |
||
| 267 | |||
| 268 | // fill in each property by extending the property |
||
| 269 | // definition base |
||
| 270 | foreach (static::$properties as $k => &$property) { |
||
| 271 | $property = array_replace(self::$propertyDefinitionBase, $property); |
||
| 272 | |||
| 273 | // populate relationship property settings |
||
| 274 | if (isset($property['relation'])) { |
||
| 275 | // this is added for BC with older versions of pulsar |
||
| 276 | // that only supported belongs to relationships |
||
| 277 | if (!isset($property['relation_type'])) { |
||
| 278 | $property['relation_type'] = self::RELATIONSHIP_BELONGS_TO; |
||
| 279 | $property['local_key'] = $k; |
||
| 280 | } |
||
| 281 | |||
| 282 | $relation = $this->getRelationshipManager($k); |
||
| 283 | if (!isset($property['foreign_key'])) { |
||
| 284 | $property['foreign_key'] = $relation->getForeignKey(); |
||
| 285 | } |
||
| 286 | |||
| 287 | if (!isset($property['local_key'])) { |
||
| 288 | $property['local_key'] = $relation->getLocalKey(); |
||
| 289 | } |
||
| 290 | |||
| 291 | if (!isset($property['pivot_tablename']) && $relation instanceof BelongsToMany) { |
||
| 292 | $property['pivot_tablename'] = $relation->getTablename(); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | // order the properties array by name for consistency |
||
| 298 | // since it is constructed in a random order |
||
| 299 | ksort(static::$properties); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Installs the `created_at` and `updated_at` properties. |
||
| 304 | */ |
||
| 305 | private function installAutoTimestamps() |
||
| 306 | { |
||
| 307 | static::$properties = array_replace(self::$timestampProperties, static::$properties); |
||
| 308 | |||
| 309 | self::creating(function (ModelEvent $event) { |
||
| 310 | $model = $event->getModel(); |
||
| 311 | $model->created_at = time(); |
||
|
|
|||
| 312 | $model->updated_at = time(); |
||
| 313 | }); |
||
| 314 | |||
| 315 | self::updating(function (ModelEvent $event) { |
||
| 316 | $event->getModel()->updated_at = time(); |
||
| 317 | }); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Installs the `deleted_at` properties. |
||
| 322 | */ |
||
| 323 | private function installSoftDelete() |
||
| 324 | { |
||
| 325 | static::$properties = array_replace(self::$softDeleteProperties, static::$properties); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Parses the given ID, which can be a single or composite primary key. |
||
| 330 | * |
||
| 331 | * @param mixed $id |
||
| 332 | */ |
||
| 333 | private function parseId($id) |
||
| 334 | { |
||
| 335 | if (is_array($id)) { |
||
| 336 | // A model can be supplied as a primary key |
||
| 337 | foreach ($id as &$el) { |
||
| 338 | if ($el instanceof self) { |
||
| 339 | $el = $el->id(); |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | // The IDs come in as the same order as ::$ids. |
||
| 344 | // We need to match up the elements on that |
||
| 345 | // input into a key-value map for each ID property. |
||
| 346 | $ids = []; |
||
| 347 | $idQueue = array_reverse($id); |
||
| 348 | $this->hasId = true; |
||
| 349 | foreach (static::$ids as $k => $f) { |
||
| 350 | // type cast |
||
| 351 | if (count($idQueue) > 0) { |
||
| 352 | $idProperty = static::getProperty($f); |
||
| 353 | $ids[$f] = Type::cast($idProperty, array_pop($idQueue)); |
||
| 354 | } else { |
||
| 355 | $ids[$f] = false; |
||
| 356 | $this->hasId = false; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | $this->idValues = $ids; |
||
| 361 | } elseif ($id instanceof self) { |
||
| 362 | // A model can be supplied as a primary key |
||
| 363 | $this->hasId = $id->hasId; |
||
| 364 | $this->idValues = $id->ids(); |
||
| 365 | } else { |
||
| 366 | // type cast the single primary key |
||
| 367 | $idName = static::$ids[0]; |
||
| 368 | $this->hasId = false; |
||
| 369 | if (false !== $id) { |
||
| 370 | $idProperty = static::getProperty($idName); |
||
| 371 | $id = Type::cast($idProperty, $id); |
||
| 372 | $this->hasId = true; |
||
| 373 | } |
||
| 374 | |||
| 375 | $this->idValues = [$idName => $id]; |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Sets the driver for all models. |
||
| 381 | */ |
||
| 382 | public static function setDriver(DriverInterface $driver) |
||
| 383 | { |
||
| 384 | self::$driver = $driver; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Gets the driver for all models. |
||
| 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 | public static function modelName(): string |
||
| 413 | { |
||
| 414 | // strip namespacing |
||
| 415 | $paths = explode('\\', get_called_class()); |
||
| 416 | |||
| 417 | return end($paths); |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Gets the model ID. |
||
| 422 | * |
||
| 423 | * @return string|number|false ID |
||
| 424 | */ |
||
| 425 | public function id() |
||
| 426 | { |
||
| 427 | if (!$this->hasId) { |
||
| 428 | return false; |
||
| 429 | } |
||
| 430 | |||
| 431 | if (1 == count($this->idValues)) { |
||
| 432 | return reset($this->idValues); |
||
| 433 | } |
||
| 434 | |||
| 435 | $result = []; |
||
| 436 | foreach (static::$ids as $k) { |
||
| 437 | $result[] = $this->idValues[$k]; |
||
| 438 | } |
||
| 439 | |||
| 440 | return implode(',', $result); |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Gets a key-value map of the model ID. |
||
| 445 | * |
||
| 446 | * @return array ID map |
||
| 447 | */ |
||
| 448 | public function ids(): array |
||
| 452 | |||
| 453 | ///////////////////////////// |
||
| 454 | // Magic Methods |
||
| 455 | ///////////////////////////// |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Converts the model into a string. |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function __toString() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Shortcut to a get() call for a given property. |
||
| 472 | * |
||
| 473 | * @param string $name |
||
| 474 | * |
||
| 475 | * @return mixed |
||
| 476 | */ |
||
| 477 | public function __get($name) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Sets an unsaved value. |
||
| 486 | * |
||
| 487 | * @param string $name |
||
| 488 | * @param mixed $value |
||
| 489 | */ |
||
| 490 | public function __set($name, $value) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Checks if an unsaved value or property exists by this name. |
||
| 508 | * |
||
| 509 | * @param string $name |
||
| 510 | * |
||
| 511 | * @return bool |
||
| 512 | */ |
||
| 513 | public function __isset($name) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Unsets an unsaved value. |
||
| 520 | * |
||
| 521 | * @param string $name |
||
| 522 | */ |
||
| 523 | public function __unset($name) |
||
| 534 | |||
| 535 | ///////////////////////////// |
||
| 536 | // ArrayAccess Interface |
||
| 537 | ///////////////////////////// |
||
| 538 | |||
| 539 | public function offsetExists($offset) |
||
| 543 | |||
| 544 | public function offsetGet($offset) |
||
| 548 | |||
| 549 | public function offsetSet($offset, $value) |
||
| 553 | |||
| 554 | public function offsetUnset($offset) |
||
| 558 | |||
| 559 | public static function __callStatic($name, $parameters) |
||
| 566 | |||
| 567 | ///////////////////////////// |
||
| 568 | // Property Definitions |
||
| 569 | ///////////////////////////// |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Gets the definition of all model properties. |
||
| 573 | */ |
||
| 574 | public static function getProperties(): Definition |
||
| 575 | { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Gets the definition of a specific property. |
||
| 586 | * |
||
| 587 | * @param string $property property to lookup |
||
| 588 | */ |
||
| 589 | public static function getProperty(string $property): ?Property |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Gets the names of the model ID properties. |
||
| 596 | */ |
||
| 597 | public static function getIDProperties(): array |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Checks if the model has a property. |
||
| 604 | * |
||
| 605 | * @param string $property property |
||
| 606 | * |
||
| 607 | * @return bool has property |
||
| 608 | */ |
||
| 609 | public static function hasProperty(string $property): bool |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Gets the mutator method name for a given proeprty name. |
||
| 616 | * Looks for methods in the form of `setPropertyValue`. |
||
| 617 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 618 | * |
||
| 619 | * @param string $property property |
||
| 620 | * |
||
| 621 | * @return string|null method name if it exists |
||
| 622 | */ |
||
| 623 | public static function getMutator(string $property): ?string |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Gets the accessor method name for a given proeprty name. |
||
| 644 | * Looks for methods in the form of `getPropertyValue`. |
||
| 645 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 646 | * |
||
| 647 | * @param string $property property |
||
| 648 | * |
||
| 649 | * @return string|null method name if it exists |
||
| 650 | */ |
||
| 651 | public static function getAccessor(string $property): ?string |
||
| 669 | |||
| 670 | ///////////////////////////// |
||
| 671 | // CRUD Operations |
||
| 672 | ///////////////////////////// |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Gets the tablename for storing this model. |
||
| 676 | */ |
||
| 677 | public function getTablename(): string |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Gets the ID of the connection in the connection manager |
||
| 686 | * that stores this model. |
||
| 687 | */ |
||
| 688 | public function getConnection(): ?string |
||
| 692 | |||
| 693 | protected function usesTransactions(): bool |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Saves the model. |
||
| 700 | * |
||
| 701 | * @return bool true when the operation was successful |
||
| 702 | */ |
||
| 703 | public function save(): bool |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Saves the model. Throws an exception when the operation fails. |
||
| 714 | * |
||
| 715 | * @throws ModelException when the model cannot be saved |
||
| 716 | */ |
||
| 717 | public function saveOrFail() |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Creates a new model. |
||
| 731 | * |
||
| 732 | * @param array $data optional key-value properties to set |
||
| 733 | * |
||
| 734 | * @return bool true when the operation was successful |
||
| 735 | * |
||
| 736 | * @throws BadMethodCallException when called on an existing model |
||
| 737 | */ |
||
| 738 | public function create(array $data = []): bool |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Ignores unsaved values when fetching the next value. |
||
| 843 | * |
||
| 844 | * @return $this |
||
| 845 | */ |
||
| 846 | public function ignoreUnsaved() |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Fetches property values from the model. |
||
| 855 | * |
||
| 856 | * This method looks up values in this order: |
||
| 857 | * IDs, local cache, unsaved values, storage layer, defaults |
||
| 858 | * |
||
| 859 | * @param array $properties list of property names to fetch values of |
||
| 860 | */ |
||
| 861 | public function get(array $properties): array |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Gets a property value from the model. |
||
| 902 | * |
||
| 903 | * Values are looked up in this order: |
||
| 904 | * 1. unsaved values |
||
| 905 | * 2. local values |
||
| 906 | * 3. default value |
||
| 907 | * 4. null |
||
| 908 | * |
||
| 909 | * @param string $property |
||
| 910 | * |
||
| 911 | * @return mixed |
||
| 912 | */ |
||
| 913 | protected function getValue($property, array $values) |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Populates a newly created model with its ID. |
||
| 933 | */ |
||
| 934 | protected function getNewID() |
||
| 954 | |||
| 955 | /** |
||
| 956 | * Sets a collection values on the model from an untrusted input. |
||
| 957 | * |
||
| 958 | * @param array $values |
||
| 959 | * |
||
| 960 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 961 | * |
||
| 962 | * @return $this |
||
| 963 | */ |
||
| 964 | public function setValues($values) |
||
| 984 | |||
| 985 | /** |
||
| 986 | * Converts the model to an array. |
||
| 987 | */ |
||
| 988 | public function toArray(): array |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Updates the model. |
||
| 1016 | * |
||
| 1017 | * @param array $data optional key-value properties to set |
||
| 1018 | * |
||
| 1019 | * @return bool true when the operation was successful |
||
| 1020 | * |
||
| 1021 | * @throws BadMethodCallException when not called on an existing model |
||
| 1022 | */ |
||
| 1023 | public function set(array $data = []): bool |
||
| 1100 | |||
| 1101 | /** |
||
| 1102 | * Delete the model. |
||
| 1103 | * |
||
| 1104 | * @return bool true when the operation was successful |
||
| 1105 | */ |
||
| 1106 | public function delete(): bool |
||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Restores a soft-deleted model. |
||
| 1159 | */ |
||
| 1160 | public function restore(): bool |
||
| 1194 | |||
| 1195 | /** |
||
| 1196 | * Checks if the model has been deleted. |
||
| 1197 | */ |
||
| 1198 | public function isDeleted(): bool |
||
| 1206 | |||
| 1207 | ///////////////////////////// |
||
| 1208 | // Queries |
||
| 1209 | ///////////////////////////// |
||
| 1210 | |||
| 1211 | /** |
||
| 1212 | * Generates a new query instance. |
||
| 1213 | */ |
||
| 1214 | public static function query(): Query |
||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * Generates a new query instance that includes soft-deleted models. |
||
| 1232 | */ |
||
| 1233 | public static function withDeleted(): Query |
||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Finds a single instance of a model given it's ID. |
||
| 1245 | * |
||
| 1246 | * @param mixed $id |
||
| 1247 | * |
||
| 1248 | * @return static|null |
||
| 1249 | */ |
||
| 1250 | public static function find($id): ?self |
||
| 1267 | |||
| 1268 | /** |
||
| 1269 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1270 | * |
||
| 1271 | * @param mixed $id |
||
| 1272 | * |
||
| 1273 | * @return static |
||
| 1274 | * |
||
| 1275 | * @throws ModelNotFoundException when a model could not be found |
||
| 1276 | */ |
||
| 1277 | public static function findOrFail($id): self |
||
| 1286 | |||
| 1287 | /** |
||
| 1288 | * Tells if this model instance has been persisted to the data layer. |
||
| 1289 | * |
||
| 1290 | * NOTE: this does not actually perform a check with the data layer |
||
| 1291 | */ |
||
| 1292 | public function persisted(): bool |
||
| 1296 | |||
| 1297 | /** |
||
| 1298 | * Loads the model from the storage layer. |
||
| 1299 | * |
||
| 1300 | * @return $this |
||
| 1301 | */ |
||
| 1302 | public function refresh() |
||
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Loads values into the model. |
||
| 1322 | * |
||
| 1323 | * @param array $values values |
||
| 1324 | * |
||
| 1325 | * @return $this |
||
| 1326 | */ |
||
| 1327 | public function refreshWith(array $values) |
||
| 1342 | |||
| 1343 | /** |
||
| 1344 | * Clears the cache for this model. |
||
| 1345 | * |
||
| 1346 | * @return $this |
||
| 1347 | */ |
||
| 1348 | public function clearCache() |
||
| 1357 | |||
| 1358 | ///////////////////////////// |
||
| 1359 | // Relationships |
||
| 1360 | ///////////////////////////// |
||
| 1361 | |||
| 1362 | /** |
||
| 1363 | * @deprecated |
||
| 1364 | * |
||
| 1365 | * Gets the model(s) for a relationship |
||
| 1366 | * |
||
| 1367 | * @param string $k property |
||
| 1368 | * |
||
| 1369 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
| 1370 | * |
||
| 1371 | * @return Model|array|null |
||
| 1372 | */ |
||
| 1373 | public function relation(string $k) |
||
| 1382 | |||
| 1383 | /** |
||
| 1384 | * @deprecated |
||
| 1385 | * |
||
| 1386 | * Sets the model for a one-to-one relationship (has-one or belongs-to) |
||
| 1387 | * |
||
| 1388 | * @return $this |
||
| 1389 | */ |
||
| 1390 | public function setRelation(string $k, Model $model) |
||
| 1397 | |||
| 1398 | /** |
||
| 1399 | * @deprecated |
||
| 1400 | * |
||
| 1401 | * Sets the model for a one-to-many relationship |
||
| 1402 | * |
||
| 1403 | * @return $this |
||
| 1404 | */ |
||
| 1405 | public function setRelationCollection(string $k, iterable $models) |
||
| 1411 | |||
| 1412 | /** |
||
| 1413 | * Sets the model for a one-to-one relationship (has-one or belongs-to) as null. |
||
| 1414 | * |
||
| 1415 | * @return $this |
||
| 1416 | */ |
||
| 1417 | public function clearRelation(string $k) |
||
| 1424 | |||
| 1425 | /** |
||
| 1426 | * Builds a relationship manager object for a given property. |
||
| 1427 | * |
||
| 1428 | * @param array $k |
||
| 1429 | * |
||
| 1430 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
| 1431 | */ |
||
| 1432 | public function getRelationshipManager(string $k): Relation |
||
| 1468 | |||
| 1469 | /** |
||
| 1470 | * Creates the parent side of a One-To-One relationship. |
||
| 1471 | * |
||
| 1472 | * @param string $model foreign model class |
||
| 1473 | * @param string $foreignKey identifying key on foreign model |
||
| 1474 | * @param string $localKey identifying key on local model |
||
| 1475 | */ |
||
| 1476 | public function hasOne($model, $foreignKey = '', $localKey = ''): HasOne |
||
| 1480 | |||
| 1481 | /** |
||
| 1482 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
| 1483 | * |
||
| 1484 | * @param string $model foreign model class |
||
| 1485 | * @param string $foreignKey identifying key on foreign model |
||
| 1486 | * @param string $localKey identifying key on local model |
||
| 1487 | */ |
||
| 1488 | public function belongsTo($model, $foreignKey = '', $localKey = ''): BelongsTo |
||
| 1492 | |||
| 1493 | /** |
||
| 1494 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
| 1495 | * |
||
| 1496 | * @param string $model foreign model class |
||
| 1497 | * @param string $foreignKey identifying key on foreign model |
||
| 1498 | * @param string $localKey identifying key on local model |
||
| 1499 | */ |
||
| 1500 | public function hasMany($model, $foreignKey = '', $localKey = ''): HasMany |
||
| 1504 | |||
| 1505 | /** |
||
| 1506 | * Creates the child side of a Many-To-Many relationship. |
||
| 1507 | * |
||
| 1508 | * @param string $model foreign model class |
||
| 1509 | * @param string $tablename pivot table name |
||
| 1510 | * @param string $foreignKey identifying key on foreign model |
||
| 1511 | * @param string $localKey identifying key on local model |
||
| 1512 | */ |
||
| 1513 | public function belongsToMany($model, $tablename = '', $foreignKey = '', $localKey = ''): BelongsToMany |
||
| 1517 | |||
| 1518 | ///////////////////////////// |
||
| 1519 | // Events |
||
| 1520 | ///////////////////////////// |
||
| 1521 | |||
| 1522 | /** |
||
| 1523 | * Gets the event dispatcher. |
||
| 1524 | */ |
||
| 1525 | public static function getDispatcher($ignoreCache = false): EventDispatcher |
||
| 1534 | |||
| 1535 | /** |
||
| 1536 | * Subscribes to a listener to an event. |
||
| 1537 | * |
||
| 1538 | * @param string $event event name |
||
| 1539 | * @param int $priority optional priority, higher #s get called first |
||
| 1540 | */ |
||
| 1541 | public static function listen(string $event, callable $listener, int $priority = 0) |
||
| 1545 | |||
| 1546 | /** |
||
| 1547 | * Adds a listener to the model.creating and model.updating events. |
||
| 1548 | */ |
||
| 1549 | public static function saving(callable $listener, int $priority = 0) |
||
| 1554 | |||
| 1555 | /** |
||
| 1556 | * Adds a listener to the model.created and model.updated events. |
||
| 1557 | */ |
||
| 1558 | public static function saved(callable $listener, int $priority = 0) |
||
| 1563 | |||
| 1564 | /** |
||
| 1565 | * Adds a listener to the model.creating event. |
||
| 1566 | */ |
||
| 1567 | public static function creating(callable $listener, int $priority = 0) |
||
| 1571 | |||
| 1572 | /** |
||
| 1573 | * Adds a listener to the model.created event. |
||
| 1574 | */ |
||
| 1575 | public static function created(callable $listener, int $priority = 0) |
||
| 1579 | |||
| 1580 | /** |
||
| 1581 | * Adds a listener to the model.updating event. |
||
| 1582 | */ |
||
| 1583 | public static function updating(callable $listener, int $priority = 0) |
||
| 1587 | |||
| 1588 | /** |
||
| 1589 | * Adds a listener to the model.updated event. |
||
| 1590 | */ |
||
| 1591 | public static function updated(callable $listener, int $priority = 0) |
||
| 1595 | |||
| 1596 | /** |
||
| 1597 | * Adds a listener to the model.deleting event. |
||
| 1598 | */ |
||
| 1599 | public static function deleting(callable $listener, int $priority = 0) |
||
| 1603 | |||
| 1604 | /** |
||
| 1605 | * Adds a listener to the model.deleted event. |
||
| 1606 | */ |
||
| 1607 | public static function deleted(callable $listener, int $priority = 0) |
||
| 1611 | |||
| 1612 | /** |
||
| 1613 | * Dispatches the given event and checks if it was successful. |
||
| 1614 | * |
||
| 1615 | * @return bool true if the events were successfully propagated |
||
| 1616 | */ |
||
| 1617 | private function performDispatch(string $eventName, bool $usesTransactions): bool |
||
| 1633 | |||
| 1634 | ///////////////////////////// |
||
| 1635 | // Validation |
||
| 1636 | ///////////////////////////// |
||
| 1637 | |||
| 1638 | /** |
||
| 1639 | * Gets the error stack for this model. |
||
| 1640 | */ |
||
| 1641 | public function getErrors(): Errors |
||
| 1649 | |||
| 1650 | /** |
||
| 1651 | * Checks if the model in its current state is valid. |
||
| 1652 | */ |
||
| 1653 | public function valid(): bool |
||
| 1674 | |||
| 1675 | /** |
||
| 1676 | * Validates and marshals a value to storage. |
||
| 1677 | * |
||
| 1678 | * @param Property $property property definition |
||
| 1679 | * @param string $name property name |
||
| 1680 | * @param mixed $value |
||
| 1681 | */ |
||
| 1682 | private function filterAndValidate(Property $property, string $name, &$value): bool |
||
| 1702 | |||
| 1703 | /** |
||
| 1704 | * Validates a value for a property. |
||
| 1705 | * |
||
| 1706 | * @param Property $property property definition |
||
| 1707 | * @param string $name property name |
||
| 1708 | * @param mixed $value |
||
| 1709 | */ |
||
| 1710 | private function validateValue(Property $property, string $name, $value): array |
||
| 1734 | |||
| 1735 | /** |
||
| 1736 | * Checks if a value is unique for a property. |
||
| 1737 | * |
||
| 1738 | * @param string $name property name |
||
| 1739 | * @param mixed $value |
||
| 1740 | */ |
||
| 1741 | private function checkUniqueness(string $name, $value): bool |
||
| 1756 | |||
| 1757 | /** |
||
| 1758 | * Gets the humanized name of a property. |
||
| 1759 | * |
||
| 1760 | * @param string $name property name |
||
| 1761 | */ |
||
| 1762 | private function getPropertyTitle(string $name): string |
||
| 1776 | } |
||
| 1777 |
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.