diff --git a/src/Models/Attributes/PrimaryKey.php b/src/Models/Attributes/PrimaryKey.php new file mode 100644 index 0000000..04dec93 --- /dev/null +++ b/src/Models/Attributes/PrimaryKey.php @@ -0,0 +1,18 @@ +applyDatabaseCustomizations(); + $this->applyPrimaryKeyCustomization(); $this->applyFillable(); $this->applyHidden(); $this->applyCasts(); @@ -46,6 +49,27 @@ private function applyDatabaseCustomizations(): void $this->timestamps = $databaseAttribute->timestamps; } + private function applyPrimaryKeyCustomization(): void + { + /** @var ReflectionAttribute|null $attribute */ + $attribute = $this->resolveSingleAttribute(PrimaryKey::class); + if (is_null($attribute)) { + return; + } + + /** @var PrimaryKey $primaryKeyAttribute */ + $primaryKeyAttribute = $attribute->newInstance(); + if (! is_null($primaryKeyAttribute->name)) { + $this->setKeyName($primaryKeyAttribute->name); + } + if (! is_null($primaryKeyAttribute->type)) { + $this->setKeyType($primaryKeyAttribute->type); + } + if (! is_null($primaryKeyAttribute->incrementing)) { + $this->setIncrementing($primaryKeyAttribute->incrementing); + } + } + private function applyFillable(): void { /** @var ReflectionAttribute|null $attribute */ @@ -63,6 +87,7 @@ private function applyFillable(): void private function applyHidden(): void { + /** @var ReflectionAttribute|null $attribute */ $attribute = $this->resolveSingleAttribute(Hidden::class); if (is_null($attribute)) { return; @@ -77,9 +102,8 @@ private function applyHidden(): void private function applyCasts(): void { - /** @var array|null $castAttributes */ $castAttributes = $this->resolveMultipleAttributes(Cast::class); - if (is_null($castAttributes)) { + if ($castAttributes->isEmpty()) { return; } @@ -100,26 +124,26 @@ private function applyCasts(): void */ private function resolveSingleAttribute(string $attributeClass): ?ReflectionAttribute { - $reflectionClass = $this->reflectionClass(); - $attributes = $reflectionClass->getAttributes($attributeClass); + $classAttributes = $this->classAttributes(); - return $attributes === [] ? null : $attributes[0]; + return $classAttributes->filter(fn (ReflectionAttribute $attribute) => $attribute->getName() === $attributeClass) + ->first(); } - private function resolveMultipleAttributes(string $attributeClass): ?array + private function resolveMultipleAttributes(string $attributeClass): Collection { - $reflectionClass = $this->reflectionClass(); - $attributes = $reflectionClass->getAttributes($attributeClass); + $classAttributes = $this->classAttributes(); - return $attributes === [] ? null : $attributes; + return $classAttributes->filter(fn (ReflectionAttribute $attribute) => $attribute->getName() === $attributeClass); } - private function reflectionClass(): ReflectionClass + private function classAttributes(): Collection { - if (is_null($this->reflectionClass)) { - $this->reflectionClass = new ReflectionClass(static::class); + if (is_null($this->classAttributes)) { + $reflectionClass = new ReflectionClass(static::class); + $this->classAttributes = Collection::make($reflectionClass->getAttributes()); } - return $this->reflectionClass; + return $this->classAttributes; } }