-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract local Model scopes to Eloquent Builder traits (#227)
- Loading branch information
1 parent
0c0036a
commit 1df2ed9
Showing
14 changed files
with
1,813 additions
and
1,480 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Love. | ||
* | ||
* (c) Anton Komarev <anton@komarev.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cog\Laravel\Love\Reactable; | ||
|
||
use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableInterface; | ||
use Cog\Laravel\Love\Reactant\ReactionCounter\Models\ReactionCounter; | ||
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal; | ||
use Cog\Laravel\Love\ReactionType\Models\ReactionType; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Query\JoinClause; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* @mixin Builder | ||
*/ | ||
trait ReactableEloquentBuilderTrait | ||
{ | ||
public function whereReactedBy( | ||
ReacterableInterface $reacterable, | ||
?string $reactionTypeName = null, | ||
): Builder { | ||
return $this->whereHas( | ||
'loveReactant.reactions', | ||
function (Builder $reactionsQuery) use ( | ||
$reacterable, | ||
$reactionTypeName, | ||
): void { | ||
$reactionsQuery->where( | ||
'reacter_id', | ||
$reacterable->getLoveReacter()->getId(), | ||
); | ||
|
||
if ($reactionTypeName !== null) { | ||
$reactionsQuery->where( | ||
'reaction_type_id', | ||
ReactionType::fromName($reactionTypeName)->getId(), | ||
); | ||
} | ||
} | ||
); | ||
} | ||
|
||
public function whereNotReactedBy( | ||
ReacterableInterface $reacterable, | ||
?string $reactionTypeName = null, | ||
): Builder { | ||
return $this->whereDoesntHave( | ||
'loveReactant.reactions', | ||
function (Builder $reactionsQuery) use ( | ||
$reacterable, | ||
$reactionTypeName, | ||
): void { | ||
$reactionsQuery->where( | ||
'reacter_id', | ||
$reacterable->getLoveReacter()->getId(), | ||
); | ||
|
||
if ($reactionTypeName !== null) { | ||
$reactionsQuery->where( | ||
'reaction_type_id', | ||
ReactionType::fromName($reactionTypeName)->getId(), | ||
); | ||
} | ||
} | ||
); | ||
} | ||
|
||
public function joinReactionCounterOfType( | ||
string $reactionTypeName, | ||
?string $alias = null, | ||
): Builder { | ||
$reactionType = ReactionType::fromName($reactionTypeName); | ||
$alias = $alias === null | ||
? 'reaction_' . Str::snake($reactionType->getName()) | ||
: $alias; | ||
|
||
$select = $this->getQuery()->columns ?? ["{$this->getModel()->getTable()}.*"]; | ||
$select[] = $this->raw("COALESCE({$alias}.count, 0) as {$alias}_count"); | ||
$select[] = $this->raw("COALESCE({$alias}.weight, 0) as {$alias}_weight"); | ||
|
||
return $this | ||
->leftJoin( | ||
(new ReactionCounter())->getTable() . ' as ' . $alias, | ||
function (JoinClause $join) use ( | ||
$reactionType, | ||
$alias, | ||
): void { | ||
$join->on( | ||
"{$alias}.reactant_id", | ||
'=', | ||
"{$this->getModel()->getTable()}.love_reactant_id", | ||
); | ||
$join->where( | ||
"{$alias}.reaction_type_id", | ||
$reactionType->getId(), | ||
); | ||
} | ||
) | ||
->select($select); | ||
} | ||
|
||
public function joinReactionTotal( | ||
?string $alias = null, | ||
): Builder { | ||
$alias = $alias === null | ||
? 'reaction_total' | ||
: $alias; | ||
|
||
$select = $this->getQuery()->columns ?? ["{$this->getModel()->getTable()}.*"]; | ||
$select[] = $this->raw("COALESCE({$alias}.count, 0) as {$alias}_count"); | ||
$select[] = $this->raw("COALESCE({$alias}.weight, 0) as {$alias}_weight"); | ||
|
||
return $this | ||
->leftJoin( | ||
(new ReactionTotal())->getTable() . ' as ' . $alias, | ||
"{$alias}.reactant_id", | ||
'=', | ||
"{$this->getModel()->getTable()}.love_reactant_id", | ||
) | ||
->select($select); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.