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\Reactable; |
15
|
|
|
|
16
|
|
|
use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableInterface; |
17
|
|
|
use Cog\Laravel\Love\Reactant\ReactionCounter\Models\ReactionCounter; |
18
|
|
|
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal; |
19
|
|
|
use Cog\Laravel\Love\ReactionType\Models\ReactionType; |
20
|
|
|
use Illuminate\Database\Eloquent\Builder; |
21
|
|
|
use Illuminate\Database\Query\JoinClause; |
22
|
|
|
use Illuminate\Support\Facades\DB; |
23
|
|
|
use Illuminate\Support\Str; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @mixin Builder |
27
|
|
|
*/ |
28
|
|
|
trait ReactableEloquentBuilderTrait |
29
|
|
|
{ |
30
|
|
|
public function whereReactedBy( |
31
|
|
|
ReacterableInterface $reacterable, |
32
|
|
|
?string $reactionTypeName = null, |
33
|
|
|
): Builder { |
34
|
|
|
return $this->whereHas( |
|
|
|
|
35
|
|
|
'loveReactant.reactions', |
36
|
|
|
function (Builder $reactionsQuery) use ($reacterable, $reactionTypeName): void { |
37
|
|
|
$reactionsQuery->where('reacter_id', $reacterable->getLoveReacter()->getId()); |
38
|
|
|
if ($reactionTypeName !== null) { |
39
|
|
|
$reactionsQuery->where('reaction_type_id', ReactionType::fromName($reactionTypeName)->getId()); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function whereNotReactedBy( |
46
|
|
|
ReacterableInterface $reacterable, |
47
|
|
|
?string $reactionTypeName = null, |
48
|
|
|
): Builder { |
49
|
|
|
return $this->whereDoesntHave( |
|
|
|
|
50
|
|
|
'loveReactant.reactions', |
51
|
|
|
function (Builder $reactionsQuery) use ($reacterable, $reactionTypeName): void { |
52
|
|
|
$reactionsQuery->where('reacter_id', $reacterable->getLoveReacter()->getId()); |
53
|
|
|
if ($reactionTypeName !== null) { |
54
|
|
|
$reactionsQuery->where('reaction_type_id', ReactionType::fromName($reactionTypeName)->getId()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function joinReactionCounterOfType( |
61
|
|
|
string $reactionTypeName, |
62
|
|
|
?string $alias = null, |
63
|
|
|
): Builder { |
64
|
|
|
$reactionType = ReactionType::fromName($reactionTypeName); |
65
|
|
|
$alias = $alias === null ? 'reaction_' . Str::snake($reactionType->getName()) : $alias; |
66
|
|
|
|
67
|
|
|
$select = $this->getQuery()->columns ?? ["{$this->getModel()->getTable()}.*"]; |
|
|
|
|
68
|
|
|
$select[] = DB::raw("COALESCE({$alias}.count, 0) as {$alias}_count"); |
69
|
|
|
$select[] = DB::raw("COALESCE({$alias}.weight, 0) as {$alias}_weight"); |
70
|
|
|
|
71
|
|
|
return $this |
72
|
|
|
->leftJoin( |
|
|
|
|
73
|
|
|
(new ReactionCounter())->getTable() . ' as ' . $alias, |
74
|
|
|
function (JoinClause $join) use ($reactionType, $alias): void { |
75
|
|
|
$join->on("{$alias}.reactant_id", '=', "{$this->getModel()->getTable()}.love_reactant_id"); |
76
|
|
|
$join->where("{$alias}.reaction_type_id", $reactionType->getId()); |
77
|
|
|
} |
78
|
|
|
) |
79
|
|
|
->select($select); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function joinReactionTotal( |
83
|
|
|
?string $alias = null, |
84
|
|
|
): Builder { |
85
|
|
|
$alias = $alias === null ? 'reaction_total' : $alias; |
86
|
|
|
$select = $this->getQuery()->columns ?? ["{$this->getModel()->getTable()}.*"]; |
87
|
|
|
$select[] = DB::raw("COALESCE({$alias}.count, 0) as {$alias}_count"); |
88
|
|
|
$select[] = DB::raw("COALESCE({$alias}.weight, 0) as {$alias}_weight"); |
89
|
|
|
|
90
|
|
|
return $this |
91
|
|
|
->leftJoin( |
92
|
|
|
(new ReactionTotal())->getTable() . ' as ' . $alias, |
93
|
|
|
"{$alias}.reactant_id", |
94
|
|
|
'=', |
95
|
|
|
"{$this->getModel()->getTable()}.love_reactant_id" |
96
|
|
|
) |
97
|
|
|
->select($select); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|