Skip to content

Commit 1acf3ea

Browse files
committed
refactor: rename branch to pathFromRoot and createLeaf() to withLeaf() in Ltree and LtreeInterface, update related methods and tests
1 parent eeaf8d3 commit 1acf3ea

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

docs/LTREE-TYPE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ class MyEntity implements \Stringable
122122
throw new \InvalidArgumentException("Parent MyEntity can't be self");
123123
}
124124

125-
if (in_array($this->getId()->toBase58(), $parent->getPath()->getBranch(), true)) {
125+
if (in_array($this->getId()->toBase58(), $parent->getPath()->getPathFromRoot(), true)) {
126126
throw new \InvalidArgumentException("Parent MyEntity can't be a child of the current MyEntity");
127127
}
128128

129129
$this->parent = $parent;
130130

131-
// Use createLeaf() to create a new Ltree instance
131+
// Use withLeaf() to create a new Ltree instance
132132
// with the parent's path and the current entity's ID.
133-
$this->path = $parent->getPath()->createLeaf($this->id->toBase58());
133+
$this->path = $parent->getPath()->withLeaf($this->id->toBase58());
134134
}
135135
}
136136
```

src/MartinGeorgiev/Doctrine/DBAL/Types/ValueObject/Ltree.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
class Ltree implements LtreeInterface
1111
{
1212
/**
13-
* @param list<non-empty-string> $branch
13+
* @param list<non-empty-string> $pathFromRoot
1414
*
15-
* @throws \InvalidArgumentException if the branch contains empty strings or is not a list
15+
* @throws \InvalidArgumentException if the pathFromRoot contains empty strings or is not a list
1616
*/
1717
public function __construct(
18-
private readonly array $branch,
18+
private readonly array $pathFromRoot,
1919
) {
20-
self::assertListOfNonEmptyStrings($branch);
20+
self::assertListOfNonEmptyStrings($pathFromRoot);
2121
}
2222

2323
#[\Override]
2424
public function __toString(): string
2525
{
26-
return \implode('.', $this->branch);
26+
return \implode('.', $this->pathFromRoot);
2727
}
2828

2929
#[\Override]
@@ -33,19 +33,19 @@ public static function fromString(string $ltree): static
3333
return new static([]);
3434
}
3535

36-
$branch = \explode('.', $ltree);
36+
$pathFromRoot = \explode('.', $ltree);
3737

38-
return new static($branch); // @phpstan-ignore-line argument.type
38+
return new static($pathFromRoot); // @phpstan-ignore-line argument.type
3939
}
4040

4141
#[\Override]
4242
public function jsonSerialize(): array
4343
{
44-
return $this->branch;
44+
return $this->pathFromRoot;
4545
}
4646

4747
#[\Override]
48-
public function createLeaf(string $leaf): static
48+
public function withLeaf(string $leaf): static
4949
{
5050
if ('' === $leaf) {
5151
throw new \InvalidArgumentException('Leaf cannot be empty.');
@@ -55,49 +55,49 @@ public function createLeaf(string $leaf): static
5555
throw new \InvalidArgumentException('Leaf cannot contain dot.');
5656
}
5757

58-
$newBranch = [...$this->branch, $leaf];
58+
$newBranch = [...$this->pathFromRoot, $leaf];
5959

6060
return new static($newBranch);
6161
}
6262

6363
#[\Override]
64-
public function getBranch(): array
64+
public function getPathFromRoot(): array
6565
{
66-
return $this->branch;
66+
return $this->pathFromRoot;
6767
}
6868

6969
#[\Override]
7070
public function equals(LtreeInterface $ltree): bool
7171
{
72-
return $this->branch === $ltree->getBranch();
72+
return $this->pathFromRoot === $ltree->getPathFromRoot();
7373
}
7474

7575
#[\Override]
7676
public function isAncestorOf(LtreeInterface $ltree): bool
7777
{
78-
return [] === $this->branch || \str_starts_with((string) $ltree, \sprintf('%s.', (string) $this));
78+
return [] === $this->pathFromRoot || \str_starts_with((string) $ltree, \sprintf('%s.', (string) $this));
7979
}
8080

8181
#[\Override]
82-
public function isDescendantOf(LtreeInterface $ltree): bool
82+
public function isLeafOf(LtreeInterface $ltree): bool
8383
{
8484
return \str_starts_with((string) $this, \sprintf('%s.', (string) $ltree));
8585
}
8686

8787
#[\Override]
8888
public function isRoot(): bool
8989
{
90-
return 1 >= \count($this->branch);
90+
return 1 >= \count($this->pathFromRoot);
9191
}
9292

9393
#[\Override]
9494
public function getParent(): static
9595
{
96-
if ([] === $this->branch) {
96+
if ([] === $this->pathFromRoot) {
9797
throw new \LogicException('Empty ltree has no parent.');
9898
}
9999

100-
$parentBranch = \array_slice($this->branch, 0, -1);
100+
$parentBranch = \array_slice($this->pathFromRoot, 0, -1);
101101
self::assertListOfNonEmptyStrings($parentBranch);
102102

103103
return new static($parentBranch);

src/MartinGeorgiev/Doctrine/DBAL/Types/ValueObject/LtreeInterface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
interface LtreeInterface extends \Stringable, \JsonSerializable
88
{
99
/**
10-
* @param list<non-empty-string> $branch
10+
* @param list<non-empty-string> $pathFromRoot
1111
*
12-
* @throws \InvalidArgumentException if the branch is empty
12+
* @throws \InvalidArgumentException if the pathFromRoot is empty
1313
*/
14-
public function __construct(array $branch);
14+
public function __construct(array $pathFromRoot);
1515

1616
/**
1717
* @throws \InvalidArgumentException if the ltree is empty
@@ -29,18 +29,18 @@ public function jsonSerialize(): array;
2929
*
3030
* @throws \InvalidArgumentException if the leaf is empty or contains dot
3131
*/
32-
public function createLeaf(string $leaf): static;
32+
public function withLeaf(string $leaf): static;
3333

3434
/**
3535
* @return list<non-empty-string>
3636
*/
37-
public function getBranch(): array;
37+
public function getPathFromRoot(): array;
3838

3939
public function equals(LtreeInterface $ltree): bool;
4040

4141
public function isAncestorOf(LtreeInterface $ltree): bool;
4242

43-
public function isDescendantOf(LtreeInterface $ltree): bool;
43+
public function isLeafOf(LtreeInterface $ltree): bool;
4444

4545
public function isRoot(): bool;
4646

tests/Unit/MartinGeorgiev/Doctrine/DBAL/Types/ValueObject/LtreeTest.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function test_construct_trows_on_non_list(): void
2121
new Ltree([0 => 'a', 2 => 'b', 3 => 'c']); // @phpstan-ignore argument.type
2222
}
2323

24-
public function test_construct_trows_on_empty_string_in_branch(): void
24+
public function test_construct_trows_on_empty_string_in_path_from_root(): void
2525
{
2626
$this->expectException(\InvalidArgumentException::class);
2727
new Ltree(['a', '', 'c']); // @phpstan-ignore argument.type
@@ -30,22 +30,22 @@ public function test_construct_trows_on_empty_string_in_branch(): void
3030
public function test_from_string(): void
3131
{
3232
$ltree = Ltree::fromString('x.y.z');
33-
self::assertSame(['x', 'y', 'z'], $ltree->getBranch());
33+
self::assertSame(['x', 'y', 'z'], $ltree->getPathFromRoot());
3434
self::assertSame('x.y.z', (string) $ltree);
3535
}
3636

3737
public function test_from_string_empty(): void
3838
{
3939
$ltree = Ltree::fromString('');
40-
self::assertSame([], $ltree->getBranch());
40+
self::assertSame([], $ltree->getPathFromRoot());
4141
self::assertSame('', (string) $ltree);
4242
}
4343

4444
public function test_json_serialize(): void
4545
{
46-
$branch = ['a', 'b', 'c'];
47-
$ltree = new Ltree($branch);
48-
self::assertSame($branch, $ltree->jsonSerialize());
46+
$pathFromRoot = ['a', 'b', 'c'];
47+
$ltree = new Ltree($pathFromRoot);
48+
self::assertSame($pathFromRoot, $ltree->jsonSerialize());
4949
}
5050

5151
public function test_json_encode(): void
@@ -55,11 +55,11 @@ public function test_json_encode(): void
5555
self::assertSame('["a","b","c"]', $json);
5656
}
5757

58-
public function test_create_leaf(): void
58+
public function test_with_leaf(): void
5959
{
6060
$ltree = new Ltree(['root']);
61-
$newLtree = $ltree->createLeaf('leaf');
62-
self::assertSame(['root', 'leaf'], $newLtree->getBranch());
61+
$newLtree = $ltree->withLeaf('leaf');
62+
self::assertSame(['root', 'leaf'], $newLtree->getPathFromRoot());
6363
self::assertSame('root.leaf', (string) $newLtree);
6464
}
6565

@@ -75,17 +75,17 @@ public function test_equals(): void
7575
public function test_is_ancestor_of(): void
7676
{
7777
$ancestor = new Ltree(['a', 'b']);
78-
$descendant = new Ltree(['a', 'b', 'c']);
79-
self::assertTrue($ancestor->isAncestorOf($descendant));
80-
self::assertFalse($descendant->isAncestorOf($ancestor));
78+
$leaf = new Ltree(['a', 'b', 'c']);
79+
self::assertTrue($ancestor->isAncestorOf($leaf));
80+
self::assertFalse($leaf->isAncestorOf($ancestor));
8181
}
8282

83-
public function test_is_descendant_of(): void
83+
public function test_is_leaf_of(): void
8484
{
8585
$ancestor = new Ltree(['a', 'b']);
86-
$descendant = new Ltree(['a', 'b', 'c']);
87-
self::assertTrue($descendant->isDescendantOf($ancestor));
88-
self::assertFalse($ancestor->isDescendantOf($descendant));
86+
$leaf = new Ltree(['a', 'b', 'c']);
87+
self::assertTrue($leaf->isLeafOf($ancestor));
88+
self::assertFalse($ancestor->isLeafOf($leaf));
8989
}
9090

9191
public function test_is_root(): void
@@ -102,7 +102,7 @@ public function test_get_parent(): void
102102
{
103103
$ltree = new Ltree(['a', 'b', 'c']);
104104
$parent = $ltree->getParent();
105-
self::assertSame(['a', 'b'], $parent->getBranch());
105+
self::assertSame(['a', 'b'], $parent->getPathFromRoot());
106106
self::assertSame('a.b', (string) $parent);
107107
}
108108

@@ -111,15 +111,15 @@ public function test_get_parent_respect_immutability(): void
111111
$ltree = new Ltree(['a', 'b', 'c']);
112112
$parent = $ltree->getParent();
113113
self::assertNotSame($ltree, $parent);
114-
self::assertSame(['a', 'b', 'c'], $ltree->getBranch());
114+
self::assertSame(['a', 'b', 'c'], $ltree->getPathFromRoot());
115115
self::assertSame('a.b.c', (string) $ltree);
116116
}
117117

118118
public function test_get_parent_on_root(): void
119119
{
120120
$ltree = new Ltree(['a']);
121121
$parent = $ltree->getParent();
122-
self::assertSame([], $parent->getBranch());
122+
self::assertSame([], $parent->getPathFromRoot());
123123
}
124124

125125
public function test_get_parent_throws_on_empty(): void
@@ -128,17 +128,17 @@ public function test_get_parent_throws_on_empty(): void
128128
(new Ltree([]))->getParent();
129129
}
130130

131-
public function test_create_leaf_empty_throws(): void
131+
public function test_with_leaf_empty_throws(): void
132132
{
133133
$ltree = new Ltree(['a']);
134134
$this->expectException(\InvalidArgumentException::class);
135-
$ltree->createLeaf(''); // @phpstan-ignore argument.type
135+
$ltree->withLeaf(''); // @phpstan-ignore argument.type
136136
}
137137

138-
public function test_create_leaf_with_dot_throws(): void
138+
public function test_with_leaf_with_dot_throws(): void
139139
{
140140
$ltree = new Ltree(['a']);
141141
$this->expectException(\InvalidArgumentException::class);
142-
$ltree->createLeaf('foo.bar');
142+
$ltree->withLeaf('foo.bar');
143143
}
144144
}

0 commit comments

Comments
 (0)