Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMuller committed Sep 9, 2024
1 parent 3e7b50b commit 25bc3c2
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions src/Eloquent/Traits/HasGraphRelationshipScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,74 @@ trait HasGraphRelationshipScopes
/**
* Add a recursive expression for a custom subgraph to the query.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @param callable $constraint
* @param int|null $maxDepth
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self>
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static>
*/
public function scopeSubgraph(Builder $query, callable $constraint, ?int $maxDepth = null): Builder
{
return $query->withRelationshipExpression('desc', $constraint, 0, null, $maxDepth);
$query->withRelationshipExpression('desc', $constraint, 0, null, $maxDepth);

return $query;
}

/**
* Limit the query by depth.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @param mixed $operator
* @param mixed|null $value
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self>
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static>
*/
public function scopeWhereDepth(Builder $query, mixed $operator, mixed $value = null): Builder
{
$arguments = array_slice(func_get_args(), 1);

return $query->where($this->getDepthName(), ...$arguments);
$query->where($this->getDepthName(), ...$arguments);

return $query;
}

/**
* Order the query breadth-first.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self>
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static>
*/
public function scopeBreadthFirst(Builder $query): Builder
{
return $query->orderBy($this->getDepthName());
$query->orderBy($this->getDepthName());

return $query;
}

/**
* Order the query depth-first.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self>
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static>
*/
public function scopeDepthFirst(Builder $query): Builder
{
$sql = $query->getExpressionGrammar()->compileOrderByPath();

return $query->orderByRaw($sql);
$query->orderByRaw($sql);

return $query;
}

/**
* Add a recursive expression for the relationship to the query.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @param string $direction
* @param callable $constraint
* @param int $initialDepth
* @param string|null $from
* @param int|null $maxDepth
* @param string $union
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self>
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static>
*/
public function scopeWithRelationshipExpression(
Builder $query,
Expand All @@ -85,7 +93,7 @@ public function scopeWithRelationshipExpression(

$grammar = $query->getExpressionGrammar();

/** @var \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $expression */
/** @var \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $expression */
$expression = $this->getInitialQuery($grammar, $constraint, $initialDepth, $from)
->$union(
$this->getRecursiveQuery($grammar, $direction, $from, $maxDepth)
Expand All @@ -107,7 +115,7 @@ public function scopeWithRelationshipExpression(
* @param callable $constraint
* @param int $initialDepth
* @param string $from
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self>
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static>
*/
protected function getInitialQuery(
ExpressionGrammar $grammar,
Expand All @@ -128,6 +136,7 @@ protected function getInitialQuery(
$this->getPathName()
);

/** @var \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query */
$query = $this->newModelQuery()
->select("$table.*")
->selectRaw($initialDepth . ' as ' . $depth)
Expand All @@ -154,7 +163,7 @@ protected function getInitialQuery(
/**
* Add custom paths to the initial query.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @param \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar $grammar
* @return void
*/
Expand All @@ -173,7 +182,7 @@ protected function addInitialQueryCustomPaths(Builder $query, ExpressionGrammar
/**
* Add pivot columns to the initial query for a relationship expression.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @param \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar $grammar
* @param string $pivotTable
* @param int $initialDepth
Expand Down Expand Up @@ -211,7 +220,7 @@ protected function addInitialQueryPivotColumns(
/**
* Add cycle detection to the initial query for a relationship expression.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @param \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar $grammar
* @return void
*/
Expand All @@ -229,7 +238,7 @@ protected function addInitialQueryCycleDetection(Builder $query, ExpressionGramm
/**
* Add join clauses to the initial query for a relationship expression.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @param string $pivotTable
* @param int $initialDepth
* @return void
Expand Down Expand Up @@ -260,7 +269,7 @@ protected function addInitialQueryJoins(Builder $query, string $pivotTable, int
* @param string $direction
* @param string $from
* @param int|null $maxDepth
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self>
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static>
*/
protected function getRecursiveQuery(
ExpressionGrammar $grammar,
Expand Down Expand Up @@ -296,6 +305,7 @@ protected function getRecursiveQuery(

$recursivePathBindings = $grammar->getRecursivePathBindings($this->getPathSeparator());

/** @var \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query */
$query = $this->newModelQuery()
->select($table . '.*')
->selectRaw($recursiveDepth . ' as ' . $depth)
Expand All @@ -320,7 +330,7 @@ protected function getRecursiveQuery(
/**
* Add customs path to the recursive query for a relationship expression.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @param \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar $grammar
* @return void
*/
Expand All @@ -341,7 +351,7 @@ protected function addRecursiveQueryCustomPaths(Builder $query, ExpressionGramma
/**
* Add pivot columns to the recursive query for a relationship expression.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @param string $pivotTable
* @return void
*/
Expand All @@ -357,7 +367,7 @@ protected function addRecursiveQueryPivotColumns(Builder $query, string $pivotTa
/**
* Add cycle detection to the recursive query for a relationship expression.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @param \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar $grammar
* @return void
*/
Expand Down Expand Up @@ -395,7 +405,7 @@ protected function addRecursiveQueryCycleDetection(Builder $query, ExpressionGra
/**
* Add join and where clauses to the recursive query for a relationship expression.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<self> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<static> $query
* @param string $pivotTable
* @param string $direction
* @param string $name
Expand Down

0 comments on commit 25bc3c2

Please sign in to comment.