Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Structures/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,27 @@ public function tree($tree = null)
return $this->structure()->validateTree($tree, $this->locale());
});
})
->setter(function ($tree) {
return $this->removeNullItems($tree);
})
->args(func_get_args());
}

protected function removeNullItems($tree)
{
return collect($tree)
->reject(fn ($item) => is_null($item))
->map(function ($item) {
if (isset($item['children'])) {
$item['children'] = $this->removeNullItems($item['children']);
}

return $item;
})
->values()
->all();
}

public function root()
{
if (! $this->structure()->expectsRoot()) {
Expand Down
26 changes: 26 additions & 0 deletions tests/Data/Entries/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,32 @@ public function it_gets_the_order_from_the_collections_structure()
$this->assertEquals(3, $four->order());
}

#[Test]
public function it_gets_the_order_from_the_collections_structure_when_the_tree_contains_a_null_item()
{
$collection = tap(Collection::make('ordered'))->save();

$one = tap((new Entry)->locale('en')->id('one')->collection($collection))->save();
$two = tap((new Entry)->locale('en')->id('two')->collection($collection))->save();
$three = tap((new Entry)->locale('en')->id('three')->collection($collection))->save();

$collection->structureContents([
'max_depth' => 3,
])->save();
$collection->structure()->in('en')->tree(
[
['entry' => 'three'],
null,
['entry' => 'one'],
['entry' => 'two'],
]
)->save();

$this->assertEquals(2, $one->order());
$this->assertEquals(3, $two->order());
$this->assertEquals(1, $three->order());
}

#[Test]
public function it_gets_the_order_from_the_data_if_not_structured()
{
Expand Down
38 changes: 36 additions & 2 deletions tests/Data/Structures/TreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ public function the_structure_validates_the_tree_when_getting_it_the_first_time(
$structure = $this->mock(Structure::class);
$structure->shouldReceive('handle')->andReturn('test');

$firstContents = ['first' => 'time'];
$secondContents = ['second' => 'time'];
$firstContents = [['entry' => 'first']];
$secondContents = [['entry' => 'second']];

$structure->shouldReceive('validateTree')->with($firstContents, 'the-locale')->once()->andReturn($firstContents);
$structure->shouldReceive('validateTree')->with($secondContents, 'the-locale')->once()->andReturn($secondContents);
Expand All @@ -451,6 +451,40 @@ public function the_structure_validates_the_tree_when_getting_it_the_first_time(
$tree->tree();
}

#[Test]
public function it_removes_null_items_when_setting_the_tree()
{
$tree = $this->tree([
[
'id' => 'pages-home',
],
null,
[
'id' => 'pages-about',
'children' => [
[
'id' => 'pages-board',
],
null,
],
],
]);

$this->assertEquals([
[
'id' => 'pages-home',
],
[
'id' => 'pages-about',
'children' => [
[
'id' => 'pages-board',
],
],
],
], $tree->tree());
}

#[Test]
public function it_cannot_move_into_root_if_structure_expects_root()
{
Expand Down
Loading