-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add withInitialQueryConstraint() and withQueryConstraint() methods
- Loading branch information
1 parent
48d0b84
commit 04df119
Showing
8 changed files
with
210 additions
and
124 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
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,106 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Traits; | ||
|
||
trait HasQueryConstraints | ||
{ | ||
/** | ||
* The additional constraint for the initial query. | ||
* | ||
* @var callable|null | ||
*/ | ||
public static $initialQueryConstraint; | ||
|
||
/** | ||
* The additional constraint for the recursive query. | ||
* | ||
* @var callable|null | ||
*/ | ||
public static $recursiveQueryConstraint; | ||
|
||
/** | ||
* Execute a query with an additional constraint for the initial query. | ||
* | ||
* @param callable $constraint | ||
* @param callable $query | ||
* @return mixed | ||
*/ | ||
public static function withInitialQueryConstraint(callable $constraint, callable $query): mixed | ||
{ | ||
$previous = static::$initialQueryConstraint; | ||
|
||
static::$initialQueryConstraint = $constraint; | ||
|
||
$result = $query(); | ||
|
||
static::$initialQueryConstraint = $previous; | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Execute a query with an additional constraint for the recursive query. | ||
* | ||
* @param callable $constraint | ||
* @param callable $query | ||
* @return mixed | ||
*/ | ||
public static function withRecursiveQueryConstraint(callable $constraint, callable $query): mixed | ||
{ | ||
$previous = static::$recursiveQueryConstraint; | ||
|
||
static::$recursiveQueryConstraint = $constraint; | ||
|
||
$result = $query(); | ||
|
||
static::$recursiveQueryConstraint = $previous; | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Set an additional constraint for the recursive query. | ||
* | ||
* @param callable $constraint | ||
* @return void | ||
*/ | ||
public static function setRecursiveQueryConstraint(callable $constraint): void | ||
{ | ||
static::$recursiveQueryConstraint = $constraint; | ||
} | ||
|
||
/** | ||
* Unset the additional constraint for the recursive query. | ||
* | ||
* @return void | ||
*/ | ||
public static function unsetRecursiveQueryConstraint(): void | ||
{ | ||
static::$recursiveQueryConstraint = null; | ||
} | ||
|
||
/** | ||
* Execute a query with an additional constraint for the initial and recursive query. | ||
* | ||
* @param callable $constraint | ||
* @param callable $query | ||
* @return mixed | ||
*/ | ||
public static function withQueryConstraint( | ||
callable $constraint, | ||
callable $query | ||
): mixed { | ||
$previousInitialConstraint = static::$initialQueryConstraint; | ||
$previousRecursiveConstraint = static::$recursiveQueryConstraint; | ||
|
||
static::$initialQueryConstraint = $constraint; | ||
static::$recursiveQueryConstraint = $constraint; | ||
|
||
$result = $query(); | ||
|
||
static::$initialQueryConstraint = $previousInitialConstraint; | ||
static::$recursiveQueryConstraint = $previousRecursiveConstraint; | ||
|
||
return $result; | ||
} | ||
} |
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.