Skip to content

Commit 18ffac5

Browse files
authored
Merge pull request #4 from cslant/feat/new-interaction-scope
Feat/new interaction scopes
2 parents 56f4a3c + 3f83d72 commit 18ffac5

File tree

5 files changed

+144
-1
lines changed

5 files changed

+144
-1
lines changed

src/Enums/InteractionTypeEnum.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,23 @@ public function isLove(): bool
6666
{
6767
return $this === self::LOVE;
6868
}
69+
70+
/**
71+
* Get the type by value.
72+
* This method can be used to get the value of InteractionTypeEnum based on the passed string.
73+
* You can use it to convert string value to enum value.
74+
*
75+
* @param string $value
76+
*
77+
* @return InteractionTypeEnum
78+
*/
79+
public function getTypeByValue(string $value): InteractionTypeEnum
80+
{
81+
return match ($value) {
82+
self::LIKE->value => self::LIKE,
83+
self::DISLIKE->value => self::DISLIKE,
84+
self::LOVE->value => self::LOVE,
85+
default => self::DEFAULT,
86+
};
87+
}
6988
}

src/Traits/InteractionRelationship.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CSlant\LaravelLike\Traits;
44

5+
use CSlant\LaravelLike\Enums\InteractionTypeEnum;
56
use CSlant\LaravelLike\Models\Like;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\MorphMany;
@@ -37,4 +38,57 @@ public function likes(): MorphMany
3738
{
3839
return $this->morphMany((string) config('like.interaction_model') ?? Like::class, 'model');
3940
}
41+
42+
/**
43+
* Check if the model has been interacted by the given user.
44+
*
45+
* @param int $userId
46+
* @param null|InteractionTypeEnum $interactionType
47+
*
48+
* @return bool
49+
*/
50+
public function isInteractedBy(int $userId, ?InteractionTypeEnum $interactionType = null): bool
51+
{
52+
$userForeignKey = (string) (config('like.users.foreign_key') ?? 'user_id');
53+
54+
$query = $this->likes()->where($userForeignKey, $userId);
55+
56+
if (in_array($interactionType, InteractionTypeEnum::getValuesAsStrings())) {
57+
$query->where('type', $interactionType);
58+
}
59+
60+
return $query->exists();
61+
}
62+
63+
/**
64+
* Check and forget all recorded interactions of the given type.
65+
*
66+
* @param string $interactionType
67+
*
68+
* @return static
69+
*/
70+
public function forgetInteractionsOfType(string $interactionType): static
71+
{
72+
$this->likes()->where('type', $interactionType)->delete();
73+
74+
return $this;
75+
}
76+
77+
/**
78+
* Check and forget all recorded interactions.
79+
*
80+
* @param null|string $interactionType
81+
*
82+
* @return static
83+
*/
84+
public function forgetInteractions(?string $interactionType = null): static
85+
{
86+
if ($interactionType && in_array($interactionType, InteractionTypeEnum::getValuesAsStrings())) {
87+
return $this->forgetInteractionsOfType($interactionType);
88+
}
89+
90+
$this->likes()->delete();
91+
92+
return $this;
93+
}
4094
}

src/Traits/Like/LikeScopes.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,28 @@ public function dislikesTo(): MorphMany
5757
{
5858
return $this->likes()->where('type', InteractionTypeEnum::DISLIKE);
5959
}
60+
61+
/**
62+
* Check if the model has been interacted by the given user.
63+
*
64+
* @param int $userId
65+
*
66+
* @return bool
67+
*/
68+
public function isLikedBy(int $userId): bool
69+
{
70+
return $this->isInteractedBy($userId, InteractionTypeEnum::LIKE);
71+
}
72+
73+
/**
74+
* Check if the model has been interacted by the given user.
75+
*
76+
* @param int $userId
77+
*
78+
* @return bool
79+
*/
80+
public function isDislikedBy(int $userId): bool
81+
{
82+
return $this->isInteractedBy($userId, InteractionTypeEnum::DISLIKE);
83+
}
6084
}

src/Traits/Love/LoveScope.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ public function lovesTo(): MorphMany
3737
{
3838
return $this->likes()->where('type', InteractionTypeEnum::LOVE);
3939
}
40+
41+
/**
42+
* Check if the model has been loved by the given user.
43+
*
44+
* @param int $userId
45+
*
46+
* @return bool
47+
*/
48+
public function isLovedBy(int $userId): bool
49+
{
50+
return $this->isInteractedBy($userId, InteractionTypeEnum::LOVE);
51+
}
4052
}

src/UserHasInteraction.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace CSlant\LaravelLike;
44

5+
use CSlant\LaravelLike\Enums\InteractionTypeEnum;
6+
use CSlant\LaravelLike\Models\Like;
57
use Illuminate\Database\Eloquent\Model;
68
use Illuminate\Database\Eloquent\Relations\HasMany;
79

@@ -22,9 +24,41 @@ trait UserHasInteraction
2224
*/
2325
public function likes(): HasMany
2426
{
25-
$interactionModel = (string) (config('like.interaction_model') ?? 'CSlant\LaravelLike\Models\Like');
27+
$interactionModel = (string) (config('like.interaction_model') ?? Like::class);
2628
$userForeignKey = (string) (config('like.users.foreign_key') ?? 'user_id');
2729

2830
return $this->hasMany($interactionModel, $userForeignKey);
2931
}
32+
33+
/**
34+
* Check if the user has liked the given model.
35+
*
36+
* @param string $interactionType
37+
*
38+
* @return static
39+
*/
40+
public function forgetInteractionsOfType(string $interactionType): static
41+
{
42+
$this->likes()->where('type', $interactionType)->delete();
43+
44+
return $this;
45+
}
46+
47+
/**
48+
* Check if the user has liked the given model.
49+
*
50+
* @param null|string $interactionType
51+
*
52+
* @return static
53+
*/
54+
public function forgetInteractions(?string $interactionType = null): static
55+
{
56+
if ($interactionType && in_array($interactionType, InteractionTypeEnum::getValuesAsStrings())) {
57+
return $this->forgetInteractionsOfType($interactionType);
58+
}
59+
60+
$this->likes()->delete();
61+
62+
return $this;
63+
}
3064
}

0 commit comments

Comments
 (0)