Skip to content

Commit

Permalink
Improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Oct 6, 2024
1 parent 26e29fd commit 94fbc24
Showing 1 changed file with 46 additions and 109 deletions.
155 changes: 46 additions & 109 deletions src/HasJsonRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@

trait HasJsonRelationships
{
/**
* Get an attribute from the model.
*
* @param string $key
* @return mixed
*/
/** @inheritDoc */
public function getAttribute($key)
{
$attribute = preg_split('/(->|\[])/', $key)[0];
Expand All @@ -43,12 +38,7 @@ public function getAttribute($key)
return parent::getAttribute($key);
}

/**
* Get an attribute from the $attributes array.
*
* @param string $key
* @return mixed
*/
/** @inheritDoc */
public function getAttributeFromArray($key)
{
if (str_contains($key, '->')) {
Expand All @@ -58,12 +48,7 @@ public function getAttributeFromArray($key)
return parent::getAttributeFromArray($key);
}

/**
* Get a plain attribute (not a relationship).
*
* @param string $key
* @return mixed
*/
/** @inheritDoc */
public function getAttributeValue($key)
{
if (str_contains($key, '->')) {
Expand All @@ -83,15 +68,7 @@ public function getAttributeValue($key)
return parent::getAttributeValue($key);
}

/**
* Instantiate a new HasOne relationship.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
/** @inheritDoc */
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
{
/** @var \Illuminate\Database\Connection $connection */
Expand All @@ -104,18 +81,7 @@ protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localK
return new HasOne($query, $parent, $foreignKey, $localKey);
}

/**
* Instantiate a new HasOneThrough relationship.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $farParent
* @param \Illuminate\Database\Eloquent\Model $throughParent
* @param string $firstKey
* @param string $secondKey
* @param string $localKey
* @param string $secondLocalKey
* @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
*/
/** @inheritDoc */
protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
{
/** @var \Illuminate\Database\Connection $connection */
Expand All @@ -128,16 +94,7 @@ protected function newHasOneThrough(Builder $query, Model $farParent, Model $thr
return new HasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
}

/**
* Instantiate a new MorphOne relationship.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $type
* @param string $id
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
/** @inheritDoc */
protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey)
{
/** @var \Illuminate\Database\Connection $connection */
Expand All @@ -150,16 +107,7 @@ protected function newMorphOne(Builder $query, Model $parent, $type, $id, $local
return new MorphOne($query, $parent, $type, $id, $localKey);
}

/**
* Instantiate a new BelongsTo relationship.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $child
* @param string $foreignKey
* @param string $ownerKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
/** @inheritDoc */
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
{
/** @var \Illuminate\Database\Connection $connection */
Expand All @@ -172,15 +120,7 @@ protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $owne
return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
}

/**
* Instantiate a new HasMany relationship.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
/** @inheritDoc */
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
{
/** @var \Illuminate\Database\Connection $connection */
Expand All @@ -193,18 +133,7 @@ protected function newHasMany(Builder $query, Model $parent, $foreignKey, $local
return new HasMany($query, $parent, $foreignKey, $localKey);
}

/**
* Instantiate a new HasManyThrough relationship.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $farParent
* @param \Illuminate\Database\Eloquent\Model $throughParent
* @param string $firstKey
* @param string $secondKey
* @param string $localKey
* @param string $secondLocalKey
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
/** @inheritDoc */
protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
{
/** @var \Illuminate\Database\Connection $connection */
Expand All @@ -217,16 +146,7 @@ protected function newHasManyThrough(Builder $query, Model $farParent, Model $th
return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
}

/**
* Instantiate a new MorphMany relationship.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $type
* @param string $id
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
/** @inheritDoc */
protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey)
{
/** @var \Illuminate\Database\Connection $connection */
Expand All @@ -242,11 +162,13 @@ protected function newMorphMany(Builder $query, Model $parent, $type, $id, $loca
/**
* Define an inverse one-to-one or many JSON relationship.
*
* @param string $related
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @param class-string<TRelatedModel> $related
* @param string|array $foreignKey
* @param string|array $ownerKey
* @param string $relation
* @return \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson
* @return \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson<TRelatedModel, $this>
*/
public function belongsToJson($related, $foreignKey, $ownerKey = null, $relation = null)
{
Expand All @@ -271,12 +193,15 @@ public function belongsToJson($related, $foreignKey, $ownerKey = null, $relation
/**
* Instantiate a new BelongsToJson relationship.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $child
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
* @param TDeclaringModel $child
* @param string|array $foreignKey
* @param string|array $ownerKey
* @param string $relation
* @return \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson
* @return \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson<TRelatedModel, TDeclaringModel>
*/
protected function newBelongsToJson(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
{
Expand All @@ -286,10 +211,12 @@ protected function newBelongsToJson(Builder $query, Model $child, $foreignKey, $
/**
* Define a one-to-many JSON relationship.
*
* @param string $related
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @param class-string<TRelatedModel> $related
* @param string|array $foreignKey
* @param string|array $localKey
* @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyJson
* @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyJson<TRelatedModel, $this>
*/
public function hasManyJson($related, $foreignKey, $localKey = null)
{
Expand Down Expand Up @@ -318,11 +245,14 @@ public function hasManyJson($related, $foreignKey, $localKey = null)
/**
* Instantiate a new HasManyJson relationship.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
* @param TDeclaringModel $parent
* @param string|array $foreignKey
* @param string|array $localKey
* @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyJson
* @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyJson<TRelatedModel, TDeclaringModel>
*/
protected function newHasManyJson(Builder $query, Model $parent, $foreignKey, $localKey)
{
Expand All @@ -332,10 +262,12 @@ protected function newHasManyJson(Builder $query, Model $parent, $foreignKey, $l
/**
* Define a one-to-one JSON relationship.
*
* @param string $related
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @param class-string<TRelatedModel> $related
* @param string|array $foreignKey
* @param string|array|null $localKey
* @return \Staudenmeir\EloquentJsonRelations\Relations\HasOneJson
* @return \Staudenmeir\EloquentJsonRelations\Relations\HasOneJson<TRelatedModel, $this>
*/
public function hasOneJson(string $related, string|array $foreignKey, string|array|null $localKey = null): HasOneJson
{
Expand Down Expand Up @@ -364,11 +296,14 @@ public function hasOneJson(string $related, string|array $foreignKey, string|arr
/**
* Instantiate a new HasOneJson relationship.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
* @param TDeclaringModel $parent
* @param string|array $foreignKey
* @param string|array $localKey
* @return \Staudenmeir\EloquentJsonRelations\Relations\HasOneJson
* @return \Staudenmeir\EloquentJsonRelations\Relations\HasOneJson<TRelatedModel, TDeclaringModel>
*/
protected function newHasOneJson(Builder $query, Model $parent, string|array $foreignKey, string|array $localKey): HasOneJson
{
Expand All @@ -378,13 +313,15 @@ protected function newHasOneJson(Builder $query, Model $parent, string|array $fo
/**
* Define has-many-through JSON relationship.
*
* @param string $related
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @param class-string<TRelatedModel> $related
* @param string $through
* @param string|\Staudenmeir\EloquentJsonRelations\JsonKey $firstKey
* @param string|null $secondKey
* @param string|null $localKey
* @param string|\Staudenmeir\EloquentJsonRelations\JsonKey|null $secondLocalKey
* @return \Staudenmeir\EloquentHasManyDeep\HasManyDeep
* @return \Staudenmeir\EloquentHasManyDeep\HasManyDeep<TRelatedModel, $this>
*/
public function hasManyThroughJson(
string $related,
Expand Down Expand Up @@ -431,7 +368,7 @@ public function hasManyThroughJson(
* Add the pivot relationship to the has-many-through JSON relationship.
*
* @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep $hasManyThroughJson
* @param \Illuminate\Database\Eloquent\Relations\Relation[] $relationships
* @param non-empty-list<\Illuminate\Database\Eloquent\Relations\Relation> $relationships
* @param \Illuminate\Database\Eloquent\Model $through
* @return void
*/
Expand Down Expand Up @@ -497,7 +434,7 @@ function (array $models, Collection $results, string $relation) use ($hasManyJso
/**
* Instantiate a new HasManyThroughJson relationship.
*
* @param \Illuminate\Database\Eloquent\Relations\Relation[] $relationships
* @param non-empty-list<\Illuminate\Database\Eloquent\Relations\Relation> $relationships
* @return \Staudenmeir\EloquentHasManyDeep\HasManyDeep
*/
protected function newHasManyThroughJson(array $relationships)
Expand Down

0 comments on commit 94fbc24

Please sign in to comment.