Skip to content

Commit

Permalink
Merge pull request #97 from JustSteveKing/feature/improvements
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
JustSteveKing authored Jun 22, 2022
2 parents 381d8bc + edc126c commit c4f458f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 56 deletions.
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
"license": "MIT",
"authors": [
{
"role": "author",
"name": "Caleb Porzio",
"email": "[email protected]"
"email": "[email protected]",
"homepage": "https://calebporzio.com/"
},
{
"role": "developer",
"name": "Steve McDougall",
"email": "[email protected]",
"homepage": "https://www.juststeveking.uk/"
}
],
"require": {
Expand Down
112 changes: 75 additions & 37 deletions src/HasChildren.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@

namespace Parental;

use Closure;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;

trait HasChildren
{
/**
* @var bool
*/
protected static $parentBootMethods;

/**
* @var bool
*/
protected $hasChildren = true;

protected static function registerModelEvent($event, $callback)
/**
* Register a model event with the dispatcher.
*
* @param string $event
* @param Closure|string $callback
* @return void
*/
protected static function registerModelEvent($event, $callback): void
{
parent::registerModelEvent($event, $callback);

Expand All @@ -29,7 +46,10 @@ protected static function registerModelEvent($event, $callback)
}
}

protected static function parentIsBooting()
/**
* @return bool
*/
protected static function parentIsBooting(): bool
{
if (! isset(self::$parentBootMethods)) {
self::$parentBootMethods[] = 'boot';
Expand All @@ -54,11 +74,13 @@ protected static function parentIsBooting()
}

/**
* @param array $attributes
* @param bool $exists
* @return $this
* Create a new instance of the given model.
*
* @param array $attributes
* @param bool $exists
* @return static
*/
public function newInstance($attributes = [], $exists = false)
public function newInstance($attributes = [], $exists = false): self
{
$model = isset($attributes[$this->getInheritanceColumn()])
? $this->getChildModel($attributes)
Expand All @@ -74,11 +96,13 @@ public function newInstance($attributes = [], $exists = false)
}

/**
* @param array $attributes
* @param null $connection
* @return $this
* Create a new model instance that is existing.
*
* @param array $attributes
* @param string|null $connection
* @return static
*/
public function newFromBuilder($attributes = [], $connection = null)
public function newFromBuilder($attributes = [], $connection = null): self
{
$attributes = (array) $attributes;

Expand All @@ -104,12 +128,12 @@ public function newFromBuilder($attributes = [], $connection = null)
* Define an inverse one-to-one or many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $ownerKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @param string|null $foreignKey
* @param string|null $ownerKey
* @param string|null $relation
* @return BelongsTo
*/
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null): BelongsTo
{
$instance = $this->newRelatedInstance($related);

Expand All @@ -128,11 +152,11 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat
* Define a one-to-many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* @param string|null $foreignKey
* @param string|null $localKey
* @return HasMany
*/
public function hasMany($related, $foreignKey = null, $localKey = null)
public function hasMany($related, $foreignKey = null, $localKey = null): HasMany
{
return parent::hasMany($related, $foreignKey, $localKey);
}
Expand All @@ -141,37 +165,51 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
* Define a many-to-many relationship.
*
* @param string $related
* @param string $table
* @param string $foreignPivotKey
* @param string $relatedPivotKey
* @param string $parentKey
* @param string $relatedKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
* @param string|null $table
* @param string|null $foreignPivotKey
* @param string|null $relatedPivotKey
* @param string|null $parentKey
* @param string|null $relatedKey
* @param string|null $relation
* @return BelongsToMany
*/
public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null)
{
public function belongsToMany(
$related, $table = null,
$foreignPivotKey = null,
$relatedPivotKey = null,
$parentKey = null,
$relatedKey = null,
$relation = null
): BelongsToMany {
$instance = $this->newRelatedInstance($related);

if (is_null($table) && $instance->hasParent) {
$table = $this->joiningTable($instance->getClassNameForRelationships());
}

return parent::belongsToMany($related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relation);
return parent::belongsToMany(
$related,
$table,
$foreignPivotKey,
$relatedPivotKey,
$parentKey,
$relatedKey,
$relation,
);
}

/**
* @return string
*/
public function getClassNameForRelationships()
public function getClassNameForRelationships(): string
{
return class_basename($this);
}

/**
* @return string
*/
public function getInheritanceColumn()
public function getInheritanceColumn(): string
{
return property_exists($this, 'childColumn') ? $this->childColumn : 'type';
}
Expand All @@ -186,14 +224,14 @@ protected function getChildModel(array $attributes)
$attributes[$this->getInheritanceColumn()]
);

return new $className((array)$attributes);
return new $className((array) $attributes);
}

/**
* @param $aliasOrClass
* @param mixed $aliasOrClass
* @return string
*/
public function classFromAlias($aliasOrClass)
public function classFromAlias($aliasOrClass): string
{
$childTypes = $this->getChildTypes();

Expand All @@ -205,10 +243,10 @@ public function classFromAlias($aliasOrClass)
}

/**
* @param $className
* @param string $className
* @return string
*/
public function classToAlias($className)
public function classToAlias(string $className): string
{
$childTypes = $this->getChildTypes();

Expand All @@ -222,7 +260,7 @@ public function classToAlias($className)
/**
* @return array
*/
public function getChildTypes()
public function getChildTypes(): array
{
return property_exists($this, 'childTypes') ? $this->childTypes : [];
}
Expand Down
41 changes: 25 additions & 16 deletions src/HasParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

namespace Parental;

use Illuminate\Database\Eloquent\Model;
use ReflectionClass;
use Illuminate\Support\Str;
use Illuminate\Events\Dispatcher;
use ReflectionException;

trait HasParent
{
/**
* @var bool
*/
public $hasParent = true;

public static function bootHasParent()
/**
* @return void
* @throws ReflectionException
*/
public static function bootHasParent(): void
{
// This adds support for using Parental with standalone Eloquent, outside a normal Laravel app.
if (static::getEventDispatcher() === null) {
Expand All @@ -37,16 +46,16 @@ public static function bootHasParent()
/**
* @return bool
*/
public function parentHasHasChildrenTrait()
public function parentHasHasChildrenTrait(): bool
{
return $this->hasChildren ?? false;
}

/**
* @return string
* @throws \ReflectionException
* @throws ReflectionException
*/
public function getTable()
public function getTable(): string
{
if (! isset($this->table)) {
return str_replace('\\', '', Str::snake(Str::plural(class_basename($this->getParentClass()))));
Expand All @@ -57,20 +66,20 @@ public function getTable()

/**
* @return string
* @throws \ReflectionException
* @throws ReflectionException
*/
public function getForeignKey()
public function getForeignKey(): string
{
return Str::snake(class_basename($this->getParentClass())).'_'.$this->primaryKey;
}

/**
* @param $related
* @param null $instance
* @param string $related
* @param null|Model $instance
* @return string
* @throws \ReflectionException
* @throws ReflectionException
*/
public function joiningTable($related, $instance = null)
public function joiningTable($related, $instance = null): string
{
$relatedClassName = method_exists((new $related), 'getClassNameForRelationships')
? (new $related)->getClassNameForRelationships()
Expand All @@ -88,9 +97,9 @@ public function joiningTable($related, $instance = null)

/**
* @return string
* @throws \ReflectionException
* @throws ReflectionException
*/
public function getClassNameForRelationships()
public function getClassNameForRelationships(): string
{
return class_basename($this->getParentClass());
}
Expand All @@ -99,9 +108,9 @@ public function getClassNameForRelationships()
* Get the class name for polymorphic relations.
*
* @return string
* @throws \ReflectionException
* @throws ReflectionException
*/
public function getMorphClass()
public function getMorphClass(): string
{
$parentClass = $this->getParentClass();

Expand All @@ -112,9 +121,9 @@ public function getMorphClass()
* Get the class name for Parent Class.
*
* @return string
* @throws \ReflectionException
* @throws ReflectionException
*/
protected function getParentClass()
protected function getParentClass(): string
{
static $parentClassName;

Expand Down
10 changes: 8 additions & 2 deletions src/Providers/NovaResourceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

class NovaResourceProvider extends ServiceProvider
{
public function boot()
/**
* @return void
*/
public function boot(): void
{
if (class_exists(Nova::class)) {
Nova::serving(function () {
Expand All @@ -18,7 +21,10 @@ public function boot()
}
}

protected function setNovaResources()
/**
* @return void
*/
protected function setNovaResources(): void
{
$map = [];
foreach (Nova::$resources as $resource) {
Expand Down

0 comments on commit c4f458f

Please sign in to comment.