Skip to content

Commit

Permalink
Replace trait init with fill + add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
MannikJ committed Aug 9, 2021
1 parent 69a80cc commit 531ac37
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
31 changes: 16 additions & 15 deletions src/Traits/SingleTableInheritance.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ trait SingleTableInheritance
{
public static function bootSingleTableInheritance()
{

if (static::isSubClass()) {
static::addGlobalScope('sti', function (Builder $builder) {
return $builder->sti();
});
}
}

public function initializeSingleTableInheritance($attributes = [])
public function fill(array $attributes)
{
parent::fill($attributes);

if ($this->resolveTypeViaAttributes($attributes)) {
return;
}
Expand All @@ -37,15 +38,15 @@ public static function getStiMap(): array
})->toArray();
}

public static function getStiSubTypes()
public static function getStiSubTypes(): array
{
return array_keys(static::getStiMap());
}

/**
* Recursively traverse all sti sub classes
*/
public static function getStiSubClasses()
public static function getStiSubClasses(): array
{
$classes = collect(static::getDirectStiSubClasses());
foreach (static::getDirectStiSubClasses() as $subClass) {
Expand All @@ -72,20 +73,20 @@ public static function getDirectStiSubClasses(): array
return static::$stiSubClasses;
}

public static function getTypesForScope()
public static function getTypesForScope(): array
{
return array_merge([static::resolveTypeViaClass()], static::getStiSubTypes());
}

public function resolveTypeViaAttributes($attributes = null)
public function resolveTypeViaAttributes($attributes = null): ?string
{
$attributes = $attributes ?: $this->attributes;
return ($attribute = $this->getTypeColumn())
? Arr::get($attributes, $attribute)
: null;
}

public static function resolveTypeViaClass()
public static function resolveTypeViaClass(): ?string
{
return static::isSubClass() ? static::class : null;
}
Expand All @@ -98,12 +99,12 @@ public function applyTypeCharacteristics($type)
$this->attributes[$this->getTypeColumn()] = $type;
}

public static function isSubClass()
public static function isSubClass(): bool
{
return is_subclass_of(static::class, static::getBaseModelClass());
}

public static function getBaseModelClass()
public static function getBaseModelClass(): string
{
return self::class;
}
Expand All @@ -126,12 +127,12 @@ public function handleSaved()
{
}

public function getMorphClass()
public function getMorphClass(): string
{
return self::class;
}

public function getTypeColumn($qualified = false)
public function getTypeColumn($qualified = false): string
{
$typeColumn = isset($this->typeColumn)
? $this->typeColumn
Expand All @@ -147,24 +148,24 @@ public function getTypeColumn($qualified = false)
*
* @return string
*/
public function getForeignKey()
public function getForeignKey(): string
{
return Str::snake(class_basename(self::class)) . '_' . $this->getKeyName();
}

public function getModelClassViaAttributes($attributes = [])
public function getModelClassViaAttributes($attributes = []): ?string
{
return $this->resolveTypeViaAttributes($attributes);
}

public function getTypeAttribute()
public function getTypeAttribute(): ?string
{
$type = $this->resolveTypeViaAttributes($this->attributes);
$type = Str::kebab(class_basename($type));
return $type ?: null;
}

public function getTable()
public function getTable(): string
{
if (!isset($this->table)) {
return str_replace(
Expand Down
11 changes: 9 additions & 2 deletions tests/Unit/Traits/SingleTableInheritanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,20 @@ public function queries_are_scoped_correctly()
$this->assertCount(10, Car::all());
}

/** @test */
public function _scope_sti_with_types_param()
/** @test */
public function scope_sti_with_types_param()
{
factory(Vehicle::class, 10)->create();
factory(Car::class, 10)->create();
$this->assertCount(20, Vehicle::all());
$this->assertEquals(10, Vehicle::sti([Car::class])->count());
$this->assertCount(10, Car::all());
}

/** @test */
public function attribute_is_set_when_subclass_is_created_via_constructor()
{
$car = new Car();
$this->assertEquals('car', $car->type);
}
}

0 comments on commit 531ac37

Please sign in to comment.