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 |
||
| 28 | abstract class Model implements \ArrayAccess |
||
| 29 | { |
||
| 30 | const TYPE_STRING = 'string'; |
||
| 31 | const TYPE_INTEGER = 'integer'; |
||
| 32 | const TYPE_FLOAT = 'float'; |
||
| 33 | const TYPE_BOOLEAN = 'boolean'; |
||
| 34 | const TYPE_DATE = 'date'; |
||
| 35 | const TYPE_OBJECT = 'object'; |
||
| 36 | const TYPE_ARRAY = 'array'; |
||
| 37 | |||
| 38 | const DEFAULT_ID_PROPERTY = 'id'; |
||
| 39 | |||
| 40 | const DEFAULT_DATE_FORMAT = 'U'; // unix timestamps |
||
| 41 | |||
| 42 | ///////////////////////////// |
||
| 43 | // Model visible variables |
||
| 44 | ///////////////////////////// |
||
| 45 | |||
| 46 | /** |
||
| 47 | * List of model ID property names. |
||
| 48 | * |
||
| 49 | * @staticvar array |
||
| 50 | */ |
||
| 51 | protected static $ids = [self::DEFAULT_ID_PROPERTY]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Validation rules expressed as a key-value map with |
||
| 55 | * property names as the keys. |
||
| 56 | * i.e. ['name' => 'string:2']. |
||
| 57 | * |
||
| 58 | * @staticvar array |
||
| 59 | */ |
||
| 60 | protected static $validations = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @staticvar array |
||
| 64 | */ |
||
| 65 | protected static $relationships = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @staticvar array |
||
| 69 | */ |
||
| 70 | protected static $dates = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @staticvar array |
||
| 74 | */ |
||
| 75 | protected static $dispatchers = []; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $_values = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $_unsaved = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var bool |
||
| 89 | */ |
||
| 90 | protected $_persisted = false; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var Errors |
||
| 94 | */ |
||
| 95 | protected $_errors; |
||
| 96 | |||
| 97 | ///////////////////////////// |
||
| 98 | // Base model variables |
||
| 99 | ///////////////////////////// |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @staticvar array |
||
| 103 | */ |
||
| 104 | private static $initialized = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @staticvar AdapterInterface |
||
| 108 | */ |
||
| 109 | private static $adapter; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @staticvar Locale |
||
| 113 | */ |
||
| 114 | private static $locale; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @staticvar array |
||
| 118 | */ |
||
| 119 | private static $tablenames = []; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @staticvar array |
||
| 123 | */ |
||
| 124 | private static $accessors = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @staticvar array |
||
| 128 | */ |
||
| 129 | private static $mutators = []; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var bool |
||
| 133 | */ |
||
| 134 | private $_ignoreUnsaved; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Creates a new model object. |
||
| 138 | * |
||
| 139 | * @param array $values values to fill model with |
||
| 140 | */ |
||
| 141 | public function __construct(array $values = []) |
||
| 142 | { |
||
| 143 | foreach ($values as $k => $v) { |
||
| 144 | $this->setValue($k, $v, false); |
||
| 145 | } |
||
| 146 | |||
| 147 | // ensure the initialize function is called only once |
||
| 148 | $k = get_called_class(); |
||
| 149 | if (!isset(self::$initialized[$k])) { |
||
| 150 | $this->initialize(); |
||
| 151 | self::$initialized[$k] = true; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * The initialize() method is called once per model. It's used |
||
| 157 | * to perform any one-off tasks before the model gets |
||
| 158 | * constructed. This is a great place to add any model |
||
| 159 | * properties. When extending this method be sure to call |
||
| 160 | * parent::initialize() as some important stuff happens here. |
||
| 161 | * If extending this method to add properties then you should |
||
| 162 | * call parent::initialize() after adding any properties. |
||
| 163 | */ |
||
| 164 | protected function initialize() |
||
| 165 | { |
||
| 166 | // add in the default ID property |
||
| 167 | if (static::$ids == [self::DEFAULT_ID_PROPERTY]) { |
||
| 168 | if (property_exists($this, 'casts') && !isset(static::$casts[self::DEFAULT_ID_PROPERTY])) { |
||
| 169 | static::$casts[self::DEFAULT_ID_PROPERTY] = self::TYPE_INTEGER; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | // generates created_at and updated_at timestamps |
||
| 174 | if (property_exists($this, 'autoTimestamps')) { |
||
| 175 | $this->installAutoTimestamps(); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Installs the automatic timestamp properties, |
||
| 181 | * `created_at` and `updated_at`. |
||
| 182 | */ |
||
| 183 | private function installAutoTimestamps() |
||
| 184 | { |
||
| 185 | if (property_exists($this, 'casts')) { |
||
| 186 | static::$casts['created_at'] = self::TYPE_DATE; |
||
| 187 | static::$casts['updated_at'] = self::TYPE_DATE; |
||
| 188 | } |
||
| 189 | |||
| 190 | self::creating(function (ModelEvent $event) { |
||
| 191 | $model = $event->getModel(); |
||
| 192 | $model->created_at = Carbon::now(); |
||
|
|
|||
| 193 | $model->updated_at = Carbon::now(); |
||
| 194 | }); |
||
| 195 | |||
| 196 | self::updating(function (ModelEvent $event) { |
||
| 197 | $event->getModel()->updated_at = Carbon::now(); |
||
| 198 | }); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Sets the adapter for all models. |
||
| 203 | * |
||
| 204 | * @param AdapterInterface $adapter |
||
| 205 | */ |
||
| 206 | public static function setAdapter(AdapterInterface $adapter) |
||
| 207 | { |
||
| 208 | self::$adapter = $adapter; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Gets the adapter for all models. |
||
| 213 | * |
||
| 214 | * @return AdapterInterface |
||
| 215 | * |
||
| 216 | * @throws AdapterMissingException |
||
| 217 | */ |
||
| 218 | public static function getAdapter() |
||
| 219 | { |
||
| 220 | if (!self::$adapter) { |
||
| 221 | throw new AdapterMissingException('A model adapter has not been set yet.'); |
||
| 222 | } |
||
| 223 | |||
| 224 | return self::$adapter; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Clears the adapter for all models. |
||
| 229 | */ |
||
| 230 | public static function clearAdapter() |
||
| 231 | { |
||
| 232 | self::$adapter = null; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Sets the locale instance for all models. |
||
| 237 | * |
||
| 238 | * @param Locale $locale |
||
| 239 | */ |
||
| 240 | public static function setLocale(Locale $locale) |
||
| 241 | { |
||
| 242 | self::$locale = $locale; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Gets the locale instance for all models. |
||
| 247 | * |
||
| 248 | * @return Locale |
||
| 249 | */ |
||
| 250 | public static function getLocale() |
||
| 251 | { |
||
| 252 | return self::$locale; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Clears the locale for all models. |
||
| 257 | */ |
||
| 258 | public static function clearLocale() |
||
| 259 | { |
||
| 260 | self::$locale = null; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Gets the name of the model without namespacing. |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public static function modelName() |
||
| 269 | { |
||
| 270 | return explode('\\', get_called_class())[0]; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Gets the table name of the model. |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getTablename() |
||
| 279 | { |
||
| 280 | $name = static::modelName(); |
||
| 281 | if (!isset(self::$tablenames[$name])) { |
||
| 282 | $inflector = Inflector::get(); |
||
| 283 | |||
| 284 | self::$tablenames[$name] = $inflector->camelize($inflector->pluralize($name)); |
||
| 285 | } |
||
| 286 | |||
| 287 | return self::$tablenames[$name]; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Gets the model ID. |
||
| 292 | * |
||
| 293 | * @return string|number|null ID |
||
| 294 | */ |
||
| 295 | public function id() |
||
| 296 | { |
||
| 297 | $ids = $this->ids(); |
||
| 298 | |||
| 299 | // if a single ID then return it |
||
| 300 | if (count($ids) === 1) { |
||
| 301 | return reset($ids); |
||
| 302 | } |
||
| 303 | |||
| 304 | // if multiple IDs then return a comma-separated list |
||
| 305 | return implode(',', $ids); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Gets a key-value map of the model ID. |
||
| 310 | * |
||
| 311 | * @return array ID map |
||
| 312 | */ |
||
| 313 | public function ids() |
||
| 314 | { |
||
| 315 | return $this->getValues(static::$ids); |
||
| 316 | } |
||
| 317 | |||
| 318 | ///////////////////////////// |
||
| 319 | // Magic Methods |
||
| 320 | ///////////////////////////// |
||
| 321 | |||
| 322 | public function __toString() |
||
| 323 | { |
||
| 324 | return get_called_class().'('.$this->id().')'; |
||
| 325 | } |
||
| 326 | |||
| 327 | public function __get($name) |
||
| 328 | { |
||
| 329 | $value = $this->getValue($name); |
||
| 330 | $this->_ignoreUnsaved = false; |
||
| 331 | |||
| 332 | return $value; |
||
| 333 | } |
||
| 334 | |||
| 335 | public function __set($name, $value) |
||
| 339 | |||
| 340 | public function __isset($name) |
||
| 341 | { |
||
| 342 | return array_key_exists($name, $this->_unsaved) || $this->hasProperty($name); |
||
| 343 | } |
||
| 344 | |||
| 345 | public function __unset($name) |
||
| 346 | { |
||
| 347 | if (static::isRelationship($name)) { |
||
| 348 | throw new BadMethodCallException("Cannot unset the `$name` property because it is a relationship"); |
||
| 349 | } |
||
| 350 | |||
| 351 | if (array_key_exists($name, $this->_unsaved)) { |
||
| 352 | unset($this->_unsaved[$name]); |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | public static function __callStatic($name, $parameters) |
||
| 357 | { |
||
| 358 | // Any calls to unkown static methods should be deferred to |
||
| 359 | // the query. This allows calls like User::where() |
||
| 360 | // to replace User::query()->where(). |
||
| 361 | return call_user_func_array([static::query(), $name], $parameters); |
||
| 362 | } |
||
| 363 | |||
| 364 | ///////////////////////////// |
||
| 365 | // ArrayAccess Interface |
||
| 366 | ///////////////////////////// |
||
| 367 | |||
| 368 | public function offsetExists($offset) |
||
| 372 | |||
| 373 | public function offsetGet($offset) |
||
| 377 | |||
| 378 | public function offsetSet($offset, $value) |
||
| 382 | |||
| 383 | public function offsetUnset($offset) |
||
| 387 | |||
| 388 | ///////////////////////////// |
||
| 389 | // Property Definitions |
||
| 390 | ///////////////////////////// |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Gets the names of the model ID properties. |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | public static function getIdProperties() |
||
| 398 | { |
||
| 399 | return static::$ids; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Builds an existing model instance given a single ID value or |
||
| 404 | * ordered array of ID values. |
||
| 405 | * |
||
| 406 | * @param mixed $id |
||
| 407 | * |
||
| 408 | * @return Model |
||
| 409 | */ |
||
| 410 | public static function buildFromId($id) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Gets the mutator method name for a given proeprty name. |
||
| 425 | * Looks for methods in the form of `setPropertyValue`. |
||
| 426 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 427 | * |
||
| 428 | * @param string $property |
||
| 429 | * |
||
| 430 | * @return string|false method name if it exists |
||
| 431 | */ |
||
| 432 | View Code Duplication | public static function getMutator($property) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Gets the accessor method name for a given proeprty name. |
||
| 453 | * Looks for methods in the form of `getPropertyValue`. |
||
| 454 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 455 | * |
||
| 456 | * @param string $property |
||
| 457 | * |
||
| 458 | * @return string|false method name if it exists |
||
| 459 | */ |
||
| 460 | View Code Duplication | public static function getAccessor($property) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Checks if a given property is a relationship. |
||
| 481 | * |
||
| 482 | * @param string $property |
||
| 483 | * |
||
| 484 | * @return bool |
||
| 485 | */ |
||
| 486 | public static function isRelationship($property) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Gets the string date format for a property. Defaults to |
||
| 493 | * UNIX timestamps. |
||
| 494 | * |
||
| 495 | * @param string $property |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public static function getDateFormat($property) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Gets the title of a property. |
||
| 510 | * |
||
| 511 | * @param string $name |
||
| 512 | * |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | public static function getPropertyTitle($name) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Gets the type cast for a property. |
||
| 530 | * |
||
| 531 | * @param string $property |
||
| 532 | * |
||
| 533 | * @return string|null |
||
| 534 | */ |
||
| 535 | public static function getPropertyType($property) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Casts a value to a given type. |
||
| 544 | * |
||
| 545 | * @param string|null $type |
||
| 546 | * @param mixed $value |
||
| 547 | * @param string $property optional property name |
||
| 548 | * |
||
| 549 | * @return mixed casted value |
||
| 550 | */ |
||
| 551 | public static function cast($type, $value, $property = null) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Gets the properties of this model. |
||
| 570 | * |
||
| 571 | * @return array |
||
| 572 | */ |
||
| 573 | public function getProperties() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Checks if the model has a property. |
||
| 581 | * |
||
| 582 | * @param string $property |
||
| 583 | * |
||
| 584 | * @return bool has property |
||
| 585 | */ |
||
| 586 | public function hasProperty($property) |
||
| 591 | |||
| 592 | ///////////////////////////// |
||
| 593 | // Values |
||
| 594 | ///////////////////////////// |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Sets an unsaved value. |
||
| 598 | * |
||
| 599 | * @param string $name |
||
| 600 | * @param mixed $value |
||
| 601 | * @param bool $unsaved when true, sets an unsaved value |
||
| 602 | * |
||
| 603 | * @throws BadMethodCallException when setting a relationship |
||
| 604 | * |
||
| 605 | * @return self |
||
| 606 | */ |
||
| 607 | public function setValue($name, $value, $unsaved = true) |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Sets a collection values on the model from an untrusted |
||
| 635 | * input. Also known as mass assignment. |
||
| 636 | * |
||
| 637 | * @param array $values |
||
| 638 | * |
||
| 639 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 640 | * |
||
| 641 | * @return self |
||
| 642 | */ |
||
| 643 | public function setValues($values) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Ignores unsaved values when fetching the next value. |
||
| 666 | * |
||
| 667 | * @return self |
||
| 668 | */ |
||
| 669 | public function ignoreUnsaved() |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Gets a list of property values from the model. |
||
| 678 | * |
||
| 679 | * @param array $properties list of property values to fetch |
||
| 680 | * |
||
| 681 | * @return array |
||
| 682 | */ |
||
| 683 | public function getValues(array $properties) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @deprecated |
||
| 697 | */ |
||
| 698 | public function get(array $properties) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Gets a property value from the model. |
||
| 705 | * |
||
| 706 | * Values are looked up in this order: |
||
| 707 | * 1. unsaved values |
||
| 708 | * 2. local values |
||
| 709 | * 3. relationships |
||
| 710 | * |
||
| 711 | * @throws InvalidArgumentException when a property was requested not present in the values |
||
| 712 | * |
||
| 713 | * @return mixed |
||
| 714 | */ |
||
| 715 | private function getValue($property) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Converts the model to an array. |
||
| 748 | * |
||
| 749 | * @return array model array |
||
| 750 | */ |
||
| 751 | public function toArray() |
||
| 782 | |||
| 783 | ///////////////////////////// |
||
| 784 | // Persistence |
||
| 785 | ///////////////////////////// |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Saves the model. |
||
| 789 | * |
||
| 790 | * @return bool |
||
| 791 | */ |
||
| 792 | public function save() |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Creates a new model. |
||
| 803 | * |
||
| 804 | * @param array $data optional key-value properties to set |
||
| 805 | * |
||
| 806 | * @return bool |
||
| 807 | * |
||
| 808 | * @throws BadMethodCallException when called on an existing model |
||
| 809 | */ |
||
| 810 | public function create(array $data = []) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Gets the IDs for a newly created model. |
||
| 852 | * |
||
| 853 | * @return string |
||
| 854 | */ |
||
| 855 | protected function getNewIds() |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Updates the model. |
||
| 873 | * |
||
| 874 | * @param array $data optional key-value properties to set |
||
| 875 | * |
||
| 876 | * @return bool |
||
| 877 | * |
||
| 878 | * @throws BadMethodCallException when not called on an existing model |
||
| 879 | */ |
||
| 880 | public function set(array $data = []) |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Delete the model. |
||
| 921 | * |
||
| 922 | * @return bool success |
||
| 923 | */ |
||
| 924 | public function delete() |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Tells if the model has been persisted. |
||
| 953 | * |
||
| 954 | * @return bool |
||
| 955 | */ |
||
| 956 | public function persisted() |
||
| 960 | |||
| 961 | /** |
||
| 962 | * Loads the model from the data layer. |
||
| 963 | * |
||
| 964 | * @return self |
||
| 965 | * |
||
| 966 | * @throws NotFoundException |
||
| 967 | */ |
||
| 968 | public function refresh() |
||
| 985 | |||
| 986 | /** |
||
| 987 | * Loads values into the model retrieved from the data layer. |
||
| 988 | * |
||
| 989 | * @param array $values values |
||
| 990 | * |
||
| 991 | * @return self |
||
| 992 | */ |
||
| 993 | public function refreshWith(array $values) |
||
| 1010 | |||
| 1011 | ///////////////////////////// |
||
| 1012 | // Queries |
||
| 1013 | ///////////////////////////// |
||
| 1014 | |||
| 1015 | /** |
||
| 1016 | * Generates a new query instance. |
||
| 1017 | * |
||
| 1018 | * @return Query |
||
| 1019 | */ |
||
| 1020 | public static function query() |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Finds a single instance of a model given it's ID. |
||
| 1032 | * |
||
| 1033 | * @param mixed $id |
||
| 1034 | * |
||
| 1035 | * @return Model|null |
||
| 1036 | */ |
||
| 1037 | public static function find($id) |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1046 | * |
||
| 1047 | * @param mixed $id |
||
| 1048 | * |
||
| 1049 | * @return Model|false |
||
| 1050 | * |
||
| 1051 | * @throws NotFoundException when a model could not be found |
||
| 1052 | */ |
||
| 1053 | public static function findOrFail($id) |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Gets the toal number of records matching an optional criteria. |
||
| 1065 | * |
||
| 1066 | * @param array $where criteria |
||
| 1067 | * |
||
| 1068 | * @return int total |
||
| 1069 | */ |
||
| 1070 | public static function totalRecords(array $where = []) |
||
| 1077 | |||
| 1078 | ///////////////////////////// |
||
| 1079 | // Relationships |
||
| 1080 | ///////////////////////////// |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Creates the parent side of a One-To-One relationship. |
||
| 1084 | * |
||
| 1085 | * @param string $model foreign model class |
||
| 1086 | * @param string $foreignKey identifying key on foreign model |
||
| 1087 | * @param string $localKey identifying key on local model |
||
| 1088 | * |
||
| 1089 | * @return \Pulsar\Relation\Relation |
||
| 1090 | */ |
||
| 1091 | public function hasOne($model, $foreignKey = '', $localKey = '') |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
| 1098 | * |
||
| 1099 | * @param string $model foreign model class |
||
| 1100 | * @param string $foreignKey identifying key on foreign model |
||
| 1101 | * @param string $localKey identifying key on local model |
||
| 1102 | * |
||
| 1103 | * @return \Pulsar\Relation\Relation |
||
| 1104 | */ |
||
| 1105 | public function belongsTo($model, $foreignKey = '', $localKey = '') |
||
| 1109 | |||
| 1110 | /** |
||
| 1111 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
| 1112 | * |
||
| 1113 | * @param string $model foreign model class |
||
| 1114 | * @param string $foreignKey identifying key on foreign model |
||
| 1115 | * @param string $localKey identifying key on local model |
||
| 1116 | * |
||
| 1117 | * @return \Pulsar\Relation\Relation |
||
| 1118 | */ |
||
| 1119 | public function hasMany($model, $foreignKey = '', $localKey = '') |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Creates the child side of a Many-To-Many relationship. |
||
| 1126 | * |
||
| 1127 | * @param string $model foreign model class |
||
| 1128 | * @param string $tablename pivot table name |
||
| 1129 | * @param string $foreignKey identifying key on foreign model |
||
| 1130 | * @param string $localKey identifying key on local model |
||
| 1131 | * |
||
| 1132 | * @return \Pulsar\Relation\Relation |
||
| 1133 | */ |
||
| 1134 | public function belongsToMany($model, $tablename = '', $foreignKey = '', $localKey = '') |
||
| 1138 | |||
| 1139 | /** |
||
| 1140 | * Loads a given relationship (if not already) and |
||
| 1141 | * returns its results. |
||
| 1142 | * |
||
| 1143 | * @param string $name |
||
| 1144 | * |
||
| 1145 | * @return mixed |
||
| 1146 | */ |
||
| 1147 | protected function loadRelationship($name) |
||
| 1156 | |||
| 1157 | ///////////////////////////// |
||
| 1158 | // Events |
||
| 1159 | ///////////////////////////// |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * Gets the event dispatcher. |
||
| 1163 | * |
||
| 1164 | * @return \Symfony\Component\EventDispatcher\EventDispatcher |
||
| 1165 | */ |
||
| 1166 | public static function getDispatcher($ignoreCache = false) |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Subscribes to a listener to an event. |
||
| 1178 | * |
||
| 1179 | * @param string $event event name |
||
| 1180 | * @param callable $listener |
||
| 1181 | * @param int $priority optional priority, higher #s get called first |
||
| 1182 | */ |
||
| 1183 | public static function listen($event, callable $listener, $priority = 0) |
||
| 1187 | |||
| 1188 | /** |
||
| 1189 | * Adds a listener to the model.creating event. |
||
| 1190 | * |
||
| 1191 | * @param callable $listener |
||
| 1192 | * @param int $priority |
||
| 1193 | */ |
||
| 1194 | public static function creating(callable $listener, $priority = 0) |
||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Adds a listener to the model.created event. |
||
| 1201 | * |
||
| 1202 | * @param callable $listener |
||
| 1203 | * @param int $priority |
||
| 1204 | */ |
||
| 1205 | public static function created(callable $listener, $priority = 0) |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Adds a listener to the model.updating event. |
||
| 1212 | * |
||
| 1213 | * @param callable $listener |
||
| 1214 | * @param int $priority |
||
| 1215 | */ |
||
| 1216 | public static function updating(callable $listener, $priority = 0) |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Adds a listener to the model.updated event. |
||
| 1223 | * |
||
| 1224 | * @param callable $listener |
||
| 1225 | * @param int $priority |
||
| 1226 | */ |
||
| 1227 | public static function updated(callable $listener, $priority = 0) |
||
| 1231 | |||
| 1232 | /** |
||
| 1233 | * Adds a listener to the model.deleting event. |
||
| 1234 | * |
||
| 1235 | * @param callable $listener |
||
| 1236 | * @param int $priority |
||
| 1237 | */ |
||
| 1238 | public static function deleting(callable $listener, $priority = 0) |
||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Adds a listener to the model.deleted event. |
||
| 1245 | * |
||
| 1246 | * @param callable $listener |
||
| 1247 | * @param int $priority |
||
| 1248 | */ |
||
| 1249 | public static function deleted(callable $listener, $priority = 0) |
||
| 1253 | |||
| 1254 | /** |
||
| 1255 | * Dispatches an event. |
||
| 1256 | * |
||
| 1257 | * @param string $eventName |
||
| 1258 | * |
||
| 1259 | * @return ModelEvent |
||
| 1260 | */ |
||
| 1261 | protected function dispatch($eventName) |
||
| 1267 | |||
| 1268 | ///////////////////////////// |
||
| 1269 | // Validation |
||
| 1270 | ///////////////////////////// |
||
| 1271 | |||
| 1272 | /** |
||
| 1273 | * Gets the error stack for this model instance. Used to |
||
| 1274 | * keep track of validation errors. |
||
| 1275 | * |
||
| 1276 | * @return Errors |
||
| 1277 | */ |
||
| 1278 | public function errors() |
||
| 1286 | |||
| 1287 | /** |
||
| 1288 | * Checks if the model is valid in its current state. |
||
| 1289 | * |
||
| 1290 | * @return bool |
||
| 1291 | */ |
||
| 1292 | public function valid() |
||
| 1309 | |||
| 1310 | /** |
||
| 1311 | * Gets a new validator instance for this model. |
||
| 1312 | * |
||
| 1313 | * @return Validator |
||
| 1314 | */ |
||
| 1315 | public function getValidator() |
||
| 1319 | } |
||
| 1320 |
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.