cybercog /
laravel-love
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of Laravel Love. |
||
| 5 | * |
||
| 6 | * (c) Anton Komarev <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | declare(strict_types=1); |
||
| 13 | |||
| 14 | namespace Cog\Laravel\Love\Reacterable; |
||
| 15 | |||
| 16 | use Cog\Contracts\Love\Reactable\Models\Reactable as ReactableInterface; |
||
| 17 | use Cog\Laravel\Love\Reaction\Models\Reaction; |
||
| 18 | use Cog\Laravel\Love\ReactionType\Models\ReactionType; |
||
| 19 | use DateTimeInterface; |
||
| 20 | use Illuminate\Database\Eloquent\Builder; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @mixin Builder |
||
| 24 | */ |
||
| 25 | trait ReacterableEloquentBuilderTrait |
||
| 26 | { |
||
| 27 | public function whereReactedTo( |
||
| 28 | ReactableInterface $reactable, |
||
| 29 | string | null $reactionTypeName = null, |
||
| 30 | ): Builder { |
||
| 31 | return $this->whereHas( |
||
| 32 | 'loveReacter.reactions', |
||
| 33 | function (Builder $reactionsQuery) use ( |
||
| 34 | $reactable, |
||
| 35 | $reactionTypeName, |
||
| 36 | ): void { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 37 | $reactionsQuery->where( |
||
| 38 | 'reactant_id', |
||
| 39 | $reactable->getLoveReactant()->getId(), |
||
| 40 | ); |
||
| 41 | |||
| 42 | if ($reactionTypeName !== null) { |
||
| 43 | $reactionsQuery->where( |
||
| 44 | 'reaction_type_id', |
||
| 45 | ReactionType::fromName($reactionTypeName)->getId(), |
||
| 46 | ); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function whereNotReactedTo( |
||
| 53 | ReactableInterface $reactable, |
||
| 54 | string | null $reactionTypeName = null, |
||
| 55 | ): Builder { |
||
| 56 | return $this->whereDoesntHave( |
||
| 57 | 'loveReacter.reactions', |
||
| 58 | function (Builder $reactionsQuery) use ( |
||
| 59 | $reactable, |
||
| 60 | $reactionTypeName, |
||
| 61 | ): void { |
||
| 62 | $reactionsQuery->where( |
||
| 63 | 'reactant_id', |
||
| 64 | $reactable->getLoveReactant()->getId(), |
||
| 65 | ); |
||
| 66 | |||
| 67 | if ($reactionTypeName !== null) { |
||
| 68 | $reactionsQuery->where( |
||
| 69 | 'reaction_type_id', |
||
| 70 | ReactionType::fromName($reactionTypeName)->getId(), |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function whereReactedToBetween( |
||
| 78 | ReactableInterface $reactable, |
||
| 79 | DateTimeInterface $reactedAtFrom, |
||
| 80 | DateTimeInterface $reactedAtTo, |
||
| 81 | string | null $reactionTypeName = null, |
||
| 82 | ): Builder { |
||
| 83 | return $this->whereHas( |
||
| 84 | 'loveReacter.reactions', |
||
| 85 | function (Builder $reactionsQuery) use ( |
||
| 86 | $reactable, |
||
| 87 | $reactedAtFrom, |
||
| 88 | $reactedAtTo, |
||
| 89 | $reactionTypeName, |
||
| 90 | ): void { |
||
| 91 | $reactionsQuery->where( |
||
| 92 | 'reactant_id', |
||
| 93 | $reactable->getLoveReactant()->getId(), |
||
| 94 | ); |
||
| 95 | |||
| 96 | $reactionsQuery->whereBetween( |
||
| 97 | (new Reaction())->getTable() . '.created_at', |
||
| 98 | [$reactedAtFrom, $reactedAtTo], |
||
| 99 | ); |
||
| 100 | |||
| 101 | if ($reactionTypeName !== null) { |
||
| 102 | $reactionsQuery->where( |
||
| 103 | 'reaction_type_id', |
||
| 104 | ReactionType::fromName($reactionTypeName)->getId(), |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | } |
||
| 111 |