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 |
||
| 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_NUMBER = 'number'; // DEPRECATED |
||
| 53 | const TYPE_INTEGER = 'integer'; |
||
| 54 | const TYPE_FLOAT = 'float'; |
||
| 55 | const TYPE_BOOLEAN = 'boolean'; |
||
| 56 | const TYPE_DATE = 'date'; |
||
| 57 | const TYPE_OBJECT = 'object'; |
||
| 58 | const TYPE_ARRAY = 'array'; |
||
| 59 | |||
| 60 | const RELATIONSHIP_HAS_ONE = 'has_one'; |
||
| 61 | const RELATIONSHIP_HAS_MANY = 'has_many'; |
||
| 62 | const RELATIONSHIP_BELONGS_TO = 'belongs_to'; |
||
| 63 | const RELATIONSHIP_BELONGS_TO_MANY = 'belongs_to_many'; |
||
| 64 | |||
| 65 | const DEFAULT_ID_PROPERTY = 'id'; |
||
| 66 | |||
| 67 | ///////////////////////////// |
||
| 68 | // Model visible variables |
||
| 69 | ///////////////////////////// |
||
| 70 | |||
| 71 | /** |
||
| 72 | * List of model ID property names. |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected static $ids = [self::DEFAULT_ID_PROPERTY]; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Property definitions expressed as a key-value map with |
||
| 80 | * property names as the keys. |
||
| 81 | * i.e. ['enabled' => ['type' => Model::TYPE_BOOLEAN]]. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected static $properties = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected static $dispatchers; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var number|string|false |
||
| 94 | */ |
||
| 95 | protected $_id; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $_ids; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | protected $_values = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | protected $_unsaved = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var bool |
||
| 114 | */ |
||
| 115 | protected $_persisted = false; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var bool |
||
| 119 | */ |
||
| 120 | protected $_loaded = false; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | protected $_relationships = []; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var Errors |
||
| 129 | */ |
||
| 130 | protected $_errors; |
||
| 131 | |||
| 132 | ///////////////////////////// |
||
| 133 | // Base model variables |
||
| 134 | ///////////////////////////// |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var array |
||
| 138 | */ |
||
| 139 | private static $propertyDefinitionBase = [ |
||
| 140 | 'type' => null, |
||
| 141 | 'mutable' => self::MUTABLE, |
||
| 142 | 'null' => false, |
||
| 143 | 'unique' => false, |
||
| 144 | 'required' => false, |
||
| 145 | ]; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var array |
||
| 149 | */ |
||
| 150 | private static $defaultIDProperty = [ |
||
| 151 | 'type' => self::TYPE_INTEGER, |
||
| 152 | 'mutable' => self::IMMUTABLE, |
||
| 153 | ]; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | private static $timestampProperties = [ |
||
| 159 | 'created_at' => [ |
||
| 160 | 'type' => self::TYPE_DATE, |
||
| 161 | 'validate' => 'timestamp|db_timestamp', |
||
| 162 | ], |
||
| 163 | 'updated_at' => [ |
||
| 164 | 'type' => self::TYPE_DATE, |
||
| 165 | 'validate' => 'timestamp|db_timestamp', |
||
| 166 | ], |
||
| 167 | ]; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var array |
||
| 171 | */ |
||
| 172 | private static $softDeleteProperties = [ |
||
| 173 | 'deleted_at' => [ |
||
| 174 | 'type' => self::TYPE_DATE, |
||
| 175 | 'validate' => 'timestamp|db_timestamp', |
||
| 176 | 'null' => true, |
||
| 177 | ], |
||
| 178 | ]; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var array |
||
| 182 | */ |
||
| 183 | private static $initialized = []; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @var DriverInterface |
||
| 187 | */ |
||
| 188 | private static $driver; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @var array |
||
| 192 | */ |
||
| 193 | private static $accessors = []; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @var array |
||
| 197 | */ |
||
| 198 | private static $mutators = []; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @var bool |
||
| 202 | */ |
||
| 203 | private $_ignoreUnsaved; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Creates a new model object. |
||
| 207 | * |
||
| 208 | * @param array|string|Model|false $id ordered array of ids or comma-separated id string |
||
| 209 | * @param array $values optional key-value map to pre-seed model |
||
| 210 | */ |
||
| 211 | public function __construct($id = false, array $values = []) |
||
| 212 | { |
||
| 213 | // initialize the model |
||
| 214 | $this->init(); |
||
| 215 | |||
| 216 | // parse the supplied model ID |
||
| 217 | $this->parseId($id); |
||
| 218 | |||
| 219 | // load any given values |
||
| 220 | if (count($values) > 0) { |
||
| 221 | $this->refreshWith($values); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Performs initialization on this model. |
||
| 227 | */ |
||
| 228 | private function init() |
||
| 229 | { |
||
| 230 | // ensure the initialize function is called only once |
||
| 231 | $k = get_called_class(); |
||
| 232 | if (!isset(self::$initialized[$k])) { |
||
| 233 | $this->initialize(); |
||
| 234 | self::$initialized[$k] = true; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * The initialize() method is called once per model. It's used |
||
| 240 | * to perform any one-off tasks before the model gets |
||
| 241 | * constructed. This is a great place to add any model |
||
| 242 | * properties. When extending this method be sure to call |
||
| 243 | * parent::initialize() as some important stuff happens here. |
||
| 244 | * If extending this method to add properties then you should |
||
| 245 | * call parent::initialize() after adding any properties. |
||
| 246 | */ |
||
| 247 | protected function initialize() |
||
| 248 | { |
||
| 249 | // load the driver |
||
| 250 | static::getDriver(); |
||
| 251 | |||
| 252 | // add in the default ID property |
||
| 253 | if (static::$ids == [self::DEFAULT_ID_PROPERTY] && !isset(static::$properties[self::DEFAULT_ID_PROPERTY])) { |
||
| 254 | static::$properties[self::DEFAULT_ID_PROPERTY] = self::$defaultIDProperty; |
||
| 255 | } |
||
| 256 | |||
| 257 | // generates created_at and updated_at timestamps |
||
| 258 | if (property_exists($this, 'autoTimestamps')) { |
||
| 259 | $this->installAutoTimestamps(); |
||
| 260 | } |
||
| 261 | |||
| 262 | // generates deleted_at timestamps |
||
| 263 | if (property_exists($this, 'softDelete')) { |
||
| 264 | $this->installSoftDelete(); |
||
| 265 | } |
||
| 266 | |||
| 267 | // fill in each property by extending the property |
||
| 268 | // definition base |
||
| 269 | foreach (static::$properties as $k => &$property) { |
||
| 270 | $property = array_replace(self::$propertyDefinitionBase, $property); |
||
| 271 | |||
| 272 | // populate relationship property settings |
||
| 273 | if (isset($property['relation'])) { |
||
| 274 | // this is added for BC with older versions of pulsar |
||
| 275 | // that only supported belongs to relationships |
||
| 276 | if (!isset($property['relation_type'])) { |
||
| 277 | $property['relation_type'] = self::RELATIONSHIP_BELONGS_TO; |
||
| 278 | $property['local_key'] = $k; |
||
| 279 | } |
||
| 280 | |||
| 281 | $relation = $this->getRelationshipManager($k); |
||
|
|
|||
| 282 | if (!isset($property['foreign_key'])) { |
||
| 283 | $property['foreign_key'] = $relation->getForeignKey(); |
||
| 284 | } |
||
| 285 | |||
| 286 | if (!isset($property['local_key'])) { |
||
| 287 | $property['local_key'] = $relation->getLocalKey(); |
||
| 288 | } |
||
| 289 | |||
| 290 | if (!isset($property['pivot_tablename']) && $relation instanceof BelongsToMany) { |
||
| 291 | $property['pivot_tablename'] = $relation->getTablename(); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | // order the properties array by name for consistency |
||
| 297 | // since it is constructed in a random order |
||
| 298 | ksort(static::$properties); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Installs the `created_at` and `updated_at` properties. |
||
| 303 | */ |
||
| 304 | private function installAutoTimestamps() |
||
| 305 | { |
||
| 306 | static::$properties = array_replace(self::$timestampProperties, static::$properties); |
||
| 307 | |||
| 308 | self::creating(function (ModelEvent $event) { |
||
| 309 | $model = $event->getModel(); |
||
| 310 | $model->created_at = time(); |
||
| 311 | $model->updated_at = time(); |
||
| 312 | }); |
||
| 313 | |||
| 314 | self::updating(function (ModelEvent $event) { |
||
| 315 | $event->getModel()->updated_at = time(); |
||
| 316 | }); |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Installs the `deleted_at` properties. |
||
| 321 | */ |
||
| 322 | private function installSoftDelete() |
||
| 323 | { |
||
| 324 | static::$properties = array_replace(self::$softDeleteProperties, static::$properties); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Parses the given ID, which can be a single or composite primary key. |
||
| 329 | * |
||
| 330 | * @param mixed $id |
||
| 331 | */ |
||
| 332 | private function parseId($id) |
||
| 333 | { |
||
| 334 | if (is_array($id)) { |
||
| 335 | // A model can be supplied as a primary key |
||
| 336 | foreach ($id as &$el) { |
||
| 337 | if ($el instanceof self) { |
||
| 338 | $el = $el->id(); |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | // The IDs come in as the same order as ::$ids. |
||
| 343 | // We need to match up the elements on that |
||
| 344 | // input into a key-value map for each ID property. |
||
| 345 | $ids = []; |
||
| 346 | $idQueue = array_reverse($id); |
||
| 347 | foreach (static::$ids as $k => $f) { |
||
| 348 | // type cast |
||
| 349 | if (count($idQueue) > 0) { |
||
| 350 | $idProperty = static::getProperty($f); |
||
| 351 | $ids[$f] = static::cast($idProperty, array_pop($idQueue)); |
||
| 352 | } else { |
||
| 353 | $ids[$f] = false; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | $this->_id = implode(',', $id); |
||
| 358 | $this->_ids = $ids; |
||
| 359 | } elseif ($id instanceof self) { |
||
| 360 | // A model can be supplied as a primary key |
||
| 361 | $this->_id = $id->id(); |
||
| 362 | $this->_ids = $id->ids(); |
||
| 363 | } else { |
||
| 364 | // type cast the single primary key |
||
| 365 | $idName = static::$ids[0]; |
||
| 366 | if (false !== $id) { |
||
| 367 | $idProperty = static::getProperty($idName); |
||
| 368 | $id = static::cast($idProperty, $id); |
||
| 369 | } |
||
| 370 | |||
| 371 | $this->_id = $id; |
||
| 372 | $this->_ids = [$idName => $id]; |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Sets the driver for all models. |
||
| 378 | * |
||
| 379 | * @param DriverInterface $driver |
||
| 380 | */ |
||
| 381 | public static function setDriver(DriverInterface $driver) |
||
| 382 | { |
||
| 383 | self::$driver = $driver; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Gets the driver for all models. |
||
| 388 | * |
||
| 389 | * @return DriverInterface |
||
| 390 | * |
||
| 391 | * @throws DriverMissingException when a driver has not been set yet |
||
| 392 | */ |
||
| 393 | public static function getDriver() |
||
| 394 | { |
||
| 395 | if (!self::$driver) { |
||
| 396 | throw new DriverMissingException('A model driver has not been set yet.'); |
||
| 397 | } |
||
| 398 | |||
| 399 | return self::$driver; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Clears the driver for all models. |
||
| 404 | */ |
||
| 405 | public static function clearDriver() |
||
| 406 | { |
||
| 407 | self::$driver = null; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Gets the name of the model, i.e. User. |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | public static function modelName() |
||
| 416 | { |
||
| 417 | // strip namespacing |
||
| 418 | $paths = explode('\\', get_called_class()); |
||
| 419 | |||
| 420 | return end($paths); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Gets the model ID. |
||
| 425 | * |
||
| 426 | * @return string|number|false ID |
||
| 427 | */ |
||
| 428 | public function id() |
||
| 429 | { |
||
| 430 | return $this->_id; |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Gets a key-value map of the model ID. |
||
| 435 | * |
||
| 436 | * @return array ID map |
||
| 437 | */ |
||
| 438 | public function ids() |
||
| 439 | { |
||
| 440 | return $this->_ids; |
||
| 441 | } |
||
| 442 | |||
| 443 | ///////////////////////////// |
||
| 444 | // Magic Methods |
||
| 445 | ///////////////////////////// |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Converts the model into a string. |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | public function __toString() |
||
| 453 | { |
||
| 454 | $values = array_merge($this->_values, $this->_unsaved, $this->_ids); |
||
| 455 | ksort($values); |
||
| 456 | |||
| 457 | return get_called_class().'('.json_encode($values, JSON_PRETTY_PRINT).')'; |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Shortcut to a get() call for a given property. |
||
| 462 | * |
||
| 463 | * @param string $name |
||
| 464 | * |
||
| 465 | * @return mixed |
||
| 466 | */ |
||
| 467 | public function __get($name) |
||
| 468 | { |
||
| 469 | $result = $this->get([$name]); |
||
| 470 | |||
| 471 | return reset($result); |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Sets an unsaved value. |
||
| 476 | * |
||
| 477 | * @param string $name |
||
| 478 | * @param mixed $value |
||
| 479 | */ |
||
| 480 | public function __set($name, $value) |
||
| 481 | { |
||
| 482 | // if changing property, remove relation model |
||
| 483 | if (isset($this->_relationships[$name])) { |
||
| 484 | unset($this->_relationships[$name]); |
||
| 485 | } |
||
| 486 | |||
| 487 | // call any mutators |
||
| 488 | $mutator = self::getMutator($name); |
||
| 489 | if ($mutator) { |
||
| 490 | $this->_unsaved[$name] = $this->$mutator($value); |
||
| 491 | } else { |
||
| 492 | $this->_unsaved[$name] = $value; |
||
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Checks if an unsaved value or property exists by this name. |
||
| 498 | * |
||
| 499 | * @param string $name |
||
| 500 | * |
||
| 501 | * @return bool |
||
| 502 | */ |
||
| 503 | public function __isset($name) |
||
| 504 | { |
||
| 505 | return array_key_exists($name, $this->_unsaved) || static::hasProperty($name); |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Unsets an unsaved value. |
||
| 510 | * |
||
| 511 | * @param string $name |
||
| 512 | */ |
||
| 513 | public function __unset($name) |
||
| 514 | { |
||
| 515 | if (array_key_exists($name, $this->_unsaved)) { |
||
| 516 | // if changing property, remove relation model |
||
| 517 | if (isset($this->_relationships[$name])) { |
||
| 518 | unset($this->_relationships[$name]); |
||
| 519 | } |
||
| 520 | |||
| 521 | unset($this->_unsaved[$name]); |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | ///////////////////////////// |
||
| 526 | // ArrayAccess Interface |
||
| 527 | ///////////////////////////// |
||
| 528 | |||
| 529 | public function offsetExists($offset) |
||
| 530 | { |
||
| 531 | return isset($this->$offset); |
||
| 532 | } |
||
| 533 | |||
| 534 | public function offsetGet($offset) |
||
| 535 | { |
||
| 536 | return $this->$offset; |
||
| 537 | } |
||
| 538 | |||
| 539 | public function offsetSet($offset, $value) |
||
| 540 | { |
||
| 541 | $this->$offset = $value; |
||
| 542 | } |
||
| 543 | |||
| 544 | public function offsetUnset($offset) |
||
| 545 | { |
||
| 546 | unset($this->$offset); |
||
| 547 | } |
||
| 548 | |||
| 549 | public static function __callStatic($name, $parameters) |
||
| 550 | { |
||
| 551 | // Any calls to unkown static methods should be deferred to |
||
| 552 | // the query. This allows calls like User::where() |
||
| 553 | // to replace User::query()->where(). |
||
| 554 | return call_user_func_array([static::query(), $name], $parameters); |
||
| 555 | } |
||
| 556 | |||
| 557 | ///////////////////////////// |
||
| 558 | // Property Definitions |
||
| 559 | ///////////////////////////// |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Gets all the property definitions for the model. |
||
| 563 | * |
||
| 564 | * @return array key-value map of properties |
||
| 565 | */ |
||
| 566 | public static function getProperties() |
||
| 567 | { |
||
| 568 | return static::$properties; |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Gets a property defition for the model. |
||
| 573 | * |
||
| 574 | * @param string $property property to lookup |
||
| 575 | * |
||
| 576 | * @return array|null property |
||
| 577 | */ |
||
| 578 | public static function getProperty($property) |
||
| 579 | { |
||
| 580 | return array_value(static::$properties, $property); |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Gets the names of the model ID properties. |
||
| 585 | * |
||
| 586 | * @return array |
||
| 587 | */ |
||
| 588 | public static function getIDProperties() |
||
| 589 | { |
||
| 590 | return static::$ids; |
||
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Checks if the model has a property. |
||
| 595 | * |
||
| 596 | * @param string $property property |
||
| 597 | * |
||
| 598 | * @return bool has property |
||
| 599 | */ |
||
| 600 | public static function hasProperty($property) |
||
| 601 | { |
||
| 602 | return isset(static::$properties[$property]); |
||
| 603 | } |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Gets the mutator method name for a given proeprty name. |
||
| 607 | * Looks for methods in the form of `setPropertyValue`. |
||
| 608 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 609 | * |
||
| 610 | * @param string $property property |
||
| 611 | * |
||
| 612 | * @return string|false method name if it exists |
||
| 613 | */ |
||
| 614 | View Code Duplication | public static function getMutator($property) |
|
| 632 | |||
| 633 | /** |
||
| 634 | * Gets the accessor method name for a given proeprty name. |
||
| 635 | * Looks for methods in the form of `getPropertyValue`. |
||
| 636 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 637 | * |
||
| 638 | * @param string $property property |
||
| 639 | * |
||
| 640 | * @return string|false method name if it exists |
||
| 641 | */ |
||
| 642 | View Code Duplication | public static function getAccessor($property) |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Marshals a value for a given property from storage. |
||
| 663 | * |
||
| 664 | * @param array $property |
||
| 665 | * @param mixed $value |
||
| 666 | * |
||
| 667 | * @return mixed type-casted value |
||
| 668 | */ |
||
| 669 | public static function cast(array $property, $value) |
||
| 670 | { |
||
| 671 | if (null === $value) { |
||
| 672 | return; |
||
| 673 | } |
||
| 674 | |||
| 675 | // handle empty strings as null |
||
| 676 | if ($property['null'] && '' == $value) { |
||
| 677 | return; |
||
| 678 | } |
||
| 679 | |||
| 680 | $type = array_value($property, 'type'); |
||
| 681 | $m = 'to_'.$type; |
||
| 682 | |||
| 683 | if (!method_exists(Property::class, $m)) { |
||
| 684 | return $value; |
||
| 685 | } |
||
| 686 | |||
| 689 | |||
| 690 | ///////////////////////////// |
||
| 691 | // CRUD Operations |
||
| 692 | ///////////////////////////// |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Gets the tablename for storing this model. |
||
| 696 | * |
||
| 697 | * @return string |
||
| 698 | */ |
||
| 699 | public function getTablename() |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Gets the ID of the connection in the connection manager |
||
| 708 | * that stores this model. |
||
| 709 | * |
||
| 710 | * @return string|null |
||
| 711 | */ |
||
| 712 | public function getConnection() |
||
| 716 | |||
| 717 | protected function usesTransactions(): bool |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Saves the model. |
||
| 724 | * |
||
| 725 | * @return bool true when the operation was successful |
||
| 726 | */ |
||
| 727 | public function save() |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Saves the model. Throws an exception when the operation fails. |
||
| 738 | * |
||
| 739 | * @throws ModelException when the model cannot be saved |
||
| 740 | */ |
||
| 741 | public function saveOrFail() |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Creates a new model. |
||
| 755 | * |
||
| 756 | * @param array $data optional key-value properties to set |
||
| 757 | * |
||
| 758 | * @return bool true when the operation was successful |
||
| 759 | * |
||
| 760 | * @throws BadMethodCallException when called on an existing model |
||
| 761 | */ |
||
| 762 | public function create(array $data = []) |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Ignores unsaved values when fetching the next value. |
||
| 868 | * |
||
| 869 | * @return $this |
||
| 870 | */ |
||
| 871 | public function ignoreUnsaved() |
||
| 877 | |||
| 878 | /** |
||
| 879 | * Fetches property values from the model. |
||
| 880 | * |
||
| 881 | * This method looks up values in this order: |
||
| 882 | * IDs, local cache, unsaved values, storage layer, defaults |
||
| 883 | * |
||
| 884 | * @param array $properties list of property names to fetch values of |
||
| 885 | * |
||
| 886 | * @return array |
||
| 887 | */ |
||
| 888 | public function get(array $properties) |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Gets a property value from the model. |
||
| 929 | * |
||
| 930 | * Values are looked up in this order: |
||
| 931 | * 1. unsaved values |
||
| 932 | * 2. local values |
||
| 933 | * 3. default value |
||
| 934 | * 4. null |
||
| 935 | * |
||
| 936 | * @param string $property |
||
| 937 | * @param array $values |
||
| 938 | * |
||
| 939 | * @return mixed |
||
| 940 | */ |
||
| 941 | protected function getValue($property, array $values) |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Populates a newly created model with its ID. |
||
| 961 | */ |
||
| 962 | protected function getNewID() |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Sets a collection values on the model from an untrusted input. |
||
| 985 | * |
||
| 986 | * @param array $values |
||
| 987 | * |
||
| 988 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 989 | * |
||
| 990 | * @return $this |
||
| 991 | */ |
||
| 992 | public function setValues($values) |
||
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Converts the model to an array. |
||
| 1015 | * |
||
| 1016 | * @return array |
||
| 1017 | */ |
||
| 1018 | public function toArray() |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Updates the model. |
||
| 1052 | * |
||
| 1053 | * @param array $data optional key-value properties to set |
||
| 1054 | * |
||
| 1055 | * @return bool true when the operation was successful |
||
| 1056 | * |
||
| 1057 | * @throws BadMethodCallException when not called on an existing model |
||
| 1058 | */ |
||
| 1059 | public function set(array $data = []) |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Delete the model. |
||
| 1139 | * |
||
| 1140 | * @return bool true when the operation was successful |
||
| 1141 | */ |
||
| 1142 | public function delete() |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Restores a soft-deleted model. |
||
| 1195 | * |
||
| 1196 | * @return bool |
||
| 1197 | */ |
||
| 1198 | public function restore() |
||
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Checks if the model has been deleted. |
||
| 1235 | * |
||
| 1236 | * @return bool |
||
| 1237 | */ |
||
| 1238 | public function isDeleted() |
||
| 1246 | |||
| 1247 | ///////////////////////////// |
||
| 1248 | // Queries |
||
| 1249 | ///////////////////////////// |
||
| 1250 | |||
| 1251 | /** |
||
| 1252 | * Generates a new query instance. |
||
| 1253 | * |
||
| 1254 | * @return Query |
||
| 1255 | */ |
||
| 1256 | public static function query() |
||
| 1271 | |||
| 1272 | /** |
||
| 1273 | * Generates a new query instance that includes soft-deleted models. |
||
| 1274 | * |
||
| 1275 | * @return Query |
||
| 1276 | */ |
||
| 1277 | public static function withDeleted() |
||
| 1286 | |||
| 1287 | /** |
||
| 1288 | * Finds a single instance of a model given it's ID. |
||
| 1289 | * |
||
| 1290 | * @param mixed $id |
||
| 1291 | * |
||
| 1292 | * @return static|null |
||
| 1293 | */ |
||
| 1294 | public static function find($id) |
||
| 1311 | |||
| 1312 | /** |
||
| 1313 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1314 | * |
||
| 1315 | * @param mixed $id |
||
| 1316 | * |
||
| 1317 | * @return static |
||
| 1318 | * |
||
| 1319 | * @throws ModelNotFoundException when a model could not be found |
||
| 1320 | */ |
||
| 1321 | public static function findOrFail($id) |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * @deprecated |
||
| 1333 | * |
||
| 1334 | * Checks if the model exists in the database |
||
| 1335 | * |
||
| 1336 | * @return bool |
||
| 1337 | */ |
||
| 1338 | public function exists() |
||
| 1342 | |||
| 1343 | /** |
||
| 1344 | * Tells if this model instance has been persisted to the data layer. |
||
| 1345 | * |
||
| 1346 | * NOTE: this does not actually perform a check with the data layer |
||
| 1347 | * |
||
| 1348 | * @return bool |
||
| 1349 | */ |
||
| 1350 | public function persisted() |
||
| 1354 | |||
| 1355 | /** |
||
| 1356 | * Loads the model from the storage layer. |
||
| 1357 | * |
||
| 1358 | * @return $this |
||
| 1359 | */ |
||
| 1360 | public function refresh() |
||
| 1377 | |||
| 1378 | /** |
||
| 1379 | * Loads values into the model. |
||
| 1380 | * |
||
| 1381 | * @param array $values values |
||
| 1382 | * |
||
| 1383 | * @return $this |
||
| 1384 | */ |
||
| 1385 | public function refreshWith(array $values) |
||
| 1400 | |||
| 1401 | /** |
||
| 1402 | * Clears the cache for this model. |
||
| 1403 | * |
||
| 1404 | * @return $this |
||
| 1405 | */ |
||
| 1406 | public function clearCache() |
||
| 1415 | |||
| 1416 | ///////////////////////////// |
||
| 1417 | // Relationships |
||
| 1418 | ///////////////////////////// |
||
| 1419 | |||
| 1420 | /** |
||
| 1421 | * @deprecated |
||
| 1422 | * |
||
| 1423 | * Gets the model(s) for a relationship |
||
| 1424 | * |
||
| 1425 | * @param string $k property |
||
| 1426 | * |
||
| 1427 | * @throws \InvalidArgumentException when the relationship manager cannot be created |
||
| 1428 | * |
||
| 1429 | * @return Model|null |
||
| 1430 | */ |
||
| 1431 | public function relation($k) |
||
| 1440 | |||
| 1441 | /** |
||
| 1442 | * @deprecated |
||
| 1443 | * |
||
| 1444 | * Sets the model for a one-to-one relationship (has-one or belongs-to) |
||
| 1445 | * |
||
| 1446 | * @param string $k |
||
| 1447 | * @param Model $model |
||
| 1448 | * |
||
| 1449 | * @return $this |
||
| 1450 | */ |
||
| 1451 | public function setRelation($k, self $model) |
||
| 1458 | |||
| 1459 | /** |
||
| 1460 | * @deprecated |
||
| 1461 | * |
||
| 1462 | * Sets the model for a one-to-many relationship |
||
| 1463 | * |
||
| 1464 | * @param string $k |
||
| 1465 | * @param iterable $models |
||
| 1466 | * |
||
| 1467 | * @return $this |
||
| 1468 | */ |
||
| 1469 | public function setRelationCollection($k, $models) |
||
| 1475 | |||
| 1476 | /** |
||
| 1477 | * Sets the model for a one-to-one relationship (has-one or belongs-to) as null. |
||
| 1478 | * |
||
| 1479 | * @param string $k |
||
| 1480 | * |
||
| 1481 | * @return $this |
||
| 1482 | */ |
||
| 1483 | public function clearRelation($k) |
||
| 1490 | |||
| 1491 | /** |
||
| 1492 | * Builds a relationship manager object for a given property. |
||
| 1493 | * |
||
| 1494 | * @param array $k |
||
| 1495 | * |
||
| 1496 | * @throws \InvalidArgumentException when the relationship manager cannot be created |
||
| 1497 | * |
||
| 1498 | * @return Relation |
||
| 1499 | */ |
||
| 1500 | public function getRelationshipManager($k) |
||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * Creates the parent side of a One-To-One 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 HasOne |
||
| 1540 | */ |
||
| 1541 | public function hasOne($model, $foreignKey = '', $localKey = '') |
||
| 1545 | |||
| 1546 | /** |
||
| 1547 | * Creates the child side of a One-To-One or One-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 BelongsTo |
||
| 1554 | */ |
||
| 1555 | public function belongsTo($model, $foreignKey = '', $localKey = '') |
||
| 1559 | |||
| 1560 | /** |
||
| 1561 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
| 1562 | * |
||
| 1563 | * @param string $model foreign model class |
||
| 1564 | * @param string $foreignKey identifying key on foreign model |
||
| 1565 | * @param string $localKey identifying key on local model |
||
| 1566 | * |
||
| 1567 | * @return HasMany |
||
| 1568 | */ |
||
| 1569 | public function hasMany($model, $foreignKey = '', $localKey = '') |
||
| 1573 | |||
| 1574 | /** |
||
| 1575 | * Creates the child side of a Many-To-Many relationship. |
||
| 1576 | * |
||
| 1577 | * @param string $model foreign model class |
||
| 1578 | * @param string $tablename pivot table name |
||
| 1579 | * @param string $foreignKey identifying key on foreign model |
||
| 1580 | * @param string $localKey identifying key on local model |
||
| 1581 | * |
||
| 1582 | * @return BelongsToMany |
||
| 1583 | */ |
||
| 1584 | public function belongsToMany($model, $tablename = '', $foreignKey = '', $localKey = '') |
||
| 1588 | |||
| 1589 | ///////////////////////////// |
||
| 1590 | // Events |
||
| 1591 | ///////////////////////////// |
||
| 1592 | |||
| 1593 | /** |
||
| 1594 | * Gets the event dispatcher. |
||
| 1595 | * |
||
| 1596 | * @return EventDispatcher |
||
| 1597 | */ |
||
| 1598 | public static function getDispatcher($ignoreCache = false) |
||
| 1607 | |||
| 1608 | /** |
||
| 1609 | * Subscribes to a listener to an event. |
||
| 1610 | * |
||
| 1611 | * @param string $event event name |
||
| 1612 | * @param callable $listener |
||
| 1613 | * @param int $priority optional priority, higher #s get called first |
||
| 1614 | */ |
||
| 1615 | public static function listen($event, callable $listener, $priority = 0) |
||
| 1619 | |||
| 1620 | /** |
||
| 1621 | * Adds a listener to the model.creating and model.updating events. |
||
| 1622 | * |
||
| 1623 | * @param callable $listener |
||
| 1624 | * @param int $priority |
||
| 1625 | */ |
||
| 1626 | public static function saving(callable $listener, $priority = 0) |
||
| 1631 | |||
| 1632 | /** |
||
| 1633 | * Adds a listener to the model.created and model.updated events. |
||
| 1634 | * |
||
| 1635 | * @param callable $listener |
||
| 1636 | * @param int $priority |
||
| 1637 | */ |
||
| 1638 | public static function saved(callable $listener, $priority = 0) |
||
| 1643 | |||
| 1644 | /** |
||
| 1645 | * Adds a listener to the model.creating event. |
||
| 1646 | * |
||
| 1647 | * @param callable $listener |
||
| 1648 | * @param int $priority |
||
| 1649 | */ |
||
| 1650 | public static function creating(callable $listener, $priority = 0) |
||
| 1654 | |||
| 1655 | /** |
||
| 1656 | * Adds a listener to the model.created event. |
||
| 1657 | * |
||
| 1658 | * @param callable $listener |
||
| 1659 | * @param int $priority |
||
| 1660 | */ |
||
| 1661 | public static function created(callable $listener, $priority = 0) |
||
| 1665 | |||
| 1666 | /** |
||
| 1667 | * Adds a listener to the model.updating event. |
||
| 1668 | * |
||
| 1669 | * @param callable $listener |
||
| 1670 | * @param int $priority |
||
| 1671 | */ |
||
| 1672 | public static function updating(callable $listener, $priority = 0) |
||
| 1676 | |||
| 1677 | /** |
||
| 1678 | * Adds a listener to the model.updated event. |
||
| 1679 | * |
||
| 1680 | * @param callable $listener |
||
| 1681 | * @param int $priority |
||
| 1682 | */ |
||
| 1683 | public static function updated(callable $listener, $priority = 0) |
||
| 1687 | |||
| 1688 | /** |
||
| 1689 | * Adds a listener to the model.deleting event. |
||
| 1690 | * |
||
| 1691 | * @param callable $listener |
||
| 1692 | * @param int $priority |
||
| 1693 | */ |
||
| 1694 | public static function deleting(callable $listener, $priority = 0) |
||
| 1698 | |||
| 1699 | /** |
||
| 1700 | * Adds a listener to the model.deleted event. |
||
| 1701 | * |
||
| 1702 | * @param callable $listener |
||
| 1703 | * @param int $priority |
||
| 1704 | */ |
||
| 1705 | public static function deleted(callable $listener, $priority = 0) |
||
| 1709 | |||
| 1710 | /** |
||
| 1711 | * Dispatches the given event and checks if it was successful. |
||
| 1712 | * |
||
| 1713 | * @return bool true if the events were successfully propagated |
||
| 1714 | */ |
||
| 1715 | private function performDispatch(string $eventName, bool $usesTransactions): bool |
||
| 1743 | |||
| 1744 | ///////////////////////////// |
||
| 1745 | // Validation |
||
| 1746 | ///////////////////////////// |
||
| 1747 | |||
| 1748 | /** |
||
| 1749 | * Gets the error stack for this model. |
||
| 1750 | * |
||
| 1751 | * @return Errors |
||
| 1752 | */ |
||
| 1753 | public function getErrors() |
||
| 1761 | |||
| 1762 | /** |
||
| 1763 | * Checks if the model in its current state is valid. |
||
| 1764 | * |
||
| 1765 | * @return bool |
||
| 1766 | */ |
||
| 1767 | public function valid() |
||
| 1788 | |||
| 1789 | /** |
||
| 1790 | * Validates and marshals a value to storage. |
||
| 1791 | * |
||
| 1792 | * @param array $property property definition |
||
| 1793 | * @param string $name property name |
||
| 1794 | * @param mixed $value |
||
| 1795 | * |
||
| 1796 | * @return bool |
||
| 1797 | */ |
||
| 1798 | private function filterAndValidate(array $property, $name, &$value) |
||
| 1818 | |||
| 1819 | /** |
||
| 1820 | * Validates a value for a property. |
||
| 1821 | * |
||
| 1822 | * @param array $property property definition |
||
| 1823 | * @param string $name property name |
||
| 1824 | * @param mixed $value |
||
| 1825 | * |
||
| 1826 | * @return array |
||
| 1827 | */ |
||
| 1828 | private function validateValue(array $property, $name, $value) |
||
| 1851 | |||
| 1852 | /** |
||
| 1853 | * Checks if a value is unique for a property. |
||
| 1854 | * |
||
| 1855 | * @param array $property property definition |
||
| 1856 | * @param string $name property name |
||
| 1857 | * @param mixed $value |
||
| 1858 | * |
||
| 1859 | * @return bool |
||
| 1860 | */ |
||
| 1861 | private function checkUniqueness(array $property, $name, $value) |
||
| 1876 | |||
| 1877 | /** |
||
| 1878 | * Gets the marshaled default value for a property (if set). |
||
| 1879 | * |
||
| 1880 | * @param array $property |
||
| 1881 | * |
||
| 1882 | * @return mixed |
||
| 1883 | */ |
||
| 1884 | private function getPropertyDefault(array $property) |
||
| 1888 | |||
| 1889 | /** |
||
| 1890 | * Gets the humanized name of a property. |
||
| 1891 | * |
||
| 1892 | * @param array $property property definition |
||
| 1893 | * @param string $name property name |
||
| 1894 | * |
||
| 1895 | * @return string |
||
| 1896 | */ |
||
| 1897 | private function getPropertyTitle(array $property, $name) |
||
| 1915 | } |
||
| 1916 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: