From 0312448791a128bb0c75311713f9e033287f5666 Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Thu, 17 Aug 2023 12:53:38 -0400 Subject: [PATCH] Switch `model` and `getKeyColumn` to static methods --- src/EloquentBacking.php | 53 +++++++++++++++------------- tests/SpecialEnums/VendorsByName.php | 2 +- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/EloquentBacking.php b/src/EloquentBacking.php index 773c293..956b784 100644 --- a/src/EloquentBacking.php +++ b/src/EloquentBacking.php @@ -18,11 +18,9 @@ trait EloquentBacking { use ForwardsCalls; - public function __call(string $name, array $arguments) - { - return $this->forwardCallTo($this->singleton(), $name, $arguments); - } - + /** + * @return class-string + */ public static function modelClass(): string { $basename = Str::of(static::class)->classBasename(); @@ -37,6 +35,27 @@ public static function modelClass(): string throw new RuntimeException("Unable to infer model name for '{$basename}' special enum (tried to find a '{$name}' model but could not)."); } + protected static function model(): Model + { + $class_name = static::modelClass(); + + return new $class_name(); + } + + protected static function getKeyColumn(): string + { + $value = static::cases()[0]->value; + + return is_int($value) + ? config('glhd-special.default_int_key_name', 'id') + : config('glhd-special.default_string_key_name', 'slug'); + } + + public function __call(string $name, array $arguments) + { + return $this->forwardCallTo($this->singleton(), $name, $arguments); + } + /** * Get a singleton instance of the matching model */ @@ -74,7 +93,7 @@ public function get(): Model */ public function fresh(): Model { - $builder = $this->model()->newQuery(); + $builder = static::model()->newQuery(); if (! $model = $builder->where($this->attributes())->first()) { if (config('glhd-special.fail_when_missing', true)) { @@ -109,8 +128,8 @@ public function getKey() public function constrain(Builder $query): Builder { $key = $query->getModel()::class === static::modelClass() - ? $this->model()->getKeyName() - : $this->model()->getForeignKey(); + ? static::model()->getKeyName() + : static::model()->getForeignKey(); return $query->where($key, '=', $this->getKey()); } @@ -118,7 +137,7 @@ public function constrain(Builder $query): Builder protected function attributes(): array { return [ - $this->getKeyColumn() => $this->valueToAttribute($this->value), + static::getKeyColumn() => $this->valueToAttribute($this->value), ]; } @@ -133,7 +152,7 @@ protected function attributesForCreation(): array protected function values(): array { - return Arr::except(ValueHelper::getValuesFor($this), [$this->getKeyColumn()]); + return Arr::except(ValueHelper::getValuesFor($this), [static::getKeyColumn()]); } protected function createWith(): array @@ -141,25 +160,11 @@ protected function createWith(): array return []; } - protected function getKeyColumn(): string - { - return is_int($this->value) - ? config('glhd-special.default_int_key_name', 'id') - : config('glhd-special.default_string_key_name', 'slug'); - } - protected function valueToAttribute($value): mixed { return $value; } - protected function model(): Model - { - $class_name = static::modelClass(); - - return new $class_name(); - } - protected function cacheKey(): string { return sprintf('glhd-special:%s:%s', static::modelClass(), $this->value); diff --git a/tests/SpecialEnums/VendorsByName.php b/tests/SpecialEnums/VendorsByName.php index 163bc4f..2b8b00b 100644 --- a/tests/SpecialEnums/VendorsByName.php +++ b/tests/SpecialEnums/VendorsByName.php @@ -18,7 +18,7 @@ public static function modelClass(): string return Vendor::class; } - protected function getKeyColumn(): string + protected static function getKeyColumn(): string { return 'name'; }