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 |
||
| 31 | abstract class Model implements \ArrayAccess |
||
| 32 | { |
||
| 33 | const IMMUTABLE = 0; |
||
| 34 | const MUTABLE_CREATE_ONLY = 1; |
||
| 35 | const MUTABLE = 2; |
||
| 36 | |||
| 37 | const TYPE_STRING = 'string'; |
||
| 38 | const TYPE_NUMBER = 'number'; // DEPRECATED |
||
| 39 | const TYPE_INTEGER = 'integer'; |
||
| 40 | const TYPE_FLOAT = 'float'; |
||
| 41 | const TYPE_BOOLEAN = 'boolean'; |
||
| 42 | const TYPE_DATE = 'date'; |
||
| 43 | const TYPE_OBJECT = 'object'; |
||
| 44 | const TYPE_ARRAY = 'array'; |
||
| 45 | |||
| 46 | const ERROR_REQUIRED_FIELD_MISSING = 'required_field_missing'; |
||
| 47 | const ERROR_VALIDATION_FAILED = 'validation_failed'; |
||
| 48 | const ERROR_NOT_UNIQUE = 'not_unique'; |
||
| 49 | |||
| 50 | const DEFAULT_ID_PROPERTY = 'id'; |
||
| 51 | |||
| 52 | ///////////////////////////// |
||
| 53 | // Model visible variables |
||
| 54 | ///////////////////////////// |
||
| 55 | |||
| 56 | /** |
||
| 57 | * List of model ID property names. |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected static $ids = [self::DEFAULT_ID_PROPERTY]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Property definitions expressed as a key-value map with |
||
| 65 | * property names as the keys. |
||
| 66 | * i.e. ['enabled' => ['type' => Model::TYPE_BOOLEAN]]. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected static $properties = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected static $dispatchers; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var Container |
||
| 79 | */ |
||
| 80 | protected static $globalContainer; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var ErrorStack |
||
| 84 | */ |
||
| 85 | protected static $globalErrorStack; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var number|string|false |
||
| 89 | */ |
||
| 90 | protected $_id; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $_ids; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $_values = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | protected $_unsaved = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | protected $_persisted = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | protected $_relationships = []; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var ErrorStack |
||
| 119 | */ |
||
| 120 | protected $_errors; |
||
| 121 | |||
| 122 | ///////////////////////////// |
||
| 123 | // Base model variables |
||
| 124 | ///////////////////////////// |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | private static $propertyDefinitionBase = [ |
||
| 130 | 'type' => self::TYPE_STRING, |
||
| 131 | 'mutable' => self::MUTABLE, |
||
| 132 | 'null' => false, |
||
| 133 | 'unique' => false, |
||
| 134 | 'required' => false, |
||
| 135 | ]; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | private static $defaultIDProperty = [ |
||
| 141 | 'type' => self::TYPE_INTEGER, |
||
| 142 | 'mutable' => self::IMMUTABLE, |
||
| 143 | ]; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | private static $timestampProperties = [ |
||
| 149 | 'created_at' => [ |
||
| 150 | 'type' => self::TYPE_DATE, |
||
| 151 | 'validate' => 'timestamp|db_timestamp', |
||
| 152 | ], |
||
| 153 | 'updated_at' => [ |
||
| 154 | 'type' => self::TYPE_DATE, |
||
| 155 | 'validate' => 'timestamp|db_timestamp', |
||
| 156 | ], |
||
| 157 | ]; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | private static $initialized = []; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var DriverInterface |
||
| 166 | */ |
||
| 167 | private static $driver; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var array |
||
| 171 | */ |
||
| 172 | private static $accessors = []; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var array |
||
| 176 | */ |
||
| 177 | private static $mutators = []; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var bool |
||
| 181 | */ |
||
| 182 | private $_ignoreUnsaved; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Creates a new model object. |
||
| 186 | * |
||
| 187 | * @param array|string|Model|false $id ordered array of ids or comma-separated id string |
||
| 188 | * @param array $values optional key-value map to pre-seed model |
||
| 189 | */ |
||
| 190 | public function __construct($id = false, array $values = []) |
||
| 191 | { |
||
| 192 | // initialize the model |
||
| 193 | $this->init(); |
||
| 194 | |||
| 195 | // parse the supplied model ID |
||
| 196 | if (is_array($id)) { |
||
| 197 | // A model can be supplied as a primary key |
||
| 198 | foreach ($id as &$el) { |
||
| 199 | if ($el instanceof self) { |
||
| 200 | $el = $el->id(); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | // The IDs come in as the same order as ::$ids. |
||
| 205 | // We need to match up the elements on that |
||
| 206 | // input into a key-value map for each ID property. |
||
| 207 | $ids = []; |
||
| 208 | $idQueue = array_reverse($id); |
||
| 209 | foreach (static::$ids as $k => $f) { |
||
| 210 | $ids[$f] = (count($idQueue) > 0) ? array_pop($idQueue) : false; |
||
| 211 | } |
||
| 212 | |||
| 213 | $this->_id = implode(',', $id); |
||
| 214 | $this->_ids = $ids; |
||
| 215 | } elseif ($id instanceof self) { |
||
| 216 | // A model can be supplied as a primary key |
||
| 217 | $this->_id = $id->id(); |
||
| 218 | $this->_ids = $id->ids(); |
||
| 219 | } else { |
||
| 220 | $this->_id = $id; |
||
| 221 | $idProperty = static::$ids[0]; |
||
| 222 | $this->_ids = [$idProperty => $id]; |
||
| 223 | } |
||
| 224 | |||
| 225 | // load any given values |
||
| 226 | if (count($values) > 0) { |
||
| 227 | $this->refreshWith($values); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Performs initialization on this model. |
||
| 233 | */ |
||
| 234 | private function init() |
||
| 235 | { |
||
| 236 | // ensure the initialize function is called only once |
||
| 237 | $k = get_called_class(); |
||
| 238 | if (!isset(self::$initialized[$k])) { |
||
| 239 | $this->initialize(); |
||
| 240 | self::$initialized[$k] = true; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * The initialize() method is called once per model. It's used |
||
| 246 | * to perform any one-off tasks before the model gets |
||
| 247 | * constructed. This is a great place to add any model |
||
| 248 | * properties. When extending this method be sure to call |
||
| 249 | * parent::initialize() as some important stuff happens here. |
||
| 250 | * If extending this method to add properties then you should |
||
| 251 | * call parent::initialize() after adding any properties. |
||
| 252 | */ |
||
| 253 | protected function initialize() |
||
| 254 | { |
||
| 255 | // load the driver |
||
| 256 | static::getDriver(); |
||
| 257 | |||
| 258 | // add in the default ID property |
||
| 259 | if (static::$ids == [self::DEFAULT_ID_PROPERTY] && !isset(static::$properties[self::DEFAULT_ID_PROPERTY])) { |
||
| 260 | static::$properties[self::DEFAULT_ID_PROPERTY] = self::$defaultIDProperty; |
||
| 261 | } |
||
| 262 | |||
| 263 | // generates created_at and updated_at timestamps |
||
| 264 | if (property_exists($this, 'autoTimestamps')) { |
||
| 265 | $this->installAutoTimestamps(); |
||
| 266 | } |
||
| 267 | |||
| 268 | // fill in each property by extending the property |
||
| 269 | // definition base |
||
| 270 | foreach (static::$properties as &$property) { |
||
| 271 | $property = array_replace(self::$propertyDefinitionBase, $property); |
||
| 272 | } |
||
| 273 | |||
| 274 | // order the properties array by name for consistency |
||
| 275 | // since it is constructed in a random order |
||
| 276 | ksort(static::$properties); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Installs the `created_at` and `updated_at` properties. |
||
| 281 | */ |
||
| 282 | private function installAutoTimestamps() |
||
| 283 | { |
||
| 284 | static::$properties = array_replace(self::$timestampProperties, static::$properties); |
||
| 285 | |||
| 286 | self::creating(function (ModelEvent $event) { |
||
| 287 | $model = $event->getModel(); |
||
| 288 | $model->created_at = time(); |
||
|
|
|||
| 289 | $model->updated_at = time(); |
||
| 290 | }); |
||
| 291 | |||
| 292 | self::updating(function (ModelEvent $event) { |
||
| 293 | $event->getModel()->updated_at = time(); |
||
| 294 | }); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @deprecated |
||
| 299 | * |
||
| 300 | * Injects a DI container |
||
| 301 | * |
||
| 302 | * @param Container $container |
||
| 303 | */ |
||
| 304 | public static function inject(Container $container) |
||
| 305 | { |
||
| 306 | self::$globalContainer = $container; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @deprecated |
||
| 311 | * |
||
| 312 | * Gets the DI container used for this model |
||
| 313 | * |
||
| 314 | * @return Container|null |
||
| 315 | */ |
||
| 316 | public function getApp() |
||
| 317 | { |
||
| 318 | return self::$globalContainer; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Sets the driver for all models. |
||
| 323 | * |
||
| 324 | * @param DriverInterface $driver |
||
| 325 | */ |
||
| 326 | public static function setDriver(DriverInterface $driver) |
||
| 327 | { |
||
| 328 | self::$driver = $driver; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Gets the driver for all models. |
||
| 333 | * |
||
| 334 | * @return DriverInterface |
||
| 335 | * |
||
| 336 | * @throws DriverMissingException when a driver has not been set yet |
||
| 337 | */ |
||
| 338 | public static function getDriver() |
||
| 339 | { |
||
| 340 | if (!self::$driver) { |
||
| 341 | throw new DriverMissingException('A model driver has not been set yet.'); |
||
| 342 | } |
||
| 343 | |||
| 344 | return self::$driver; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Clears the driver for all models. |
||
| 349 | */ |
||
| 350 | public static function clearDriver() |
||
| 351 | { |
||
| 352 | self::$driver = null; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Gets the name of the model, i.e. User. |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public static function modelName() |
||
| 361 | { |
||
| 362 | // strip namespacing |
||
| 363 | $paths = explode('\\', get_called_class()); |
||
| 364 | |||
| 365 | return end($paths); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Gets the model ID. |
||
| 370 | * |
||
| 371 | * @return string|number|false ID |
||
| 372 | */ |
||
| 373 | public function id() |
||
| 374 | { |
||
| 375 | return $this->_id; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Gets a key-value map of the model ID. |
||
| 380 | * |
||
| 381 | * @return array ID map |
||
| 382 | */ |
||
| 383 | public function ids() |
||
| 384 | { |
||
| 385 | return $this->_ids; |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Sets the global error stack instance. |
||
| 390 | * |
||
| 391 | * @param ErrorStack $stack |
||
| 392 | */ |
||
| 393 | public static function setErrorStack(ErrorStack $stack) |
||
| 394 | { |
||
| 395 | self::$globalErrorStack = $stack; |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Clears the global error stack instance. |
||
| 400 | */ |
||
| 401 | public static function clearErrorStack() |
||
| 402 | { |
||
| 403 | self::$globalErrorStack = null; |
||
| 404 | } |
||
| 405 | |||
| 406 | ///////////////////////////// |
||
| 407 | // Magic Methods |
||
| 408 | ///////////////////////////// |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Converts the model into a string. |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | public function __toString() |
||
| 416 | { |
||
| 417 | return get_called_class().'('.$this->_id.')'; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Shortcut to a get() call for a given property. |
||
| 422 | * |
||
| 423 | * @param string $name |
||
| 424 | * |
||
| 425 | * @return mixed |
||
| 426 | */ |
||
| 427 | public function __get($name) |
||
| 428 | { |
||
| 429 | $result = $this->get([$name]); |
||
| 430 | |||
| 431 | return reset($result); |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Sets an unsaved value. |
||
| 436 | * |
||
| 437 | * @param string $name |
||
| 438 | * @param mixed $value |
||
| 439 | */ |
||
| 440 | public function __set($name, $value) |
||
| 441 | { |
||
| 442 | // if changing property, remove relation model |
||
| 443 | if (isset($this->_relationships[$name])) { |
||
| 444 | unset($this->_relationships[$name]); |
||
| 445 | } |
||
| 446 | |||
| 447 | // call any mutators |
||
| 448 | $mutator = self::getMutator($name); |
||
| 449 | if ($mutator) { |
||
| 450 | $this->_unsaved[$name] = $this->$mutator($value); |
||
| 451 | } else { |
||
| 452 | $this->_unsaved[$name] = $value; |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Checks if an unsaved value or property exists by this name. |
||
| 458 | * |
||
| 459 | * @param string $name |
||
| 460 | * |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | public function __isset($name) |
||
| 464 | { |
||
| 465 | return array_key_exists($name, $this->_unsaved) || static::hasProperty($name); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Unsets an unsaved value. |
||
| 470 | * |
||
| 471 | * @param string $name |
||
| 472 | */ |
||
| 473 | public function __unset($name) |
||
| 474 | { |
||
| 475 | if (array_key_exists($name, $this->_unsaved)) { |
||
| 476 | // if changing property, remove relation model |
||
| 477 | if (isset($this->_relationships[$name])) { |
||
| 478 | unset($this->_relationships[$name]); |
||
| 479 | } |
||
| 480 | |||
| 481 | unset($this->_unsaved[$name]); |
||
| 482 | } |
||
| 483 | } |
||
| 484 | |||
| 485 | ///////////////////////////// |
||
| 486 | // ArrayAccess Interface |
||
| 487 | ///////////////////////////// |
||
| 488 | |||
| 489 | public function offsetExists($offset) |
||
| 490 | { |
||
| 491 | return isset($this->$offset); |
||
| 492 | } |
||
| 493 | |||
| 494 | public function offsetGet($offset) |
||
| 495 | { |
||
| 496 | return $this->$offset; |
||
| 497 | } |
||
| 498 | |||
| 499 | public function offsetSet($offset, $value) |
||
| 500 | { |
||
| 501 | $this->$offset = $value; |
||
| 502 | } |
||
| 503 | |||
| 504 | public function offsetUnset($offset) |
||
| 505 | { |
||
| 506 | unset($this->$offset); |
||
| 507 | } |
||
| 508 | |||
| 509 | public static function __callStatic($name, $parameters) |
||
| 510 | { |
||
| 511 | // Any calls to unkown static methods should be deferred to |
||
| 512 | // the query. This allows calls like User::where() |
||
| 513 | // to replace User::query()->where(). |
||
| 514 | return call_user_func_array([static::query(), $name], $parameters); |
||
| 515 | } |
||
| 516 | |||
| 517 | ///////////////////////////// |
||
| 518 | // Property Definitions |
||
| 519 | ///////////////////////////// |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Gets all the property definitions for the model. |
||
| 523 | * |
||
| 524 | * @return array key-value map of properties |
||
| 525 | */ |
||
| 526 | public static function getProperties() |
||
| 527 | { |
||
| 528 | return static::$properties; |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Gets a property defition for the model. |
||
| 533 | * |
||
| 534 | * @param string $property property to lookup |
||
| 535 | * |
||
| 536 | * @return array|null property |
||
| 537 | */ |
||
| 538 | public static function getProperty($property) |
||
| 539 | { |
||
| 540 | return array_value(static::$properties, $property); |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Gets the names of the model ID properties. |
||
| 545 | * |
||
| 546 | * @return array |
||
| 547 | */ |
||
| 548 | public static function getIDProperties() |
||
| 549 | { |
||
| 550 | return static::$ids; |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Checks if the model has a property. |
||
| 555 | * |
||
| 556 | * @param string $property property |
||
| 557 | * |
||
| 558 | * @return bool has property |
||
| 559 | */ |
||
| 560 | public static function hasProperty($property) |
||
| 561 | { |
||
| 562 | return isset(static::$properties[$property]); |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Gets the mutator method name for a given proeprty name. |
||
| 567 | * Looks for methods in the form of `setPropertyValue`. |
||
| 568 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 569 | * |
||
| 570 | * @param string $property property |
||
| 571 | * |
||
| 572 | * @return string|false method name if it exists |
||
| 573 | */ |
||
| 574 | View Code Duplication | public static function getMutator($property) |
|
| 592 | |||
| 593 | /** |
||
| 594 | * Gets the accessor method name for a given proeprty name. |
||
| 595 | * Looks for methods in the form of `getPropertyValue`. |
||
| 596 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 597 | * |
||
| 598 | * @param string $property property |
||
| 599 | * |
||
| 600 | * @return string|false method name if it exists |
||
| 601 | */ |
||
| 602 | View Code Duplication | public static function getAccessor($property) |
|
| 620 | |||
| 621 | /** |
||
| 622 | * Marshals a value for a given property from storage. |
||
| 623 | * |
||
| 624 | * @param array $property |
||
| 625 | * @param mixed $value |
||
| 626 | * |
||
| 627 | * @return mixed type-casted value |
||
| 628 | */ |
||
| 629 | public static function cast(array $property, $value) |
||
| 630 | { |
||
| 631 | if ($value === null) { |
||
| 632 | return; |
||
| 633 | } |
||
| 634 | |||
| 635 | // handle empty strings as null |
||
| 636 | if ($property['null'] && $value == '') { |
||
| 637 | return; |
||
| 638 | } |
||
| 639 | |||
| 640 | $type = array_value($property, 'type'); |
||
| 641 | $m = 'to_'.$type; |
||
| 642 | |||
| 643 | if (!method_exists(Property::class, $m)) { |
||
| 644 | return $value; |
||
| 645 | } |
||
| 646 | |||
| 647 | return Property::$m($value); |
||
| 648 | } |
||
| 649 | |||
| 650 | ///////////////////////////// |
||
| 651 | // CRUD Operations |
||
| 652 | ///////////////////////////// |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Gets the tablename for storing this model. |
||
| 656 | * |
||
| 657 | * @return string |
||
| 658 | */ |
||
| 659 | public function getTablename() |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Saves the model. |
||
| 668 | * |
||
| 669 | * @return bool true when the operation was successful |
||
| 670 | */ |
||
| 671 | public function save() |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Saves the model. Throws an exception when the operation fails. |
||
| 682 | * |
||
| 683 | * @throws ModelException when the model cannot be saved |
||
| 684 | */ |
||
| 685 | public function saveOrFail() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Creates a new model. |
||
| 694 | * |
||
| 695 | * @param array $data optional key-value properties to set |
||
| 696 | * |
||
| 697 | * @return bool true when the operation was successful |
||
| 698 | * |
||
| 699 | * @throws BadMethodCallException when called on an existing model |
||
| 700 | */ |
||
| 701 | public function create(array $data = []) |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Ignores unsaved values when fetching the next value. |
||
| 790 | * |
||
| 791 | * @return self |
||
| 792 | */ |
||
| 793 | public function ignoreUnsaved() |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Fetches property values from the model. |
||
| 802 | * |
||
| 803 | * This method looks up values in this order: |
||
| 804 | * IDs, local cache, unsaved values, storage layer, defaults |
||
| 805 | * |
||
| 806 | * @param array $properties list of property names to fetch values of |
||
| 807 | * |
||
| 808 | * @return array |
||
| 809 | */ |
||
| 810 | public function get(array $properties) |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Populates a newly created model with its ID. |
||
| 858 | */ |
||
| 859 | protected function getNewID() |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Sets a collection values on the model from an untrusted input. |
||
| 882 | * |
||
| 883 | * @param array $values |
||
| 884 | * |
||
| 885 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 886 | * |
||
| 887 | * @return self |
||
| 888 | */ |
||
| 889 | public function setValues($values) |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Converts the model to an array. |
||
| 912 | * |
||
| 913 | * @return array |
||
| 914 | */ |
||
| 915 | public function toArray() |
||
| 946 | |||
| 947 | /** |
||
| 948 | * Updates the model. |
||
| 949 | * |
||
| 950 | * @param array $data optional key-value properties to set |
||
| 951 | * |
||
| 952 | * @return bool true when the operation was successful |
||
| 953 | * |
||
| 954 | * @throws BadMethodCallException when not called on an existing model |
||
| 955 | */ |
||
| 956 | public function set(array $data = []) |
||
| 1021 | |||
| 1022 | /** |
||
| 1023 | * Delete the model. |
||
| 1024 | * |
||
| 1025 | * @return bool true when the operation was successful |
||
| 1026 | */ |
||
| 1027 | public function delete() |
||
| 1051 | |||
| 1052 | ///////////////////////////// |
||
| 1053 | // Queries |
||
| 1054 | ///////////////////////////// |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Generates a new query instance. |
||
| 1058 | * |
||
| 1059 | * @return Query |
||
| 1060 | */ |
||
| 1061 | public static function query() |
||
| 1070 | |||
| 1071 | /** |
||
| 1072 | * Finds a single instance of a model given it's ID. |
||
| 1073 | * |
||
| 1074 | * @param mixed $id |
||
| 1075 | * |
||
| 1076 | * @return Model|null |
||
| 1077 | */ |
||
| 1078 | public static function find($id) |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1091 | * |
||
| 1092 | * @param mixed $id |
||
| 1093 | * |
||
| 1094 | * @return Model |
||
| 1095 | * |
||
| 1096 | * @throws ModelNotFoundException when a model could not be found |
||
| 1097 | */ |
||
| 1098 | public static function findOrFail($id) |
||
| 1107 | |||
| 1108 | /** |
||
| 1109 | * @deprecated |
||
| 1110 | * |
||
| 1111 | * Checks if the model exists in the database |
||
| 1112 | * |
||
| 1113 | * @return bool |
||
| 1114 | */ |
||
| 1115 | public function exists() |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Tells if this model instance has been persisted to the data layer. |
||
| 1122 | * |
||
| 1123 | * NOTE: this does not actually perform a check with the data layer |
||
| 1124 | * |
||
| 1125 | * @return bool |
||
| 1126 | */ |
||
| 1127 | public function persisted() |
||
| 1131 | |||
| 1132 | /** |
||
| 1133 | * Loads the model from the storage layer. |
||
| 1134 | * |
||
| 1135 | * @return self |
||
| 1136 | */ |
||
| 1137 | public function refresh() |
||
| 1154 | |||
| 1155 | /** |
||
| 1156 | * Loads values into the model. |
||
| 1157 | * |
||
| 1158 | * @param array $values values |
||
| 1159 | * |
||
| 1160 | * @return self |
||
| 1161 | */ |
||
| 1162 | public function refreshWith(array $values) |
||
| 1169 | |||
| 1170 | /** |
||
| 1171 | * Clears the cache for this model. |
||
| 1172 | * |
||
| 1173 | * @return self |
||
| 1174 | */ |
||
| 1175 | public function clearCache() |
||
| 1183 | |||
| 1184 | ///////////////////////////// |
||
| 1185 | // Relationships |
||
| 1186 | ///////////////////////////// |
||
| 1187 | |||
| 1188 | /** |
||
| 1189 | * @deprecated |
||
| 1190 | * |
||
| 1191 | * Gets the model for a has-one relationship |
||
| 1192 | * |
||
| 1193 | * @param string $k property |
||
| 1194 | * |
||
| 1195 | * @return Model|null |
||
| 1196 | */ |
||
| 1197 | public function relation($k) |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * @deprecated |
||
| 1215 | * |
||
| 1216 | * Sets the model for a has-one relationship |
||
| 1217 | * |
||
| 1218 | * @param string $k |
||
| 1219 | * @param Model $model |
||
| 1220 | * |
||
| 1221 | * @return self |
||
| 1222 | */ |
||
| 1223 | public function setRelation($k, Model $model) |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Creates the parent side of a One-To-One relationship. |
||
| 1233 | * |
||
| 1234 | * @param string $model foreign model class |
||
| 1235 | * @param string $foreignKey identifying key on foreign model |
||
| 1236 | * @param string $localKey identifying key on local model |
||
| 1237 | * |
||
| 1238 | * @return Relation\Relation |
||
| 1239 | */ |
||
| 1240 | public function hasOne($model, $foreignKey = '', $localKey = '') |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
| 1247 | * |
||
| 1248 | * @param string $model foreign model class |
||
| 1249 | * @param string $foreignKey identifying key on foreign model |
||
| 1250 | * @param string $localKey identifying key on local model |
||
| 1251 | * |
||
| 1252 | * @return Relation\Relation |
||
| 1253 | */ |
||
| 1254 | public function belongsTo($model, $foreignKey = '', $localKey = '') |
||
| 1258 | |||
| 1259 | /** |
||
| 1260 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
| 1261 | * |
||
| 1262 | * @param string $model foreign model class |
||
| 1263 | * @param string $foreignKey identifying key on foreign model |
||
| 1264 | * @param string $localKey identifying key on local model |
||
| 1265 | * |
||
| 1266 | * @return Relation\Relation |
||
| 1267 | */ |
||
| 1268 | public function hasMany($model, $foreignKey = '', $localKey = '') |
||
| 1272 | |||
| 1273 | /** |
||
| 1274 | * Creates the child side of a Many-To-Many relationship. |
||
| 1275 | * |
||
| 1276 | * @param string $model foreign model class |
||
| 1277 | * @param string $tablename pivot table name |
||
| 1278 | * @param string $foreignKey identifying key on foreign model |
||
| 1279 | * @param string $localKey identifying key on local model |
||
| 1280 | * |
||
| 1281 | * @return \Pulsar\Relation\Relation |
||
| 1282 | */ |
||
| 1283 | public function belongsToMany($model, $tablename = '', $foreignKey = '', $localKey = '') |
||
| 1287 | |||
| 1288 | ///////////////////////////// |
||
| 1289 | // Events |
||
| 1290 | ///////////////////////////// |
||
| 1291 | |||
| 1292 | /** |
||
| 1293 | * Gets the event dispatcher. |
||
| 1294 | * |
||
| 1295 | * @return EventDispatcher |
||
| 1296 | */ |
||
| 1297 | public static function getDispatcher($ignoreCache = false) |
||
| 1306 | |||
| 1307 | /** |
||
| 1308 | * Subscribes to a listener to an event. |
||
| 1309 | * |
||
| 1310 | * @param string $event event name |
||
| 1311 | * @param callable $listener |
||
| 1312 | * @param int $priority optional priority, higher #s get called first |
||
| 1313 | */ |
||
| 1314 | public static function listen($event, callable $listener, $priority = 0) |
||
| 1318 | |||
| 1319 | /** |
||
| 1320 | * Adds a listener to the model.creating and model.updating events. |
||
| 1321 | * |
||
| 1322 | * @param callable $listener |
||
| 1323 | * @param int $priority |
||
| 1324 | */ |
||
| 1325 | public static function saving(callable $listener, $priority = 0) |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * Adds a listener to the model.created and model.updated events. |
||
| 1333 | * |
||
| 1334 | * @param callable $listener |
||
| 1335 | * @param int $priority |
||
| 1336 | */ |
||
| 1337 | public static function saved(callable $listener, $priority = 0) |
||
| 1342 | |||
| 1343 | /** |
||
| 1344 | * Adds a listener to the model.creating event. |
||
| 1345 | * |
||
| 1346 | * @param callable $listener |
||
| 1347 | * @param int $priority |
||
| 1348 | */ |
||
| 1349 | public static function creating(callable $listener, $priority = 0) |
||
| 1353 | |||
| 1354 | /** |
||
| 1355 | * Adds a listener to the model.created event. |
||
| 1356 | * |
||
| 1357 | * @param callable $listener |
||
| 1358 | * @param int $priority |
||
| 1359 | */ |
||
| 1360 | public static function created(callable $listener, $priority = 0) |
||
| 1364 | |||
| 1365 | /** |
||
| 1366 | * Adds a listener to the model.updating event. |
||
| 1367 | * |
||
| 1368 | * @param callable $listener |
||
| 1369 | * @param int $priority |
||
| 1370 | */ |
||
| 1371 | public static function updating(callable $listener, $priority = 0) |
||
| 1375 | |||
| 1376 | /** |
||
| 1377 | * Adds a listener to the model.updated event. |
||
| 1378 | * |
||
| 1379 | * @param callable $listener |
||
| 1380 | * @param int $priority |
||
| 1381 | */ |
||
| 1382 | public static function updated(callable $listener, $priority = 0) |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Adds a listener to the model.deleting event. |
||
| 1389 | * |
||
| 1390 | * @param callable $listener |
||
| 1391 | * @param int $priority |
||
| 1392 | */ |
||
| 1393 | public static function deleting(callable $listener, $priority = 0) |
||
| 1397 | |||
| 1398 | /** |
||
| 1399 | * Adds a listener to the model.deleted event. |
||
| 1400 | * |
||
| 1401 | * @param callable $listener |
||
| 1402 | * @param int $priority |
||
| 1403 | */ |
||
| 1404 | public static function deleted(callable $listener, $priority = 0) |
||
| 1408 | |||
| 1409 | /** |
||
| 1410 | * Dispatches an event. |
||
| 1411 | * |
||
| 1412 | * @param string $eventName |
||
| 1413 | * |
||
| 1414 | * @return ModelEvent |
||
| 1415 | */ |
||
| 1416 | protected function dispatch($eventName) |
||
| 1422 | |||
| 1423 | /** |
||
| 1424 | * Dispatches the given event and checks if it was successful. |
||
| 1425 | * |
||
| 1426 | * @param string $eventName |
||
| 1427 | * |
||
| 1428 | * @return bool true if the events were successfully propagated |
||
| 1429 | */ |
||
| 1430 | private function handleDispatch($eventName) |
||
| 1436 | |||
| 1437 | ///////////////////////////// |
||
| 1438 | // Validation |
||
| 1439 | ///////////////////////////// |
||
| 1440 | |||
| 1441 | /** |
||
| 1442 | * Gets the error stack for this model. |
||
| 1443 | * |
||
| 1444 | * @return ErrorStack |
||
| 1445 | */ |
||
| 1446 | public function getErrors() |
||
| 1456 | |||
| 1457 | /** |
||
| 1458 | * Validates and marshals a value to storage. |
||
| 1459 | * |
||
| 1460 | * @param array $property |
||
| 1461 | * @param string $propertyName |
||
| 1462 | * @param mixed $value |
||
| 1463 | * |
||
| 1464 | * @return bool |
||
| 1465 | */ |
||
| 1466 | private function filterAndValidate(array $property, $propertyName, &$value) |
||
| 1486 | |||
| 1487 | /** |
||
| 1488 | * Validates a value for a property. |
||
| 1489 | * |
||
| 1490 | * @param array $property |
||
| 1491 | * @param string $propertyName |
||
| 1492 | * @param mixed $value |
||
| 1493 | * |
||
| 1494 | * @return array |
||
| 1495 | */ |
||
| 1496 | private function validate(array $property, $propertyName, $value) |
||
| 1516 | |||
| 1517 | /** |
||
| 1518 | * Checks if a value is unique for a property. |
||
| 1519 | * |
||
| 1520 | * @param array $property |
||
| 1521 | * @param string $propertyName |
||
| 1522 | * @param mixed $value |
||
| 1523 | * |
||
| 1524 | * @return bool |
||
| 1525 | */ |
||
| 1526 | private function checkUniqueness(array $property, $propertyName, $value) |
||
| 1541 | |||
| 1542 | /** |
||
| 1543 | * Gets the marshaled default value for a property (if set). |
||
| 1544 | * |
||
| 1545 | * @param string $property |
||
| 1546 | * |
||
| 1547 | * @return mixed |
||
| 1548 | */ |
||
| 1549 | private function getPropertyDefault(array $property) |
||
| 1553 | } |
||
| 1554 |
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.