|
3 | 3 | namespace MacropaySolutions\LaravelCrudWizard\Models; |
4 | 4 |
|
5 | 5 | use Carbon\Carbon; |
| 6 | +use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes; |
6 | 7 | use Illuminate\Database\Eloquent\Model; |
7 | 8 | use Illuminate\Support\Collection; |
8 | 9 | use Illuminate\Support\Facades\Cache; |
@@ -478,7 +479,13 @@ protected function initializeActiveRecordSegregationProperties(): void |
478 | 479 | */ |
479 | 480 | public function __call($method, $parameters) |
480 | 481 | { |
481 | | - if (\in_array(\strtolower($method), ['incrementeach', 'decrementeach'], true)) { |
| 482 | + $lowerMethod = \strtolower($method); |
| 483 | + |
| 484 | + if ($lowerMethod === 'getattributefromarray') { |
| 485 | + return $this->$method(...$parameters); |
| 486 | + } |
| 487 | + |
| 488 | + if (\in_array($lowerMethod, ['incrementeach', 'decrementeach'], true)) { |
482 | 489 | /** \MacropaySolutions\MaravelRestWizard\Models\BaseModel::incrementBulk can be used instead */ |
483 | 490 | throw new \BadMethodCallException(\sprintf( |
484 | 491 | 'Call to unscoped method %s::%s(). Use $model->newQuery()->getQuery()->%s()' . |
@@ -555,4 +562,106 @@ public function unique($key = null, $strict = false): Collection |
555 | 562 | } |
556 | 563 | }; |
557 | 564 | } |
| 565 | + |
| 566 | + /** |
| 567 | + * Get all of the current attributes on the model. |
| 568 | + * @param bool $withoutCasting |
| 569 | + * @return array |
| 570 | + */ |
| 571 | + public function getAttributes(): array |
| 572 | + { |
| 573 | + if (true !== \func_get_arg(0)) { |
| 574 | + $this->mergeAttributesFromCachedCasts(); |
| 575 | + } |
| 576 | + |
| 577 | + return $this->attributes; |
| 578 | + } |
| 579 | + |
| 580 | + /** |
| 581 | + * @inheritdoc |
| 582 | + */ |
| 583 | + public function syncOriginalAttributes($attributes): static |
| 584 | + { |
| 585 | + $attributes = is_array($attributes) ? $attributes : func_get_args(); |
| 586 | + |
| 587 | + foreach ($attributes as $attribute) { |
| 588 | + $this->original[$attribute] = $this->getAttributeFromArray($attribute); |
| 589 | + } |
| 590 | + |
| 591 | + return $this; |
| 592 | + } |
| 593 | + |
| 594 | + /** |
| 595 | + * Get an attribute from the $attributes array without transformation |
| 596 | + * @see self::getAttributeValue |
| 597 | + * |
| 598 | + * @param string $key |
| 599 | + * @return mixed |
| 600 | + */ |
| 601 | + protected function getAttributeFromArray($key): mixed |
| 602 | + { |
| 603 | + $this->mergeAttributesFromClassCasts($key); |
| 604 | + $this->mergeAttributesFromAttributeCasts($key); |
| 605 | + |
| 606 | + return $this->attributes[$key] ?? null; |
| 607 | + } |
| 608 | + |
| 609 | + /** |
| 610 | + * Merge the cast class attributes back into the model. |
| 611 | + * @param string|array $keys |
| 612 | + * @return void |
| 613 | + */ |
| 614 | + protected function mergeAttributesFromClassCasts(): void |
| 615 | + { |
| 616 | + $k = \func_get_args()[0] ?? null; |
| 617 | + |
| 618 | + $classCastCache = \is_string($k) || \is_array($k) ? |
| 619 | + \array_intersect_key($this->classCastCache, \array_flip(\array_values((array)$k))) : |
| 620 | + $this->classCastCache; |
| 621 | + |
| 622 | + foreach ($classCastCache as $key => $value) { |
| 623 | + $caster = $this->resolveCasterClass($key); |
| 624 | + |
| 625 | + $this->attributes = array_merge( |
| 626 | + $this->attributes, |
| 627 | + $caster instanceof CastsInboundAttributes |
| 628 | + ? [$key => $value] |
| 629 | + : $this->normalizeCastClassResponse($key, $caster->set($this, $key, $value, $this->attributes)) |
| 630 | + ); |
| 631 | + } |
| 632 | + } |
| 633 | + |
| 634 | + /** |
| 635 | + * Merge the cast class attributes back into the model. |
| 636 | + * @param string|array $keys |
| 637 | + * @return void |
| 638 | + */ |
| 639 | + protected function mergeAttributesFromAttributeCasts(): void |
| 640 | + { |
| 641 | + $k = \func_get_args()[0] ?? null; |
| 642 | + |
| 643 | + $attributeCastCache = \is_string($k) || \is_array($k) ? |
| 644 | + \array_intersect_key($this->attributeCastCache, \array_flip(\array_values((array)$k))) : |
| 645 | + $this->attributeCastCache; |
| 646 | + |
| 647 | + foreach ($attributeCastCache as $key => $value) { |
| 648 | + $attribute = $this->{Str::camel($key)}(); |
| 649 | + |
| 650 | + if ($attribute->get && !$attribute->set) { |
| 651 | + continue; |
| 652 | + } |
| 653 | + |
| 654 | + $callback = $attribute->set ?: function ($value) use ($key) { |
| 655 | + $this->attributes[$key] = $value; |
| 656 | + }; |
| 657 | + |
| 658 | + $this->attributes = array_merge( |
| 659 | + $this->attributes, |
| 660 | + $this->normalizeCastClassResponse( |
| 661 | + $key, |
| 662 | + $callback($value, $this->attributes) |
| 663 | + ) |
| 664 | + ); |
| 665 | + } |
| 666 | + } |
558 | 667 | } |
0 commit comments