-
-
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.
# Conflicts: # src/IdeHelper/RecursiveRelationsHook.php # tests/IdeHelper/RecursiveRelationsHookTest.php
- Loading branch information
Showing
7 changed files
with
206 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Graph; | ||
|
||
use Illuminate\Database\Eloquent\Collection as Base; | ||
|
||
/** | ||
* @template TKey of array-key | ||
* @template TModel of \Illuminate\Database\Eloquent\Model | ||
* | ||
* @extends \Illuminate\Database\Eloquent\Collection<TKey, TModel> | ||
*/ | ||
class Collection extends Base | ||
{ | ||
/** | ||
* Generate a nested tree. | ||
* | ||
* @param string $childrenRelation | ||
* @return static<int, TModel> | ||
*/ | ||
public function toTree(string $childrenRelation = 'children'): static | ||
{ | ||
if ($this->isEmpty()) { | ||
return $this; | ||
} | ||
|
||
$parentKeyName = $this->first->relationLoaded('pivot') | ||
? 'pivot.' . $this->first()->getParentKeyName() | ||
: 'pivot_' . $this->first()->getParentKeyName(); | ||
$localKeyName = $this->first()->getLocalKeyName(); | ||
$depthName = $this->first()->getDepthName(); | ||
|
||
$depths = $this->pluck($depthName); | ||
|
||
$graph = new static( | ||
$this->where($depthName, $depths->min())->values() | ||
); | ||
|
||
$itemsByParentKey = $this->groupBy($parentKeyName); | ||
|
||
foreach ($this->items as $item) { | ||
$item->setRelation( | ||
$childrenRelation, | ||
$itemsByParentKey[$item->$localKeyName] ?? new static() | ||
); | ||
} | ||
|
||
return $graph; | ||
} | ||
} |
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,96 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\LaravelAdjacencyList\Tests\Graph; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Staudenmeir\LaravelAdjacencyList\Tests\Graph\Models\Node; | ||
use Staudenmeir\LaravelAdjacencyList\Tests\Graph\Models\NodeWithCycleDetection; | ||
use Staudenmeir\LaravelAdjacencyList\Tests\Graph\Models\NodeWithCycleDetectionAndStart; | ||
|
||
class CollectionTest extends TestCase | ||
{ | ||
public function testToTree() | ||
{ | ||
if ($this->database === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$constraint = fn (Builder $query) => $query->whereIn('id', [2, 3]); | ||
|
||
$nodes = Node::subgraph($constraint)->orderBy('id')->get(); | ||
|
||
$graph = $nodes->toTree(); | ||
|
||
$this->assertEquals([2, 3], $graph->pluck('id')->all()); | ||
$this->assertEquals([5], $graph[0]->children->pluck('id')->all()); | ||
$this->assertEquals([7, 8], $graph[0]->children[0]->children->pluck('id')->all()); | ||
$this->assertEquals([8], $graph[0]->children[0]->children[0]->children->pluck('id')->all()); | ||
$this->assertEquals([6], $graph[1]->children->pluck('id')->all()); | ||
} | ||
|
||
public function testToTreeWithRelationship() | ||
{ | ||
$nodes = Node::find(2)->descendants()->orderBy('id')->get(); | ||
|
||
$graph = $nodes->toTree(); | ||
|
||
$this->assertEquals([5], $graph->pluck('id')->all()); | ||
$this->assertEquals([7, 8], $graph[0]->children->pluck('id')->all()); | ||
$this->assertEquals([8], $graph[0]->children[0]->children->pluck('id')->all()); | ||
} | ||
|
||
public function testToTreeWithCycle() | ||
{ | ||
if ($this->database === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$this->seedCycle(); | ||
|
||
$constraint = fn (Builder $query) => $query->where('id', 12); | ||
|
||
$nodes = NodeWithCycleDetection::subgraph($constraint)->orderBy('id')->get(); | ||
|
||
$graph = $nodes->toTree(); | ||
|
||
$this->assertEquals([12], $graph->pluck('id')->all()); | ||
$this->assertEquals([13], $graph[0]->children->pluck('id')->all()); | ||
$this->assertEquals([14], $graph[0]->children[0]->children->pluck('id')->all()); | ||
$this->assertEmpty($graph[0]->children[0]->children[0]->children); | ||
} | ||
|
||
public function testToTreeWithCycleAndStart() | ||
{ | ||
if ($this->database === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$this->seedCycle(); | ||
|
||
$constraint = fn (Builder $query) => $query->where('id', 12); | ||
|
||
$nodes = NodeWithCycleDetectionAndStart::subgraph($constraint)->orderBy('id')->get(); | ||
|
||
$graph = $nodes->toTree(); | ||
|
||
$this->assertEquals([12], $graph->pluck('id')->all()); | ||
$this->assertEquals([13], $graph[0]->children->pluck('id')->all()); | ||
$this->assertEquals([14], $graph[0]->children[0]->children->pluck('id')->all()); | ||
$this->assertEquals([12], $graph[0]->children[0]->children[0]->children->pluck('id')->all()); | ||
} | ||
|
||
public function testToTreeWithEmptyCollection() | ||
{ | ||
if ($this->database === 'sqlsrv') { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$constraint = fn (Builder $query) => $query->where('id', 1); | ||
|
||
$nodes = Node::subgraph($constraint)->where('id', 0)->orderBy('id')->get(); | ||
|
||
$graph = $nodes->toTree(); | ||
|
||
$this->assertEmpty($graph); | ||
} | ||
} |
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