Skip to content

Commit

Permalink
Switch model and getKeyColumn to static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Aug 17, 2023
1 parent 1ca3472 commit 0312448
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
53 changes: 29 additions & 24 deletions src/EloquentBacking.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Model>
*/
public static function modelClass(): string
{
$basename = Str::of(static::class)->classBasename();
Expand All @@ -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
*/
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -109,16 +128,16 @@ 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());
}

protected function attributes(): array
{
return [
$this->getKeyColumn() => $this->valueToAttribute($this->value),
static::getKeyColumn() => $this->valueToAttribute($this->value),
];
}

Expand All @@ -133,33 +152,19 @@ 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
{
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);
Expand Down
2 changes: 1 addition & 1 deletion tests/SpecialEnums/VendorsByName.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function modelClass(): string
return Vendor::class;
}

protected function getKeyColumn(): string
protected static function getKeyColumn(): string
{
return 'name';
}
Expand Down

0 comments on commit 0312448

Please sign in to comment.