Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | abstract class Model implements \ArrayAccess |
||
| 31 | { |
||
| 32 | const IMMUTABLE = 0; |
||
| 33 | const MUTABLE_CREATE_ONLY = 1; |
||
| 34 | const MUTABLE = 2; |
||
| 35 | |||
| 36 | const TYPE_STRING = 'string'; |
||
| 37 | const TYPE_NUMBER = 'number'; // DEPRECATED |
||
| 38 | const TYPE_INTEGER = 'integer'; |
||
| 39 | const TYPE_FLOAT = 'float'; |
||
| 40 | const TYPE_BOOLEAN = 'boolean'; |
||
| 41 | const TYPE_DATE = 'date'; |
||
| 42 | const TYPE_OBJECT = 'object'; |
||
| 43 | const TYPE_ARRAY = 'array'; |
||
| 44 | |||
| 45 | const DEFAULT_ID_PROPERTY = 'id'; |
||
| 46 | |||
| 47 | ///////////////////////////// |
||
| 48 | // Model visible variables |
||
| 49 | ///////////////////////////// |
||
| 50 | |||
| 51 | /** |
||
| 52 | * List of model ID property names. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected static $ids = [self::DEFAULT_ID_PROPERTY]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Property definitions expressed as a key-value map with |
||
| 60 | * property names as the keys. |
||
| 61 | * i.e. ['enabled' => ['type' => Model::TYPE_BOOLEAN]]. |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected static $properties = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected static $dispatchers; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var number|string|false |
||
| 74 | */ |
||
| 75 | protected $_id; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $_ids; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $_values = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $_unsaved = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var bool |
||
| 94 | */ |
||
| 95 | protected $_persisted = false; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var bool |
||
| 99 | */ |
||
| 100 | protected $_loaded = false; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | protected $_relationships = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var Errors |
||
| 109 | */ |
||
| 110 | protected $_errors; |
||
| 111 | |||
| 112 | ///////////////////////////// |
||
| 113 | // Base model variables |
||
| 114 | ///////////////////////////// |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var array |
||
| 118 | */ |
||
| 119 | private static $propertyDefinitionBase = [ |
||
| 120 | 'type' => null, |
||
| 121 | 'mutable' => self::MUTABLE, |
||
| 122 | 'null' => false, |
||
| 123 | 'unique' => false, |
||
| 124 | 'required' => false, |
||
| 125 | ]; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var array |
||
| 129 | */ |
||
| 130 | private static $defaultIDProperty = [ |
||
| 131 | 'type' => self::TYPE_INTEGER, |
||
| 132 | 'mutable' => self::IMMUTABLE, |
||
| 133 | ]; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var array |
||
| 137 | */ |
||
| 138 | private static $timestampProperties = [ |
||
| 139 | 'created_at' => [ |
||
| 140 | 'type' => self::TYPE_DATE, |
||
| 141 | 'validate' => 'timestamp|db_timestamp', |
||
| 142 | ], |
||
| 143 | 'updated_at' => [ |
||
| 144 | 'type' => self::TYPE_DATE, |
||
| 145 | 'validate' => 'timestamp|db_timestamp', |
||
| 146 | ], |
||
| 147 | ]; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | private static $softDeleteProperties = [ |
||
| 153 | 'deleted_at' => [ |
||
| 154 | 'type' => self::TYPE_DATE, |
||
| 155 | 'validate' => 'timestamp|db_timestamp', |
||
| 156 | 'null' => true, |
||
| 157 | ], |
||
| 158 | ]; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var array |
||
| 162 | */ |
||
| 163 | private static $initialized = []; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var DriverInterface |
||
| 167 | */ |
||
| 168 | private static $driver; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var array |
||
| 172 | */ |
||
| 173 | private static $accessors = []; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var array |
||
| 177 | */ |
||
| 178 | private static $mutators = []; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var bool |
||
| 182 | */ |
||
| 183 | private $_ignoreUnsaved; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Creates a new model object. |
||
| 187 | * |
||
| 188 | * @param array|string|Model|false $id ordered array of ids or comma-separated id string |
||
| 189 | * @param array $values optional key-value map to pre-seed model |
||
| 190 | */ |
||
| 191 | public function __construct($id = false, array $values = []) |
||
| 192 | { |
||
| 193 | // initialize the model |
||
| 194 | $this->init(); |
||
| 195 | |||
| 196 | // parse the supplied model ID |
||
| 197 | $this->parseId($id); |
||
| 198 | |||
| 199 | // load any given values |
||
| 200 | if (count($values) > 0) { |
||
| 201 | $this->refreshWith($values); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Performs initialization on this model. |
||
| 207 | */ |
||
| 208 | private function init() |
||
| 209 | { |
||
| 210 | // ensure the initialize function is called only once |
||
| 211 | $k = get_called_class(); |
||
| 212 | if (!isset(self::$initialized[$k])) { |
||
| 213 | $this->initialize(); |
||
| 214 | self::$initialized[$k] = true; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * The initialize() method is called once per model. It's used |
||
| 220 | * to perform any one-off tasks before the model gets |
||
| 221 | * constructed. This is a great place to add any model |
||
| 222 | * properties. When extending this method be sure to call |
||
| 223 | * parent::initialize() as some important stuff happens here. |
||
| 224 | * If extending this method to add properties then you should |
||
| 225 | * call parent::initialize() after adding any properties. |
||
| 226 | */ |
||
| 227 | protected function initialize() |
||
| 228 | { |
||
| 229 | // load the driver |
||
| 230 | static::getDriver(); |
||
| 231 | |||
| 232 | // add in the default ID property |
||
| 233 | if (static::$ids == [self::DEFAULT_ID_PROPERTY] && !isset(static::$properties[self::DEFAULT_ID_PROPERTY])) { |
||
| 234 | static::$properties[self::DEFAULT_ID_PROPERTY] = self::$defaultIDProperty; |
||
| 235 | } |
||
| 236 | |||
| 237 | // generates created_at and updated_at timestamps |
||
| 238 | if (property_exists($this, 'autoTimestamps')) { |
||
| 239 | $this->installAutoTimestamps(); |
||
| 240 | } |
||
| 241 | |||
| 242 | // generates deleted_at timestamps |
||
| 243 | if (property_exists($this, 'softDelete')) { |
||
| 244 | $this->installSoftDelete(); |
||
| 245 | } |
||
| 246 | |||
| 247 | // fill in each property by extending the property |
||
| 248 | // definition base |
||
| 249 | foreach (static::$properties as &$property) { |
||
| 250 | $property = array_replace(self::$propertyDefinitionBase, $property); |
||
| 251 | } |
||
| 252 | |||
| 253 | // order the properties array by name for consistency |
||
| 254 | // since it is constructed in a random order |
||
| 255 | ksort(static::$properties); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Installs the `created_at` and `updated_at` properties. |
||
| 260 | */ |
||
| 261 | private function installAutoTimestamps() |
||
| 262 | { |
||
| 263 | static::$properties = array_replace(self::$timestampProperties, static::$properties); |
||
| 264 | |||
| 265 | self::creating(function (ModelEvent $event) { |
||
| 266 | $model = $event->getModel(); |
||
| 267 | $model->created_at = time(); |
||
|
|
|||
| 268 | $model->updated_at = time(); |
||
| 269 | }); |
||
| 270 | |||
| 271 | self::updating(function (ModelEvent $event) { |
||
| 272 | $event->getModel()->updated_at = time(); |
||
| 273 | }); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Installs the `deleted_at` properties. |
||
| 278 | */ |
||
| 279 | private function installSoftDelete() |
||
| 280 | { |
||
| 281 | static::$properties = array_replace(self::$softDeleteProperties, static::$properties); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Parses the given ID, which can be a single or composite primary key. |
||
| 286 | * |
||
| 287 | * @param mixed $id |
||
| 288 | */ |
||
| 289 | private function parseId($id) |
||
| 290 | { |
||
| 291 | if (is_array($id)) { |
||
| 292 | // A model can be supplied as a primary key |
||
| 293 | foreach ($id as &$el) { |
||
| 294 | if ($el instanceof self) { |
||
| 295 | $el = $el->id(); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | // The IDs come in as the same order as ::$ids. |
||
| 300 | // We need to match up the elements on that |
||
| 301 | // input into a key-value map for each ID property. |
||
| 302 | $ids = []; |
||
| 303 | $idQueue = array_reverse($id); |
||
| 304 | foreach (static::$ids as $k => $f) { |
||
| 305 | // type cast |
||
| 306 | if (count($idQueue) > 0) { |
||
| 307 | $idProperty = static::getProperty($f); |
||
| 308 | $ids[$f] = static::cast($idProperty, array_pop($idQueue)); |
||
| 309 | } else { |
||
| 310 | $ids[$f] = false; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | $this->_id = implode(',', $id); |
||
| 315 | $this->_ids = $ids; |
||
| 316 | } elseif ($id instanceof self) { |
||
| 317 | // A model can be supplied as a primary key |
||
| 318 | $this->_id = $id->id(); |
||
| 319 | $this->_ids = $id->ids(); |
||
| 320 | } else { |
||
| 321 | // type cast the single primary key |
||
| 322 | $idName = static::$ids[0]; |
||
| 323 | if ($id !== false) { |
||
| 324 | $idProperty = static::getProperty($idName); |
||
| 325 | $id = static::cast($idProperty, $id); |
||
| 326 | } |
||
| 327 | |||
| 328 | $this->_id = $id; |
||
| 329 | $this->_ids = [$idName => $id]; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Sets the driver for all models. |
||
| 335 | * |
||
| 336 | * @param DriverInterface $driver |
||
| 337 | */ |
||
| 338 | public static function setDriver(DriverInterface $driver) |
||
| 339 | { |
||
| 340 | self::$driver = $driver; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Gets the driver for all models. |
||
| 345 | * |
||
| 346 | * @return DriverInterface |
||
| 347 | * |
||
| 348 | * @throws DriverMissingException when a driver has not been set yet |
||
| 349 | */ |
||
| 350 | public static function getDriver() |
||
| 351 | { |
||
| 352 | if (!self::$driver) { |
||
| 353 | throw new DriverMissingException('A model driver has not been set yet.'); |
||
| 354 | } |
||
| 355 | |||
| 356 | return self::$driver; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Clears the driver for all models. |
||
| 361 | */ |
||
| 362 | public static function clearDriver() |
||
| 363 | { |
||
| 364 | self::$driver = null; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Gets the name of the model, i.e. User. |
||
| 369 | * |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | public static function modelName() |
||
| 373 | { |
||
| 374 | // strip namespacing |
||
| 375 | $paths = explode('\\', get_called_class()); |
||
| 376 | |||
| 377 | return end($paths); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Gets the model ID. |
||
| 382 | * |
||
| 383 | * @return string|number|false ID |
||
| 384 | */ |
||
| 385 | public function id() |
||
| 386 | { |
||
| 387 | return $this->_id; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Gets a key-value map of the model ID. |
||
| 392 | * |
||
| 393 | * @return array ID map |
||
| 394 | */ |
||
| 395 | public function ids() |
||
| 396 | { |
||
| 397 | return $this->_ids; |
||
| 398 | } |
||
| 399 | |||
| 400 | ///////////////////////////// |
||
| 401 | // Magic Methods |
||
| 402 | ///////////////////////////// |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Converts the model into a string. |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public function __toString() |
||
| 410 | { |
||
| 411 | $values = array_merge($this->_values, $this->_unsaved, $this->_ids); |
||
| 412 | ksort($values); |
||
| 413 | |||
| 414 | return get_called_class().'('.json_encode($values, JSON_PRETTY_PRINT).')'; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Shortcut to a get() call for a given property. |
||
| 419 | * |
||
| 420 | * @param string $name |
||
| 421 | * |
||
| 422 | * @return mixed |
||
| 423 | */ |
||
| 424 | public function __get($name) |
||
| 425 | { |
||
| 426 | $result = $this->get([$name]); |
||
| 427 | |||
| 428 | return reset($result); |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Sets an unsaved value. |
||
| 433 | * |
||
| 434 | * @param string $name |
||
| 435 | * @param mixed $value |
||
| 436 | */ |
||
| 437 | public function __set($name, $value) |
||
| 438 | { |
||
| 439 | // if changing property, remove relation model |
||
| 440 | if (isset($this->_relationships[$name])) { |
||
| 441 | unset($this->_relationships[$name]); |
||
| 442 | } |
||
| 443 | |||
| 444 | // call any mutators |
||
| 445 | $mutator = self::getMutator($name); |
||
| 446 | if ($mutator) { |
||
| 447 | $this->_unsaved[$name] = $this->$mutator($value); |
||
| 448 | } else { |
||
| 449 | $this->_unsaved[$name] = $value; |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Checks if an unsaved value or property exists by this name. |
||
| 455 | * |
||
| 456 | * @param string $name |
||
| 457 | * |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public function __isset($name) |
||
| 461 | { |
||
| 462 | return array_key_exists($name, $this->_unsaved) || static::hasProperty($name); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Unsets an unsaved value. |
||
| 467 | * |
||
| 468 | * @param string $name |
||
| 469 | */ |
||
| 470 | public function __unset($name) |
||
| 471 | { |
||
| 472 | if (array_key_exists($name, $this->_unsaved)) { |
||
| 473 | // if changing property, remove relation model |
||
| 474 | if (isset($this->_relationships[$name])) { |
||
| 475 | unset($this->_relationships[$name]); |
||
| 476 | } |
||
| 477 | |||
| 478 | unset($this->_unsaved[$name]); |
||
| 479 | } |
||
| 480 | } |
||
| 481 | |||
| 482 | ///////////////////////////// |
||
| 483 | // ArrayAccess Interface |
||
| 484 | ///////////////////////////// |
||
| 485 | |||
| 486 | public function offsetExists($offset) |
||
| 487 | { |
||
| 488 | return isset($this->$offset); |
||
| 489 | } |
||
| 490 | |||
| 491 | public function offsetGet($offset) |
||
| 492 | { |
||
| 493 | return $this->$offset; |
||
| 494 | } |
||
| 495 | |||
| 496 | public function offsetSet($offset, $value) |
||
| 497 | { |
||
| 498 | $this->$offset = $value; |
||
| 499 | } |
||
| 500 | |||
| 501 | public function offsetUnset($offset) |
||
| 502 | { |
||
| 503 | unset($this->$offset); |
||
| 504 | } |
||
| 505 | |||
| 506 | public static function __callStatic($name, $parameters) |
||
| 507 | { |
||
| 508 | // Any calls to unkown static methods should be deferred to |
||
| 509 | // the query. This allows calls like User::where() |
||
| 510 | // to replace User::query()->where(). |
||
| 511 | return call_user_func_array([static::query(), $name], $parameters); |
||
| 512 | } |
||
| 513 | |||
| 514 | ///////////////////////////// |
||
| 515 | // Property Definitions |
||
| 516 | ///////////////////////////// |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Gets all the property definitions for the model. |
||
| 520 | * |
||
| 521 | * @return array key-value map of properties |
||
| 522 | */ |
||
| 523 | public static function getProperties() |
||
| 524 | { |
||
| 525 | return static::$properties; |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Gets a property defition for the model. |
||
| 530 | * |
||
| 531 | * @param string $property property to lookup |
||
| 532 | * |
||
| 533 | * @return array|null property |
||
| 534 | */ |
||
| 535 | public static function getProperty($property) |
||
| 536 | { |
||
| 537 | return array_value(static::$properties, $property); |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Gets the names of the model ID properties. |
||
| 542 | * |
||
| 543 | * @return array |
||
| 544 | */ |
||
| 545 | public static function getIDProperties() |
||
| 546 | { |
||
| 547 | return static::$ids; |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Checks if the model has a property. |
||
| 552 | * |
||
| 553 | * @param string $property property |
||
| 554 | * |
||
| 555 | * @return bool has property |
||
| 556 | */ |
||
| 557 | public static function hasProperty($property) |
||
| 558 | { |
||
| 559 | return isset(static::$properties[$property]); |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Gets the mutator method name for a given proeprty name. |
||
| 564 | * Looks for methods in the form of `setPropertyValue`. |
||
| 565 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 566 | * |
||
| 567 | * @param string $property property |
||
| 568 | * |
||
| 569 | * @return string|false method name if it exists |
||
| 570 | */ |
||
| 571 | View Code Duplication | public static function getMutator($property) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * Gets the accessor method name for a given proeprty name. |
||
| 592 | * Looks for methods in the form of `getPropertyValue`. |
||
| 593 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 594 | * |
||
| 595 | * @param string $property property |
||
| 596 | * |
||
| 597 | * @return string|false method name if it exists |
||
| 598 | */ |
||
| 599 | View Code Duplication | public static function getAccessor($property) |
|
| 617 | |||
| 618 | /** |
||
| 619 | * Marshals a value for a given property from storage. |
||
| 620 | * |
||
| 621 | * @param array $property |
||
| 622 | * @param mixed $value |
||
| 623 | * |
||
| 624 | * @return mixed type-casted value |
||
| 625 | */ |
||
| 626 | public static function cast(array $property, $value) |
||
| 627 | { |
||
| 628 | if ($value === null) { |
||
| 629 | return; |
||
| 630 | } |
||
| 631 | |||
| 632 | // handle empty strings as null |
||
| 633 | if ($property['null'] && $value == '') { |
||
| 634 | return; |
||
| 635 | } |
||
| 636 | |||
| 637 | $type = array_value($property, 'type'); |
||
| 638 | $m = 'to_'.$type; |
||
| 639 | |||
| 640 | if (!method_exists(Property::class, $m)) { |
||
| 641 | return $value; |
||
| 642 | } |
||
| 643 | |||
| 646 | |||
| 647 | ///////////////////////////// |
||
| 648 | // CRUD Operations |
||
| 649 | ///////////////////////////// |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Gets the tablename for storing this model. |
||
| 653 | * |
||
| 654 | * @return string |
||
| 655 | */ |
||
| 656 | public function getTablename() |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Gets the ID of the connection in the connection manager |
||
| 665 | * that stores this model. |
||
| 666 | * |
||
| 667 | * @return string|false |
||
| 668 | */ |
||
| 669 | public function getConnection() |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Saves the model. |
||
| 676 | * |
||
| 677 | * @return bool true when the operation was successful |
||
| 678 | */ |
||
| 679 | public function save() |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Saves the model. Throws an exception when the operation fails. |
||
| 690 | * |
||
| 691 | * @throws ModelException when the model cannot be saved |
||
| 692 | */ |
||
| 693 | public function saveOrFail() |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Creates a new model. |
||
| 707 | * |
||
| 708 | * @param array $data optional key-value properties to set |
||
| 709 | * |
||
| 710 | * @return bool true when the operation was successful |
||
| 711 | * |
||
| 712 | * @throws BadMethodCallException when called on an existing model |
||
| 713 | */ |
||
| 714 | public function create(array $data = []) |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Ignores unsaved values when fetching the next value. |
||
| 806 | * |
||
| 807 | * @return $this |
||
| 808 | */ |
||
| 809 | public function ignoreUnsaved() |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Fetches property values from the model. |
||
| 818 | * |
||
| 819 | * This method looks up values in this order: |
||
| 820 | * IDs, local cache, unsaved values, storage layer, defaults |
||
| 821 | * |
||
| 822 | * @param array $properties list of property names to fetch values of |
||
| 823 | * |
||
| 824 | * @return array |
||
| 825 | */ |
||
| 826 | public function get(array $properties) |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Gets a property value from the model. |
||
| 867 | * |
||
| 868 | * Values are looked up in this order: |
||
| 869 | * 1. unsaved values |
||
| 870 | * 2. local values |
||
| 871 | * 3. default value |
||
| 872 | * 4. null |
||
| 873 | * |
||
| 874 | * @param string $property |
||
| 875 | * @param array $values |
||
| 876 | * |
||
| 877 | * @return mixed |
||
| 878 | */ |
||
| 879 | protected function getValue($property, array $values) |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Populates a newly created model with its ID. |
||
| 899 | */ |
||
| 900 | protected function getNewID() |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Sets a collection values on the model from an untrusted input. |
||
| 923 | * |
||
| 924 | * @param array $values |
||
| 925 | * |
||
| 926 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 927 | * |
||
| 928 | * @return $this |
||
| 929 | */ |
||
| 930 | public function setValues($values) |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Converts the model to an array. |
||
| 953 | * |
||
| 954 | * @return array |
||
| 955 | */ |
||
| 956 | public function toArray() |
||
| 987 | |||
| 988 | /** |
||
| 989 | * Updates the model. |
||
| 990 | * |
||
| 991 | * @param array $data optional key-value properties to set |
||
| 992 | * |
||
| 993 | * @return bool true when the operation was successful |
||
| 994 | * |
||
| 995 | * @throws BadMethodCallException when not called on an existing model |
||
| 996 | */ |
||
| 997 | public function set(array $data = []) |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * Delete the model. |
||
| 1068 | * |
||
| 1069 | * @return bool true when the operation was successful |
||
| 1070 | */ |
||
| 1071 | public function delete() |
||
| 1110 | |||
| 1111 | /** |
||
| 1112 | * Restores a soft-deleted model. |
||
| 1113 | * |
||
| 1114 | * @return bool |
||
| 1115 | */ |
||
| 1116 | public function restore() |
||
| 1139 | |||
| 1140 | /** |
||
| 1141 | * Checks if the model has been deleted. |
||
| 1142 | * |
||
| 1143 | * @return bool |
||
| 1144 | */ |
||
| 1145 | public function isDeleted() |
||
| 1153 | |||
| 1154 | ///////////////////////////// |
||
| 1155 | // Queries |
||
| 1156 | ///////////////////////////// |
||
| 1157 | |||
| 1158 | /** |
||
| 1159 | * Generates a new query instance. |
||
| 1160 | * |
||
| 1161 | * @return Query |
||
| 1162 | */ |
||
| 1163 | public static function query() |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * Generates a new query instance that includes soft-deleted models. |
||
| 1181 | * |
||
| 1182 | * @return Query |
||
| 1183 | */ |
||
| 1184 | public static function withDeleted() |
||
| 1193 | |||
| 1194 | /** |
||
| 1195 | * Finds a single instance of a model given it's ID. |
||
| 1196 | * |
||
| 1197 | * @param mixed $id |
||
| 1198 | * |
||
| 1199 | * @return static|null |
||
| 1200 | */ |
||
| 1201 | public static function find($id) |
||
| 1218 | |||
| 1219 | /** |
||
| 1220 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1221 | * |
||
| 1222 | * @param mixed $id |
||
| 1223 | * |
||
| 1224 | * @return static |
||
| 1225 | * |
||
| 1226 | * @throws ModelNotFoundException when a model could not be found |
||
| 1227 | */ |
||
| 1228 | public static function findOrFail($id) |
||
| 1237 | |||
| 1238 | /** |
||
| 1239 | * @deprecated |
||
| 1240 | * |
||
| 1241 | * Checks if the model exists in the database |
||
| 1242 | * |
||
| 1243 | * @return bool |
||
| 1244 | */ |
||
| 1245 | public function exists() |
||
| 1249 | |||
| 1250 | /** |
||
| 1251 | * Tells if this model instance has been persisted to the data layer. |
||
| 1252 | * |
||
| 1253 | * NOTE: this does not actually perform a check with the data layer |
||
| 1254 | * |
||
| 1255 | * @return bool |
||
| 1256 | */ |
||
| 1257 | public function persisted() |
||
| 1261 | |||
| 1262 | /** |
||
| 1263 | * Loads the model from the storage layer. |
||
| 1264 | * |
||
| 1265 | * @return $this |
||
| 1266 | */ |
||
| 1267 | public function refresh() |
||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Loads values into the model. |
||
| 1287 | * |
||
| 1288 | * @param array $values values |
||
| 1289 | * |
||
| 1290 | * @return $this |
||
| 1291 | */ |
||
| 1292 | public function refreshWith(array $values) |
||
| 1307 | |||
| 1308 | /** |
||
| 1309 | * Clears the cache for this model. |
||
| 1310 | * |
||
| 1311 | * @return $this |
||
| 1312 | */ |
||
| 1313 | public function clearCache() |
||
| 1322 | |||
| 1323 | ///////////////////////////// |
||
| 1324 | // Relationships |
||
| 1325 | ///////////////////////////// |
||
| 1326 | |||
| 1327 | /** |
||
| 1328 | * @deprecated |
||
| 1329 | * |
||
| 1330 | * Gets the model for a has-one relationship |
||
| 1331 | * |
||
| 1332 | * @param string $k property |
||
| 1333 | * |
||
| 1334 | * @return Model|null |
||
| 1335 | */ |
||
| 1336 | public function relation($k) |
||
| 1351 | |||
| 1352 | /** |
||
| 1353 | * @deprecated |
||
| 1354 | * |
||
| 1355 | * Sets the model for a has-one relationship |
||
| 1356 | * |
||
| 1357 | * @param string $k |
||
| 1358 | * @param Model $model |
||
| 1359 | * |
||
| 1360 | * @return $this |
||
| 1361 | */ |
||
| 1362 | public function setRelation($k, Model $model) |
||
| 1369 | |||
| 1370 | /** |
||
| 1371 | * Creates the parent side of a One-To-One relationship. |
||
| 1372 | * |
||
| 1373 | * @param string $model foreign model class |
||
| 1374 | * @param string $foreignKey identifying key on foreign model |
||
| 1375 | * @param string $localKey identifying key on local model |
||
| 1376 | * |
||
| 1377 | * @return Relation\Relation |
||
| 1378 | */ |
||
| 1379 | public function hasOne($model, $foreignKey = '', $localKey = '') |
||
| 1383 | |||
| 1384 | /** |
||
| 1385 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
| 1386 | * |
||
| 1387 | * @param string $model foreign model class |
||
| 1388 | * @param string $foreignKey identifying key on foreign model |
||
| 1389 | * @param string $localKey identifying key on local model |
||
| 1390 | * |
||
| 1391 | * @return Relation\Relation |
||
| 1392 | */ |
||
| 1393 | public function belongsTo($model, $foreignKey = '', $localKey = '') |
||
| 1397 | |||
| 1398 | /** |
||
| 1399 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
| 1400 | * |
||
| 1401 | * @param string $model foreign model class |
||
| 1402 | * @param string $foreignKey identifying key on foreign model |
||
| 1403 | * @param string $localKey identifying key on local model |
||
| 1404 | * |
||
| 1405 | * @return Relation\Relation |
||
| 1406 | */ |
||
| 1407 | public function hasMany($model, $foreignKey = '', $localKey = '') |
||
| 1411 | |||
| 1412 | /** |
||
| 1413 | * Creates the child side of a Many-To-Many relationship. |
||
| 1414 | * |
||
| 1415 | * @param string $model foreign model class |
||
| 1416 | * @param string $tablename pivot table name |
||
| 1417 | * @param string $foreignKey identifying key on foreign model |
||
| 1418 | * @param string $localKey identifying key on local model |
||
| 1419 | * |
||
| 1420 | * @return \Pulsar\Relation\Relation |
||
| 1421 | */ |
||
| 1422 | public function belongsToMany($model, $tablename = '', $foreignKey = '', $localKey = '') |
||
| 1426 | |||
| 1427 | ///////////////////////////// |
||
| 1428 | // Events |
||
| 1429 | ///////////////////////////// |
||
| 1430 | |||
| 1431 | /** |
||
| 1432 | * Gets the event dispatcher. |
||
| 1433 | * |
||
| 1434 | * @return EventDispatcher |
||
| 1435 | */ |
||
| 1436 | public static function getDispatcher($ignoreCache = false) |
||
| 1445 | |||
| 1446 | /** |
||
| 1447 | * Subscribes to a listener to an event. |
||
| 1448 | * |
||
| 1449 | * @param string $event event name |
||
| 1450 | * @param callable $listener |
||
| 1451 | * @param int $priority optional priority, higher #s get called first |
||
| 1452 | */ |
||
| 1453 | public static function listen($event, callable $listener, $priority = 0) |
||
| 1457 | |||
| 1458 | /** |
||
| 1459 | * Adds a listener to the model.creating and model.updating events. |
||
| 1460 | * |
||
| 1461 | * @param callable $listener |
||
| 1462 | * @param int $priority |
||
| 1463 | */ |
||
| 1464 | public static function saving(callable $listener, $priority = 0) |
||
| 1469 | |||
| 1470 | /** |
||
| 1471 | * Adds a listener to the model.created and model.updated events. |
||
| 1472 | * |
||
| 1473 | * @param callable $listener |
||
| 1474 | * @param int $priority |
||
| 1475 | */ |
||
| 1476 | public static function saved(callable $listener, $priority = 0) |
||
| 1481 | |||
| 1482 | /** |
||
| 1483 | * Adds a listener to the model.creating event. |
||
| 1484 | * |
||
| 1485 | * @param callable $listener |
||
| 1486 | * @param int $priority |
||
| 1487 | */ |
||
| 1488 | public static function creating(callable $listener, $priority = 0) |
||
| 1492 | |||
| 1493 | /** |
||
| 1494 | * Adds a listener to the model.created event. |
||
| 1495 | * |
||
| 1496 | * @param callable $listener |
||
| 1497 | * @param int $priority |
||
| 1498 | */ |
||
| 1499 | public static function created(callable $listener, $priority = 0) |
||
| 1503 | |||
| 1504 | /** |
||
| 1505 | * Adds a listener to the model.updating event. |
||
| 1506 | * |
||
| 1507 | * @param callable $listener |
||
| 1508 | * @param int $priority |
||
| 1509 | */ |
||
| 1510 | public static function updating(callable $listener, $priority = 0) |
||
| 1514 | |||
| 1515 | /** |
||
| 1516 | * Adds a listener to the model.updated event. |
||
| 1517 | * |
||
| 1518 | * @param callable $listener |
||
| 1519 | * @param int $priority |
||
| 1520 | */ |
||
| 1521 | public static function updated(callable $listener, $priority = 0) |
||
| 1525 | |||
| 1526 | /** |
||
| 1527 | * Adds a listener to the model.deleting event. |
||
| 1528 | * |
||
| 1529 | * @param callable $listener |
||
| 1530 | * @param int $priority |
||
| 1531 | */ |
||
| 1532 | public static function deleting(callable $listener, $priority = 0) |
||
| 1536 | |||
| 1537 | /** |
||
| 1538 | * Adds a listener to the model.deleted event. |
||
| 1539 | * |
||
| 1540 | * @param callable $listener |
||
| 1541 | * @param int $priority |
||
| 1542 | */ |
||
| 1543 | public static function deleted(callable $listener, $priority = 0) |
||
| 1547 | |||
| 1548 | /** |
||
| 1549 | * Dispatches the given event and checks if it was successful. |
||
| 1550 | * |
||
| 1551 | * @param string $eventName |
||
| 1552 | * |
||
| 1553 | * @return bool true if the events were successfully propagated |
||
| 1554 | */ |
||
| 1555 | private function performDispatch($eventName) |
||
| 1562 | |||
| 1563 | ///////////////////////////// |
||
| 1564 | // Validation |
||
| 1565 | ///////////////////////////// |
||
| 1566 | |||
| 1567 | /** |
||
| 1568 | * Gets the error stack for this model. |
||
| 1569 | * |
||
| 1570 | * @return Errors |
||
| 1571 | */ |
||
| 1572 | public function getErrors() |
||
| 1580 | |||
| 1581 | /** |
||
| 1582 | * Checks if the model in its current state is valid. |
||
| 1583 | * |
||
| 1584 | * @return bool |
||
| 1585 | */ |
||
| 1586 | public function valid() |
||
| 1607 | |||
| 1608 | /** |
||
| 1609 | * Validates and marshals a value to storage. |
||
| 1610 | * |
||
| 1611 | * @param array $property property definition |
||
| 1612 | * @param string $name property name |
||
| 1613 | * @param mixed $value |
||
| 1614 | * |
||
| 1615 | * @return bool |
||
| 1616 | */ |
||
| 1617 | private function filterAndValidate(array $property, $name, &$value) |
||
| 1637 | |||
| 1638 | /** |
||
| 1639 | * Validates a value for a property. |
||
| 1640 | * |
||
| 1641 | * @param array $property property definition |
||
| 1642 | * @param string $name property name |
||
| 1643 | * @param mixed $value |
||
| 1644 | * |
||
| 1645 | * @return array |
||
| 1646 | */ |
||
| 1647 | private function validateValue(array $property, $name, $value) |
||
| 1670 | |||
| 1671 | /** |
||
| 1672 | * Checks if a value is unique for a property. |
||
| 1673 | * |
||
| 1674 | * @param array $property property definition |
||
| 1675 | * @param string $name property name |
||
| 1676 | * @param mixed $value |
||
| 1677 | * |
||
| 1678 | * @return bool |
||
| 1679 | */ |
||
| 1680 | private function checkUniqueness(array $property, $name, $value) |
||
| 1695 | |||
| 1696 | /** |
||
| 1697 | * Gets the marshaled default value for a property (if set). |
||
| 1698 | * |
||
| 1699 | * @param array $property |
||
| 1700 | * |
||
| 1701 | * @return mixed |
||
| 1702 | */ |
||
| 1703 | private function getPropertyDefault(array $property) |
||
| 1707 | |||
| 1708 | /** |
||
| 1709 | * Gets the humanized name of a property. |
||
| 1710 | * |
||
| 1711 | * @param array $property property definition |
||
| 1712 | * @param string $name property name |
||
| 1713 | * |
||
| 1714 | * @return string |
||
| 1715 | */ |
||
| 1716 | private function getPropertyTitle(array $property, $name) |
||
| 1734 | } |
||
| 1735 |
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.