Skip to content

Commit

Permalink
Merge pull request #76 from renoki-co/feature/custom-morph-class-method
Browse files Browse the repository at this point in the history
[feature] Custom Morphing class method
  • Loading branch information
rennokki authored Sep 29, 2020
2 parents 65093af + 00cc679 commit cb57a53
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/Traits/CanBeBlocked.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

trait CanBeBlocked
{
use HasCustomModelClass;

/**
* Relationship for models that blocked this model.
*
Expand All @@ -12,7 +14,7 @@ trait CanBeBlocked
*/
public function blockers($model = null)
{
$modelClass = $model ? (new $model)->getMorphClass() : $this->getMorphClass();
$modelClass = $this->getModelMorphClass($model);

return $this
->morphToMany($modelClass, 'blockable', 'blockers', 'blockable_id', 'blocker_id')
Expand Down
6 changes: 4 additions & 2 deletions src/Traits/CanBeFollowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

trait CanBeFollowed
{
use HasCustomModelClass;

/**
* Relationship for models that followed this model.
*
Expand All @@ -15,7 +17,7 @@ trait CanBeFollowed
*/
public function followers($model = null)
{
$modelClass = $model ? (new $model)->getMorphClass() : $this->getMorphClass();
$modelClass = $this->getModelMorphClass($model);

return $this
->morphToMany($modelClass, 'followable', 'followers', 'followable_id', 'follower_id')
Expand All @@ -34,7 +36,7 @@ public function followers($model = null)
*/
public function followerRequests($model = null)
{
$modelClass = $model ? (new $model)->getMorphClass() : $this->getMorphClass();
$modelClass = $this->getModelMorphClass($model);

return $this
->morphToMany($modelClass, 'followable', 'followers', 'followable_id', 'follower_id')
Expand Down
4 changes: 3 additions & 1 deletion src/Traits/CanBeLiked.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

trait CanBeLiked
{
use HasCustomModelClass;

/**
* Relationship for models that liked this model.
*
Expand All @@ -12,7 +14,7 @@ trait CanBeLiked
*/
public function likers($model = null)
{
$modelClass = $model ? (new $model)->getMorphClass() : $this->getMorphClass();
$modelClass = $this->getModelMorphClass($model);

return $this
->morphToMany($modelClass, 'likeable', 'likers', 'likeable_id', 'liker_id')
Expand Down
4 changes: 3 additions & 1 deletion src/Traits/CanBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

trait CanBlock
{
use HasCustomModelClass;

/**
* Relationship for models that this model is currently blocking.
*
Expand All @@ -15,7 +17,7 @@ trait CanBlock
*/
public function blocking($model = null)
{
$modelClass = $model ? (new $model)->getMorphClass() : $this->getMorphClass();
$modelClass = $this->getModelMorphClass($model);

return $this
->morphToMany($modelClass, 'blocker', 'blockers', 'blocker_id', 'blockable_id')
Expand Down
6 changes: 4 additions & 2 deletions src/Traits/CanFollow.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

trait CanFollow
{
use HasCustomModelClass;

/**
* Relationship for models that this model is currently following.
*
Expand All @@ -15,7 +17,7 @@ trait CanFollow
*/
public function following($model = null)
{
$modelClass = $model ? (new $model)->getMorphClass() : $this->getMorphClass();
$modelClass = $this->getModelMorphClass($model);

return $this
->morphToMany($modelClass, 'follower', 'followers', 'follower_id', 'followable_id')
Expand Down Expand Up @@ -116,7 +118,7 @@ public function unfollow($model): bool
*/
public function followRequests($model = null)
{
$modelClass = $model ? (new $model)->getMorphClass() : $this->getMorphClass();
$modelClass = $this->getModelMorphClass($model);

return $this
->morphToMany($modelClass, 'follower', 'followers', 'follower_id', 'followable_id')
Expand Down
4 changes: 3 additions & 1 deletion src/Traits/CanLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

trait CanLike
{
use HasCustomModelClass;

/**
* Relationship for models that this model is currently liking.
*
Expand All @@ -15,7 +17,7 @@ trait CanLike
*/
public function liking($model = null)
{
$modelClass = $model ? (new $model)->getMorphClass() : $this->getMorphClass();
$modelClass = $this->getModelMorphClass($model);

return $this
->morphToMany($modelClass, 'liker', 'likers', 'liker_id', 'likeable_id')
Expand Down
29 changes: 29 additions & 0 deletions src/Traits/HasCustomModelClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rennokki\Befriended\Traits;

trait HasCustomModelClass
{
/**
* Get the model's morph class to act as the main resource.
*
* @param string|null $model
* @return string
*/
protected function getModelMorphClass($model = null)
{
return $model
? (new $model)->getMorphClass()
: $this->getCurrentModelMorphClass();
}

/**
* Get the current model's morph class.
*
* @return string
*/
protected function getCurrentModelMorphClass()
{
return $this->getMorphClass();
}
}

0 comments on commit cb57a53

Please sign in to comment.