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 |
||
| 44 | abstract class Model implements ArrayAccess |
||
| 45 | { |
||
| 46 | const DEFAULT_ID_NAME = 'id'; |
||
| 47 | |||
| 48 | ///////////////////////////// |
||
| 49 | // Model visible variables |
||
| 50 | ///////////////////////////// |
||
| 51 | |||
| 52 | /** |
||
| 53 | * List of model ID property names. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected static $ids = [self::DEFAULT_ID_NAME]; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Property definitions expressed as a key-value map with |
||
| 61 | * property names as the keys. |
||
| 62 | * i.e. ['enabled' => ['type' => Type::BOOLEAN]]. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected static $properties = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $_values = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private $_unsaved = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | protected $_persisted = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $_relationships = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var AbstractRelation[] |
||
| 90 | */ |
||
| 91 | private $relationships = []; |
||
| 92 | |||
| 93 | ///////////////////////////// |
||
| 94 | // Base model variables |
||
| 95 | ///////////////////////////// |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | private static $initialized = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var DriverInterface |
||
| 104 | */ |
||
| 105 | private static $driver; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | private static $accessors = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | private static $mutators = []; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | private static $dispatchers = []; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | private $tablename; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var bool |
||
| 129 | */ |
||
| 130 | private $hasId; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var array |
||
| 134 | */ |
||
| 135 | private $idValues; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var bool |
||
| 139 | */ |
||
| 140 | private $loaded = false; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var Errors |
||
| 144 | */ |
||
| 145 | private $errors; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var bool |
||
| 149 | */ |
||
| 150 | private $ignoreUnsaved; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Creates a new model object. |
||
| 154 | * |
||
| 155 | * @param array|string|Model|false $id ordered array of ids or comma-separated id string |
||
|
|
|||
| 156 | * @param array $values optional key-value map to pre-seed model |
||
| 157 | */ |
||
| 158 | public function __construct(array $values = []) |
||
| 159 | { |
||
| 160 | // initialize the model |
||
| 161 | $this->init(); |
||
| 162 | |||
| 163 | $ids = []; |
||
| 164 | $this->hasId = true; |
||
| 165 | foreach (static::$ids as $name) { |
||
| 166 | $id = false; |
||
| 167 | if (array_key_exists($name, $values)) { |
||
| 168 | $idProperty = static::definition()->get($name); |
||
| 169 | $id = Type::cast($idProperty, $values[$name]); |
||
| 170 | } |
||
| 171 | |||
| 172 | $ids[$name] = $id; |
||
| 173 | $this->hasId = $this->hasId && $id; |
||
| 174 | } |
||
| 175 | |||
| 176 | $this->idValues = $ids; |
||
| 177 | |||
| 178 | // load any given values |
||
| 179 | if ($this->hasId && count($values) > count($ids)) { |
||
| 180 | $this->refreshWith($values); |
||
| 181 | } elseif (!$this->hasId) { |
||
| 182 | $this->_unsaved = $values; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Performs initialization on this model. |
||
| 188 | */ |
||
| 189 | private function init() |
||
| 190 | { |
||
| 191 | // ensure the initialize function is called only once |
||
| 192 | $k = static::class; |
||
| 193 | if (!isset(self::$initialized[$k])) { |
||
| 194 | $this->initialize(); |
||
| 195 | self::$initialized[$k] = true; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * The initialize() method is called once per model. This is a great |
||
| 201 | * place to install event listeners. |
||
| 202 | */ |
||
| 203 | protected function initialize() |
||
| 204 | { |
||
| 205 | if (property_exists(static::class, 'autoTimestamps')) { |
||
| 206 | self::creating(function (ModelEvent $event) { |
||
| 207 | $model = $event->getModel(); |
||
| 208 | $model->created_at = time(); |
||
| 209 | $model->updated_at = time(); |
||
| 210 | }); |
||
| 211 | |||
| 212 | self::updating(function (ModelEvent $event) { |
||
| 213 | $event->getModel()->updated_at = time(); |
||
| 214 | }); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Sets the driver for all models. |
||
| 220 | */ |
||
| 221 | public static function setDriver(DriverInterface $driver) |
||
| 222 | { |
||
| 223 | self::$driver = $driver; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Gets the driver for all models. |
||
| 228 | * |
||
| 229 | * @throws DriverMissingException when a driver has not been set yet |
||
| 230 | */ |
||
| 231 | public static function getDriver(): DriverInterface |
||
| 232 | { |
||
| 233 | if (!self::$driver) { |
||
| 234 | throw new DriverMissingException('A model driver has not been set yet.'); |
||
| 235 | } |
||
| 236 | |||
| 237 | return self::$driver; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Clears the driver for all models. |
||
| 242 | */ |
||
| 243 | public static function clearDriver() |
||
| 244 | { |
||
| 245 | self::$driver = null; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Gets the name of the model, i.e. User. |
||
| 250 | */ |
||
| 251 | public static function modelName(): string |
||
| 252 | { |
||
| 253 | // strip namespacing |
||
| 254 | $paths = explode('\\', static::class); |
||
| 255 | |||
| 256 | return end($paths); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Gets the model ID. |
||
| 261 | * |
||
| 262 | * @return string|number|false ID |
||
| 263 | */ |
||
| 264 | public function id() |
||
| 265 | { |
||
| 266 | if (!$this->hasId) { |
||
| 267 | return false; |
||
| 268 | } |
||
| 269 | |||
| 270 | if (1 == count($this->idValues)) { |
||
| 271 | return reset($this->idValues); |
||
| 272 | } |
||
| 273 | |||
| 274 | $result = []; |
||
| 275 | foreach (static::$ids as $k) { |
||
| 276 | $result[] = $this->idValues[$k]; |
||
| 277 | } |
||
| 278 | |||
| 279 | return implode(',', $result); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Gets a key-value map of the model ID. |
||
| 284 | * |
||
| 285 | * @return array ID map |
||
| 286 | */ |
||
| 287 | public function ids(): array |
||
| 288 | { |
||
| 289 | return $this->idValues; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Checks if the model has an identifier present. |
||
| 294 | * This does not indicate whether the model has been |
||
| 295 | * persisted to the database or loaded from the database. |
||
| 296 | */ |
||
| 297 | public function hasId(): bool |
||
| 298 | { |
||
| 299 | return $this->hasId; |
||
| 300 | } |
||
| 301 | |||
| 302 | ///////////////////////////// |
||
| 303 | // Magic Methods |
||
| 304 | ///////////////////////////// |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Converts the model into a string. |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function __toString() |
||
| 312 | { |
||
| 313 | $values = array_merge($this->_values, $this->_unsaved, $this->idValues); |
||
| 314 | ksort($values); |
||
| 315 | |||
| 316 | return static::class.'('.json_encode($values, JSON_PRETTY_PRINT).')'; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Shortcut to a get() call for a given property. |
||
| 321 | * |
||
| 322 | * @param string $name |
||
| 323 | * |
||
| 324 | * @return mixed |
||
| 325 | */ |
||
| 326 | public function __get($name) |
||
| 327 | { |
||
| 328 | $result = $this->get([$name]); |
||
| 329 | |||
| 330 | return reset($result); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Sets an unsaved value. |
||
| 335 | * |
||
| 336 | * @param string $name |
||
| 337 | * @param mixed $value |
||
| 338 | */ |
||
| 339 | public function __set($name, $value) |
||
| 340 | { |
||
| 341 | // if changing property, remove relation model |
||
| 342 | if (isset($this->_relationships[$name])) { |
||
| 343 | unset($this->_relationships[$name]); |
||
| 344 | } |
||
| 345 | |||
| 346 | // call any mutators |
||
| 347 | $mutator = self::getMutator($name); |
||
| 348 | if ($mutator) { |
||
| 349 | $this->_unsaved[$name] = $this->$mutator($value); |
||
| 350 | } else { |
||
| 351 | $this->_unsaved[$name] = $value; |
||
| 352 | } |
||
| 353 | |||
| 354 | // set local ID property on belongs_to relationship |
||
| 355 | $property = static::definition()->get($name); |
||
| 356 | if ($property && Relationship::BELONGS_TO == $property->getRelationshipType() && !$property->isPersisted()) { |
||
| 357 | if ($value instanceof self) { |
||
| 358 | $this->_unsaved[$property->getLocalKey()] = $value->{$property->getForeignKey()}; |
||
| 359 | } elseif (null === $value) { |
||
| 360 | $this->_unsaved[$property->getLocalKey()] = null; |
||
| 361 | } else { |
||
| 362 | throw new ModelException('The value set on the "'.$name.'" property must be a model or null.'); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Checks if an unsaved value or property exists by this name. |
||
| 369 | * |
||
| 370 | * @param string $name |
||
| 371 | * |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | public function __isset($name) |
||
| 375 | { |
||
| 376 | // isset() must return true for any value that could be returned by offsetGet |
||
| 377 | // because many callers will first check isset() to see if the value is accessible. |
||
| 378 | // This method is not supposed to only be valid for unsaved values, or properties |
||
| 379 | // that have a value. |
||
| 380 | return array_key_exists($name, $this->_unsaved) || static::definition()->has($name); |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Unsets an unsaved value. |
||
| 385 | * |
||
| 386 | * @param string $name |
||
| 387 | */ |
||
| 388 | public function __unset($name) |
||
| 389 | { |
||
| 390 | if (array_key_exists($name, $this->_unsaved)) { |
||
| 391 | // if changing property, remove relation model |
||
| 392 | if (isset($this->_relationships[$name])) { |
||
| 393 | unset($this->_relationships[$name]); |
||
| 394 | } |
||
| 395 | |||
| 396 | unset($this->_unsaved[$name]); |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | ///////////////////////////// |
||
| 401 | // ArrayAccess Interface |
||
| 402 | ///////////////////////////// |
||
| 403 | |||
| 404 | public function offsetExists($offset) |
||
| 405 | { |
||
| 406 | return isset($this->$offset); |
||
| 407 | } |
||
| 408 | |||
| 409 | public function offsetGet($offset) |
||
| 410 | { |
||
| 411 | return $this->$offset; |
||
| 412 | } |
||
| 413 | |||
| 414 | public function offsetSet($offset, $value) |
||
| 415 | { |
||
| 416 | $this->$offset = $value; |
||
| 417 | } |
||
| 418 | |||
| 419 | public function offsetUnset($offset) |
||
| 420 | { |
||
| 421 | unset($this->$offset); |
||
| 422 | } |
||
| 423 | |||
| 424 | public static function __callStatic($name, $parameters) |
||
| 425 | { |
||
| 426 | // Any calls to unkown static methods should be deferred to |
||
| 427 | // the query. This allows calls like User::where() |
||
| 428 | // to replace User::query()->where(). |
||
| 429 | return call_user_func_array([static::query(), $name], $parameters); |
||
| 430 | } |
||
| 431 | |||
| 432 | ///////////////////////////// |
||
| 433 | // Property Definitions |
||
| 434 | ///////////////////////////// |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Gets the model definition. |
||
| 438 | */ |
||
| 439 | public static function definition(): Definition |
||
| 440 | { |
||
| 441 | return DefinitionBuilder::get(static::class); |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * The buildDefinition() method is called once per model. It's used |
||
| 446 | * to generate the model definition. This is a great place to add any |
||
| 447 | * dynamic model properties. |
||
| 448 | */ |
||
| 449 | public static function buildDefinition(): Definition |
||
| 450 | { |
||
| 451 | $autoTimestamps = property_exists(static::class, 'autoTimestamps'); |
||
| 452 | $softDelete = property_exists(static::class, 'softDelete'); |
||
| 453 | |||
| 454 | return DefinitionBuilder::build(static::$properties, static::class, $autoTimestamps, $softDelete); |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Gets the names of the model ID properties. |
||
| 459 | */ |
||
| 460 | public static function getIDProperties(): array |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Gets the mutator method name for a given property name. |
||
| 467 | * Looks for methods in the form of `setPropertyValue`. |
||
| 468 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 469 | * |
||
| 470 | * @param string $property property |
||
| 471 | * |
||
| 472 | * @return string|null method name if it exists |
||
| 473 | */ |
||
| 474 | public static function getMutator(string $property): ?string |
||
| 475 | { |
||
| 476 | $class = static::class; |
||
| 477 | |||
| 478 | $k = $class.':'.$property; |
||
| 479 | if (!array_key_exists($k, self::$mutators)) { |
||
| 480 | $inflector = Inflector::get(); |
||
| 481 | $method = 'set'.$inflector->camelize($property).'Value'; |
||
| 482 | |||
| 483 | if (!method_exists($class, $method)) { |
||
| 484 | $method = null; |
||
| 485 | } |
||
| 486 | |||
| 487 | self::$mutators[$k] = $method; |
||
| 488 | } |
||
| 489 | |||
| 490 | return self::$mutators[$k]; |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Gets the accessor method name for a given property name. |
||
| 495 | * Looks for methods in the form of `getPropertyValue`. |
||
| 496 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 497 | * |
||
| 498 | * @param string $property property |
||
| 499 | * |
||
| 500 | * @return string|null method name if it exists |
||
| 501 | */ |
||
| 502 | public static function getAccessor(string $property): ?string |
||
| 503 | { |
||
| 504 | $class = static::class; |
||
| 505 | |||
| 506 | $k = $class.':'.$property; |
||
| 507 | if (!array_key_exists($k, self::$accessors)) { |
||
| 508 | $inflector = Inflector::get(); |
||
| 509 | $method = 'get'.$inflector->camelize($property).'Value'; |
||
| 510 | |||
| 511 | if (!method_exists($class, $method)) { |
||
| 512 | $method = null; |
||
| 513 | } |
||
| 514 | |||
| 515 | self::$accessors[$k] = $method; |
||
| 516 | } |
||
| 517 | |||
| 518 | return self::$accessors[$k]; |
||
| 519 | } |
||
| 520 | |||
| 521 | ///////////////////////////// |
||
| 522 | // CRUD Operations |
||
| 523 | ///////////////////////////// |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Gets the table name for storing this model. |
||
| 527 | */ |
||
| 528 | public function getTablename(): string |
||
| 529 | { |
||
| 530 | if (!$this->tablename) { |
||
| 531 | $inflector = Inflector::get(); |
||
| 532 | |||
| 533 | $this->tablename = $inflector->camelize($inflector->pluralize(static::modelName())); |
||
| 534 | } |
||
| 535 | |||
| 536 | return $this->tablename; |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Gets the ID of the connection in the connection manager |
||
| 541 | * that stores this model. |
||
| 542 | */ |
||
| 543 | public function getConnection(): ?string |
||
| 544 | { |
||
| 545 | return null; |
||
| 546 | } |
||
| 547 | |||
| 548 | protected function usesTransactions(): bool |
||
| 549 | { |
||
| 550 | return false; |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Saves the model. |
||
| 555 | * |
||
| 556 | * @return bool true when the operation was successful |
||
| 557 | */ |
||
| 558 | public function save(): bool |
||
| 559 | { |
||
| 560 | if (!$this->hasId) { |
||
| 561 | return $this->create(); |
||
| 562 | } |
||
| 563 | |||
| 564 | return $this->set(); |
||
| 565 | } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Saves the model. Throws an exception when the operation fails. |
||
| 569 | * |
||
| 570 | * @throws ModelException when the model cannot be saved |
||
| 571 | */ |
||
| 572 | public function saveOrFail() |
||
| 573 | { |
||
| 574 | if (!$this->save()) { |
||
| 575 | $msg = 'Failed to save '.static::modelName(); |
||
| 576 | if ($validationErrors = $this->getErrors()->all()) { |
||
| 577 | $msg .= ': '.implode(', ', $validationErrors); |
||
| 578 | } |
||
| 579 | |||
| 580 | throw new ModelException($msg); |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Creates a new model. |
||
| 586 | * |
||
| 587 | * @param array $data optional key-value properties to set |
||
| 588 | * |
||
| 589 | * @return bool true when the operation was successful |
||
| 590 | * |
||
| 591 | * @throws BadMethodCallException when called on an existing model |
||
| 592 | */ |
||
| 593 | public function create(array $data = []): bool |
||
| 594 | { |
||
| 595 | if ($this->hasId) { |
||
| 596 | throw new BadMethodCallException('Cannot call create() on an existing model'); |
||
| 597 | } |
||
| 598 | |||
| 599 | // mass assign values passed into create() |
||
| 600 | $this->setValues($data); |
||
| 601 | |||
| 602 | // clear any previous errors |
||
| 603 | $this->getErrors()->clear(); |
||
| 604 | |||
| 605 | // start a DB transaction if needed |
||
| 606 | $usesTransactions = $this->usesTransactions(); |
||
| 607 | if ($usesTransactions) { |
||
| 608 | self::$driver->startTransaction($this->getConnection()); |
||
| 609 | } |
||
| 610 | |||
| 611 | // dispatch the model.creating event |
||
| 612 | if (!$this->performDispatch(ModelEvent::CREATING, $usesTransactions)) { |
||
| 613 | return false; |
||
| 614 | } |
||
| 615 | |||
| 616 | $requiredProperties = []; |
||
| 617 | foreach (static::definition()->all() as $name => $property) { |
||
| 618 | // build a list of the required properties |
||
| 619 | if ($property->isRequired()) { |
||
| 620 | $requiredProperties[] = $property; |
||
| 621 | } |
||
| 622 | |||
| 623 | // add in default values |
||
| 624 | if (!array_key_exists($name, $this->_unsaved) && $property->hasDefault()) { |
||
| 625 | $this->_unsaved[$name] = $property->getDefault(); |
||
| 626 | } |
||
| 627 | } |
||
| 628 | |||
| 629 | // save any relationships |
||
| 630 | if (!$this->saveRelationships($usesTransactions)) { |
||
| 631 | return false; |
||
| 632 | } |
||
| 633 | |||
| 634 | // validate the values being saved |
||
| 635 | $validated = true; |
||
| 636 | $insertArray = []; |
||
| 637 | $preservedValues = []; |
||
| 638 | foreach ($this->_unsaved as $name => $value) { |
||
| 639 | // exclude if value does not map to a property |
||
| 640 | $property = static::definition()->get($name); |
||
| 641 | if (!$property) { |
||
| 642 | continue; |
||
| 643 | } |
||
| 644 | |||
| 645 | // check if this property is persisted to the DB |
||
| 646 | if (!$property->isPersisted()) { |
||
| 647 | $preservedValues[$name] = $value; |
||
| 648 | continue; |
||
| 649 | } |
||
| 650 | |||
| 651 | // cannot insert immutable values |
||
| 652 | // (unless using the default value) |
||
| 653 | if ($property->isImmutable() && $value !== $property->getDefault()) { |
||
| 654 | continue; |
||
| 655 | } |
||
| 656 | |||
| 657 | $validated = $validated && Validator::validateProperty($this, $property, $value); |
||
| 658 | $insertArray[$name] = $value; |
||
| 659 | } |
||
| 660 | |||
| 661 | // check for required fields |
||
| 662 | foreach ($requiredProperties as $property) { |
||
| 663 | $name = $property->getName(); |
||
| 664 | if (!isset($insertArray[$name]) && !isset($preservedValues[$name])) { |
||
| 665 | $params = [ |
||
| 666 | 'field' => $name, |
||
| 667 | 'field_name' => $property->getTitle($this), |
||
| 668 | ]; |
||
| 669 | $this->getErrors()->add('pulsar.validation.required', $params); |
||
| 670 | |||
| 671 | $validated = false; |
||
| 672 | } |
||
| 673 | } |
||
| 674 | |||
| 675 | if (!$validated) { |
||
| 676 | // when validations fail roll back any database transaction |
||
| 677 | if ($usesTransactions) { |
||
| 678 | self::$driver->rollBackTransaction($this->getConnection()); |
||
| 679 | } |
||
| 680 | |||
| 681 | return false; |
||
| 682 | } |
||
| 683 | |||
| 684 | $created = self::$driver->createModel($this, $insertArray); |
||
| 685 | |||
| 686 | if ($created) { |
||
| 687 | // determine the model's new ID |
||
| 688 | $this->getNewId(); |
||
| 689 | |||
| 690 | // store the persisted values to the in-memory cache |
||
| 691 | $this->_unsaved = []; |
||
| 692 | $hydrateValues = array_replace($this->idValues, $preservedValues); |
||
| 693 | |||
| 694 | // only type-cast the values that were converted to the database format |
||
| 695 | foreach ($insertArray as $k => $v) { |
||
| 696 | if ($property = static::definition()->get($k)) { |
||
| 697 | $hydrateValues[$k] = Type::cast($property, $v); |
||
| 698 | } else { |
||
| 699 | $hydrateValues[$k] = $v; |
||
| 700 | } |
||
| 701 | } |
||
| 702 | $this->refreshWith($hydrateValues); |
||
| 703 | |||
| 704 | // dispatch the model.created event |
||
| 705 | if (!$this->performDispatch(ModelEvent::CREATED, $usesTransactions)) { |
||
| 706 | return false; |
||
| 707 | } |
||
| 708 | } |
||
| 709 | |||
| 710 | // commit the transaction, if used |
||
| 711 | if ($usesTransactions) { |
||
| 712 | self::$driver->commitTransaction($this->getConnection()); |
||
| 713 | } |
||
| 714 | |||
| 715 | return $created; |
||
| 716 | } |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Ignores unsaved values when fetching the next value. |
||
| 720 | * |
||
| 721 | * @return $this |
||
| 722 | */ |
||
| 723 | public function ignoreUnsaved() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Fetches property values from the model. |
||
| 732 | * |
||
| 733 | * This method looks up values in this order: |
||
| 734 | * IDs, local cache, unsaved values, storage layer, defaults |
||
| 735 | * |
||
| 736 | * @param array $properties list of property names to fetch values of |
||
| 737 | */ |
||
| 738 | public function get(array $properties): array |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Gets a property value from the model. |
||
| 779 | * |
||
| 780 | * Values are looked up in this order: |
||
| 781 | * 1. unsaved values |
||
| 782 | * 2. local values |
||
| 783 | * 3. default value |
||
| 784 | * 4. null |
||
| 785 | * |
||
| 786 | * @return mixed |
||
| 787 | */ |
||
| 788 | private function getValue(string $name, array $values) |
||
| 789 | { |
||
| 790 | $value = null; |
||
| 791 | |||
| 792 | if (array_key_exists($name, $values)) { |
||
| 793 | $value = $values[$name]; |
||
| 794 | } elseif ($property = static::definition()->get($name)) { |
||
| 795 | if ($property->getRelationshipType() && !$property->isPersisted()) { |
||
| 796 | $relationship = $this->getRelationship($property); |
||
| 797 | $value = $this->_values[$name] = $relationship->getResults(); |
||
| 798 | } else { |
||
| 799 | $value = $this->_values[$name] = $property->getDefault(); |
||
| 800 | } |
||
| 801 | } |
||
| 802 | |||
| 803 | // call any accessors |
||
| 804 | if ($accessor = self::getAccessor($name)) { |
||
| 805 | $value = $this->$accessor($value); |
||
| 806 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Populates a newly created model with its ID. |
||
| 813 | */ |
||
| 814 | private function getNewId() |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Sets a collection values on the model from an untrusted input. |
||
| 838 | * |
||
| 839 | * @param array $values |
||
| 840 | * |
||
| 841 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 842 | * |
||
| 843 | * @return $this |
||
| 844 | */ |
||
| 845 | public function setValues($values) |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Converts the model to an array. |
||
| 868 | */ |
||
| 869 | public function toArray(): array |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Checks if the unsaved value for a property is present and |
||
| 915 | * is different from the original value. |
||
| 916 | * |
||
| 917 | * @property string|null $name |
||
| 918 | * @property bool $hasChanged when true, checks if the unsaved value is different from the saved value |
||
| 919 | */ |
||
| 920 | public function dirty(?string $name = null, bool $hasChanged = false): bool |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Updates the model. |
||
| 943 | * |
||
| 944 | * @param array $data optional key-value properties to set |
||
| 945 | * |
||
| 946 | * @return bool true when the operation was successful |
||
| 947 | * |
||
| 948 | * @throws BadMethodCallException when not called on an existing model |
||
| 949 | */ |
||
| 950 | public function set(array $data = []): bool |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Delete the model. |
||
| 1052 | * |
||
| 1053 | * @return bool true when the operation was successful |
||
| 1054 | */ |
||
| 1055 | public function delete(): bool |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Restores a soft-deleted model. |
||
| 1108 | */ |
||
| 1109 | public function restore(): bool |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * Checks if the model has been deleted. |
||
| 1146 | */ |
||
| 1147 | public function isDeleted(): bool |
||
| 1155 | |||
| 1156 | ///////////////////////////// |
||
| 1157 | // Queries |
||
| 1158 | ///////////////////////////// |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Generates a new query instance. |
||
| 1162 | */ |
||
| 1163 | public static function query(): Query |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * Generates a new query instance that includes soft-deleted models. |
||
| 1181 | */ |
||
| 1182 | public static function withDeleted(): Query |
||
| 1191 | |||
| 1192 | /** |
||
| 1193 | * Finds a single instance of a model given it's ID. |
||
| 1194 | * |
||
| 1195 | * @param mixed $id |
||
| 1196 | * |
||
| 1197 | * @return static|null |
||
| 1198 | */ |
||
| 1199 | public static function find($id): ?self |
||
| 1216 | |||
| 1217 | /** |
||
| 1218 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1219 | * |
||
| 1220 | * @param mixed $id |
||
| 1221 | * |
||
| 1222 | * @return static |
||
| 1223 | * |
||
| 1224 | * @throws ModelNotFoundException when a model could not be found |
||
| 1225 | */ |
||
| 1226 | public static function findOrFail($id): self |
||
| 1235 | |||
| 1236 | /** |
||
| 1237 | * Tells if this model instance has been persisted to the data layer. |
||
| 1238 | * |
||
| 1239 | * NOTE: this does not actually perform a check with the data layer |
||
| 1240 | */ |
||
| 1241 | public function persisted(): bool |
||
| 1245 | |||
| 1246 | /** |
||
| 1247 | * Loads the model from the storage layer. |
||
| 1248 | * |
||
| 1249 | * @return $this |
||
| 1250 | */ |
||
| 1251 | public function refresh() |
||
| 1275 | |||
| 1276 | /** |
||
| 1277 | * Loads values into the model. |
||
| 1278 | * |
||
| 1279 | * @param array $values values |
||
| 1280 | * |
||
| 1281 | * @return $this |
||
| 1282 | */ |
||
| 1283 | public function refreshWith(array $values) |
||
| 1291 | |||
| 1292 | /** |
||
| 1293 | * Clears the cache for this model. |
||
| 1294 | * |
||
| 1295 | * @return $this |
||
| 1296 | */ |
||
| 1297 | public function clearCache() |
||
| 1306 | |||
| 1307 | ///////////////////////////// |
||
| 1308 | // Relationships |
||
| 1309 | ///////////////////////////// |
||
| 1310 | |||
| 1311 | /** |
||
| 1312 | * Gets the relationship manager for a property. |
||
| 1313 | * |
||
| 1314 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
| 1315 | */ |
||
| 1316 | private function getRelationship(Property $property): AbstractRelation |
||
| 1325 | |||
| 1326 | /** |
||
| 1327 | * Saves any unsaved models attached through a relationship. This will only |
||
| 1328 | * save attached models that have not been saved yet. |
||
| 1329 | */ |
||
| 1330 | private function saveRelationships(bool $usesTransactions): bool |
||
| 1364 | |||
| 1365 | /** |
||
| 1366 | * This hydrates an individual property in the model. It can be a |
||
| 1367 | * scalar value or relationship. |
||
| 1368 | * |
||
| 1369 | * @internal |
||
| 1370 | * |
||
| 1371 | * @param $value |
||
| 1372 | */ |
||
| 1373 | public function hydrateValue(string $name, $value): void |
||
| 1382 | |||
| 1383 | /** |
||
| 1384 | * @deprecated |
||
| 1385 | * |
||
| 1386 | * Gets the model(s) for a relationship |
||
| 1387 | * |
||
| 1388 | * @param string $k property |
||
| 1389 | * |
||
| 1390 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
| 1391 | * |
||
| 1392 | * @return Model|array|null |
||
| 1393 | */ |
||
| 1394 | public function relation(string $k) |
||
| 1403 | |||
| 1404 | /** |
||
| 1405 | * @deprecated |
||
| 1406 | * |
||
| 1407 | * Sets the model for a one-to-one relationship (has-one or belongs-to) |
||
| 1408 | * |
||
| 1409 | * @return $this |
||
| 1410 | */ |
||
| 1411 | public function setRelation(string $k, Model $model) |
||
| 1418 | |||
| 1419 | /** |
||
| 1420 | * @deprecated |
||
| 1421 | * |
||
| 1422 | * Sets the model for a one-to-many relationship |
||
| 1423 | * |
||
| 1424 | * @return $this |
||
| 1425 | */ |
||
| 1426 | public function setRelationCollection(string $k, iterable $models) |
||
| 1432 | |||
| 1433 | /** |
||
| 1434 | * @deprecated |
||
| 1435 | * |
||
| 1436 | * Sets the model for a one-to-one relationship (has-one or belongs-to) as null |
||
| 1437 | * |
||
| 1438 | * @return $this |
||
| 1439 | */ |
||
| 1440 | public function clearRelation(string $k) |
||
| 1447 | |||
| 1448 | ///////////////////////////// |
||
| 1449 | // Events |
||
| 1450 | ///////////////////////////// |
||
| 1451 | |||
| 1452 | /** |
||
| 1453 | * Gets the event dispatcher. |
||
| 1454 | */ |
||
| 1455 | public static function getDispatcher($ignoreCache = false): EventDispatcher |
||
| 1464 | |||
| 1465 | /** |
||
| 1466 | * Subscribes to a listener to an event. |
||
| 1467 | * |
||
| 1468 | * @param string $event event name |
||
| 1469 | * @param int $priority optional priority, higher #s get called first |
||
| 1470 | */ |
||
| 1471 | public static function listen(string $event, callable $listener, int $priority = 0) |
||
| 1475 | |||
| 1476 | /** |
||
| 1477 | * Adds a listener to the model.creating and model.updating events. |
||
| 1478 | */ |
||
| 1479 | public static function saving(callable $listener, int $priority = 0) |
||
| 1484 | |||
| 1485 | /** |
||
| 1486 | * Adds a listener to the model.created and model.updated events. |
||
| 1487 | */ |
||
| 1488 | public static function saved(callable $listener, int $priority = 0) |
||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * Adds a listener to the model.creating event. |
||
| 1496 | */ |
||
| 1497 | public static function creating(callable $listener, int $priority = 0) |
||
| 1501 | |||
| 1502 | /** |
||
| 1503 | * Adds a listener to the model.created event. |
||
| 1504 | */ |
||
| 1505 | public static function created(callable $listener, int $priority = 0) |
||
| 1509 | |||
| 1510 | /** |
||
| 1511 | * Adds a listener to the model.updating event. |
||
| 1512 | */ |
||
| 1513 | public static function updating(callable $listener, int $priority = 0) |
||
| 1517 | |||
| 1518 | /** |
||
| 1519 | * Adds a listener to the model.updated event. |
||
| 1520 | */ |
||
| 1521 | public static function updated(callable $listener, int $priority = 0) |
||
| 1525 | |||
| 1526 | /** |
||
| 1527 | * Adds a listener to the model.deleting event. |
||
| 1528 | */ |
||
| 1529 | public static function deleting(callable $listener, int $priority = 0) |
||
| 1533 | |||
| 1534 | /** |
||
| 1535 | * Adds a listener to the model.deleted event. |
||
| 1536 | */ |
||
| 1537 | public static function deleted(callable $listener, int $priority = 0) |
||
| 1541 | |||
| 1542 | /** |
||
| 1543 | * Dispatches the given event and checks if it was successful. |
||
| 1544 | * |
||
| 1545 | * @return bool true if the events were successfully propagated |
||
| 1546 | */ |
||
| 1547 | private function performDispatch(string $eventName, bool $usesTransactions): bool |
||
| 1563 | |||
| 1564 | ///////////////////////////// |
||
| 1565 | // Validation |
||
| 1566 | ///////////////////////////// |
||
| 1567 | |||
| 1568 | /** |
||
| 1569 | * Gets the error stack for this model. |
||
| 1570 | */ |
||
| 1571 | public function getErrors(): Errors |
||
| 1579 | |||
| 1580 | /** |
||
| 1581 | * Checks if the model in its current state is valid. |
||
| 1582 | */ |
||
| 1583 | public function valid(): bool |
||
| 1604 | } |
||
| 1605 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.