|
14 | 14 | use LdapRecord\Query\Builder as BaseBuilder; |
15 | 15 | use LdapRecord\Query\Model\Builder; |
16 | 16 | use LdapRecord\Support\Arr; |
| 17 | +use RuntimeException; |
17 | 18 | use Stringable; |
18 | 19 | use UnexpectedValueException; |
19 | 20 |
|
@@ -70,25 +71,30 @@ abstract class Model implements ArrayAccess, Arrayable, JsonSerializable, String |
70 | 71 | protected ?string $connection = null; |
71 | 72 |
|
72 | 73 | /** |
73 | | - * The attribute key that contains the models object GUID. |
| 74 | + * The attribute key containing the models object GUID. |
74 | 75 | */ |
75 | 76 | protected string $guidKey = 'objectguid'; |
76 | 77 |
|
77 | 78 | /** |
78 | | - * The array of booted models. |
| 79 | + * The array of the model's modifications. |
79 | 80 | */ |
80 | | - protected static array $booted = []; |
| 81 | + protected array $modifications = []; |
81 | 82 |
|
82 | 83 | /** |
83 | | - * Contains the models modifications. |
| 84 | + * The array of booted models. |
84 | 85 | */ |
85 | | - protected array $modifications = []; |
| 86 | + protected static array $booted = []; |
86 | 87 |
|
87 | 88 | /** |
88 | 89 | * The array of global scopes on the model. |
89 | 90 | */ |
90 | 91 | protected static array $globalScopes = []; |
91 | 92 |
|
| 93 | + /** |
| 94 | + * The morph model cache containing object classes and their corresponding models. |
| 95 | + */ |
| 96 | + protected static array $morphCache = []; |
| 97 | + |
92 | 98 | /** |
93 | 99 | * Constructor. |
94 | 100 | */ |
@@ -120,7 +126,7 @@ protected static function boot(): void |
120 | 126 | } |
121 | 127 |
|
122 | 128 | /** |
123 | | - * Clear the list of booted models so they will be re-booted. |
| 129 | + * Clear the list of booted models, so they will be re-booted. |
124 | 130 | */ |
125 | 131 | public static function clearBootedModels(): void |
126 | 132 | { |
@@ -568,6 +574,70 @@ public function hydrate(array $records): Collection |
568 | 574 | }); |
569 | 575 | } |
570 | 576 |
|
| 577 | + /** |
| 578 | + * Morph the model into a one of matching models using their object classes. |
| 579 | + */ |
| 580 | + public function morphInto(array $models, callable $resolver = null): Model |
| 581 | + { |
| 582 | + if (class_exists($model = $this->determineMorphModel($this, $models, $resolver))) { |
| 583 | + return $this->convert(new $model); |
| 584 | + } |
| 585 | + |
| 586 | + return $this; |
| 587 | + } |
| 588 | + |
| 589 | + /** |
| 590 | + * Morph the model into a one of matching models or throw an exception. |
| 591 | + */ |
| 592 | + public function morphIntoOrFail(array $models, callable $resolver = null): Model |
| 593 | + { |
| 594 | + $model = $this->morphInto($models, $resolver); |
| 595 | + |
| 596 | + if ($model instanceof $this) { |
| 597 | + throw new RuntimeException( |
| 598 | + 'The model could not be morphed into any of the given models.' |
| 599 | + ); |
| 600 | + } |
| 601 | + |
| 602 | + return $model; |
| 603 | + } |
| 604 | + |
| 605 | + /** |
| 606 | + * Determine the model to morph into from the given models. |
| 607 | + * |
| 608 | + * @return class-string|bool |
| 609 | + */ |
| 610 | + protected function determineMorphModel(Model $model, array $models, callable $resolver = null): string|bool |
| 611 | + { |
| 612 | + $morphModelMap = []; |
| 613 | + |
| 614 | + foreach ($models as $modelClass) { |
| 615 | + $morphModelMap[$modelClass] = static::$morphCache[$modelClass] ??= $this->normalizeObjectClasses( |
| 616 | + $modelClass::$objectClasses |
| 617 | + ); |
| 618 | + } |
| 619 | + |
| 620 | + $objectClasses = $this->normalizeObjectClasses( |
| 621 | + $model->getObjectClasses() |
| 622 | + ); |
| 623 | + |
| 624 | + $resolver ??= function (array $objectClasses, array $morphModelMap) { |
| 625 | + return array_search($objectClasses, $morphModelMap); |
| 626 | + }; |
| 627 | + |
| 628 | + return $resolver($objectClasses, $morphModelMap); |
| 629 | + } |
| 630 | + |
| 631 | + /** |
| 632 | + * Sort and normalize the object classes. |
| 633 | + */ |
| 634 | + protected function normalizeObjectClasses(array $classes): array |
| 635 | + { |
| 636 | + sort($classes); |
| 637 | + |
| 638 | + return array_map('strtolower', $classes); |
| 639 | + } |
| 640 | + |
571 | 641 | /** |
572 | 642 | * Converts the current model into the given model. |
573 | 643 | */ |
|
0 commit comments