Skip to content

Commit a46dff1

Browse files
authored
Use a node key and prevent invalid state (#13)
* Use a node key and prevent invalid state * Check for invalid args
1 parent ad76c72 commit a46dff1

File tree

5 files changed

+225
-152
lines changed

5 files changed

+225
-152
lines changed

src/NestedSetInterface.php

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,50 @@ interface NestedSetInterface {
1212
*
1313
* @param \PNX\NestedSet\Node $target
1414
* The target node to insert below.
15-
* @param \PNX\NestedSet\Node $node
16-
* The node to insert. Only id and revision ID are required.
15+
* @param \PNX\NestedSet\NodeKey $nodeKey
16+
* The node to insert.
1717
*
1818
* @return \PNX\NestedSet\Node
1919
* Returns a new node with position values set.
2020
*/
21-
public function addNodeBelow(Node $target, Node $node);
21+
public function addNodeBelow(Node $target, NodeKey $nodeKey);
2222

2323
/**
2424
* Inserts a node before the target node.
2525
*
2626
* @param \PNX\NestedSet\Node $target
2727
* The target node to insert before.
28-
* @param \PNX\NestedSet\Node $node
29-
* The node to insert. Only id and revision ID are required.
28+
* @param \PNX\NestedSet\NodeKey $nodeKey
29+
* The node key to insert.
3030
*
3131
* @return \PNX\NestedSet\Node
3232
* Returns a node with position values set.
3333
*/
34-
public function addNodeBefore(Node $target, Node $node);
34+
public function addNodeBefore(Node $target, NodeKey $nodeKey);
3535

3636
/**
3737
* Inserts a node after the target node.
3838
*
3939
* @param \PNX\NestedSet\Node $target
4040
* The target node to insert after.
41-
* @param \PNX\NestedSet\Node $node
42-
* The node to insert. Only id and revision ID are required.
41+
* @param \PNX\NestedSet\NodeKey $nodeKey
42+
* The node key to insert.
4343
*
4444
* @return \PNX\NestedSet\Node
4545
* Returns a node with position values set.
4646
*/
47-
public function addNodeAfter(Node $target, Node $node);
47+
public function addNodeAfter(Node $target, NodeKey $nodeKey);
4848

4949
/**
5050
* Inserts a root node.
5151
*
52-
* @param \PNX\NestedSet\Node $node
53-
* The root node.
52+
* @param \PNX\NestedSet\NodeKey $nodeKey
53+
* The node key to insert.
5454
*
5555
* @return \PNX\NestedSet\Node
5656
* A new node with position values set.
5757
*/
58-
public function addRootNode(Node $node);
58+
public function addRootNode(NodeKey $nodeKey);
5959

6060
/**
6161
* Deletes a node and moves descendants up a level.
@@ -76,61 +76,59 @@ public function deleteSubTree(Node $node);
7676
/**
7777
* Finds all descendants of a node.
7878
*
79-
* @param \PNX\NestedSet\Node $node
80-
* The node.
79+
* @param \PNX\NestedSet\NodeKey $nodeKey
80+
* The node key to find descendants for.
8181
* @param int $depth
8282
* (optional) A depth limit. Defaults to 0, no limit.
8383
*
8484
* @return array
85-
* The nested array of descendants.
85+
* The nested array of descendants.
8686
*/
87-
public function findDescendants(Node $node, $depth = 0);
87+
public function findDescendants(NodeKey $nodeKey, $depth = 0);
8888

8989
/**
9090
* Finds all immediate children of a node.
9191
*
92-
* @param \PNX\NestedSet\Node $node
93-
* The node.
92+
* @param \PNX\NestedSet\NodeKey $nodeKey
93+
* The node key to find children for.
9494
*
9595
* @return array
9696
* The children.
9797
*/
98-
public function findChildren(Node $node);
98+
public function findChildren(NodeKey $nodeKey);
9999

100100
/**
101101
* Finds all ancestors of a node.
102102
*
103-
* @param \PNX\NestedSet\Node $node
104-
* The node.
103+
* @param \PNX\NestedSet\NodeKey $nodeKey
104+
* The node to find ancestors for.
105105
*
106106
* @return array
107107
* The ancestors.
108108
*/
109-
public function findAncestors(Node $node);
109+
public function findAncestors(NodeKey $nodeKey);
110110

111111
/**
112112
* Finds the parent node.
113113
*
114-
* @param \PNX\NestedSet\Node $node
115-
* The node.
114+
* @param \PNX\NestedSet\NodeKey $nodeKey
115+
* The node key.
116116
*
117-
* @return Node
117+
* @return \PNX\NestedSet\Node
118118
* The parent node.
119119
*/
120-
public function findParent(Node $node);
120+
public function findParent(NodeKey $nodeKey);
121121

122122
/**
123123
* Gets a node for the ID and Revision ID.
124124
*
125-
* @param int|string $id
126-
* The ID.
127-
* @param int|string $revision_id
128-
* The revision ID.
125+
* @param NodeKey $nodeKey
126+
* The node key.
129127
*
130128
* @return \PNX\NestedSet\Node
131-
* The node.
129+
* The node.
132130
*/
133-
public function getNode($id, $revision_id);
131+
public function getNode(NodeKey $nodeKey);
134132

135133
/**
136134
* Moves a subtree to be a new root of the tree.
@@ -189,4 +187,15 @@ public function getNodeAtPosition($left);
189187
*/
190188
public function getTree();
191189

190+
/**
191+
* Finds the root node for this node.
192+
*
193+
* @param \PNX\NestedSet\NodeKey $nodeKey
194+
* The node key.
195+
*
196+
* @return \PNX\NestedSet\Node
197+
* The root node.
198+
*/
199+
public function findRoot(NodeKey $nodeKey);
200+
192201
}

src/Node.php

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,11 @@
88
class Node {
99

1010
/**
11-
* The node ID.
11+
* The node key.
1212
*
13-
* @var string|int
13+
* @var NodeKey
1414
*/
15-
protected $id;
16-
17-
/**
18-
* The revision ID.
19-
*
20-
* @var string|int
21-
*/
22-
protected $revisionId;
15+
protected $nodeKey;
2316

2417
/**
2518
* The left value.
@@ -45,22 +38,31 @@ class Node {
4538
/**
4639
* Node constructor.
4740
*
48-
* @param int|string $id
49-
* The ID.
50-
* @param int|string $revisionId
51-
* The revision ID.
41+
* @param NodeKey $nodeKey
42+
* The node key.
5243
* @param int $left
5344
* The left value.
5445
* @param int $right
5546
* The right value.
5647
* @param int $depth
5748
* The depth.
5849
*/
59-
public function __construct($id, $revisionId = 0, $left = 0, $right = 0, $depth = 0) {
60-
$this->id = $id;
61-
$this->revisionId = $revisionId;
50+
public function __construct(NodeKey $nodeKey, $left, $right, $depth) {
51+
if ($nodeKey == NULL) {
52+
throw new \InvalidArgumentException("Node key cannot be NULL");
53+
}
54+
$this->nodeKey = $nodeKey;
55+
if ($left < 1) {
56+
throw new \InvalidArgumentException("Left value must be > 0");
57+
}
6258
$this->left = $left;
59+
if ($right < 1) {
60+
throw new \InvalidArgumentException("Right value must be > 0");
61+
}
6362
$this->right = $right;
63+
if ($depth < 0) {
64+
throw new \InvalidArgumentException("Depth value must be >= 0");
65+
}
6466
$this->depth = $depth;
6567
}
6668

@@ -71,7 +73,7 @@ public function __construct($id, $revisionId = 0, $left = 0, $right = 0, $depth
7173
* The ID.
7274
*/
7375
public function getId() {
74-
return $this->id;
76+
return $this->nodeKey->getId();
7577
}
7678

7779
/**
@@ -81,7 +83,17 @@ public function getId() {
8183
* The revision ID.
8284
*/
8385
public function getRevisionId() {
84-
return $this->revisionId;
86+
return $this->nodeKey->getRevisionId();
87+
}
88+
89+
/**
90+
* Gets the node key.
91+
*
92+
* @return \PNX\NestedSet\NodeKey
93+
* The node key.
94+
*/
95+
public function getNodeKey() {
96+
return $this->nodeKey;
8597
}
8698

8799
/**

src/NodeKey.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace PNX\NestedSet;
4+
5+
/**
6+
* Represents the unique key used for a node.
7+
*/
8+
class NodeKey {
9+
10+
/**
11+
* The node ID.
12+
*
13+
* @var string|int
14+
*/
15+
protected $id;
16+
17+
/**
18+
* The revision ID.
19+
*
20+
* @var string|int
21+
*/
22+
protected $revisionId;
23+
24+
/**
25+
* NodeKey constructor.
26+
*
27+
* @param int|string $id
28+
* The node id.
29+
* @param int|string $revisionId
30+
* The node revision id.
31+
*/
32+
public function __construct($id, $revisionId) {
33+
$this->id = $id;
34+
$this->revisionId = $revisionId;
35+
}
36+
37+
/**
38+
* Gets the node id.
39+
*
40+
* @return int|string
41+
* The node id.
42+
*/
43+
public function getId() {
44+
return $this->id;
45+
}
46+
47+
/**
48+
* Gets the node revision id.
49+
*
50+
* @return int|string
51+
* The node revision id.
52+
*/
53+
public function getRevisionId() {
54+
return $this->revisionId;
55+
}
56+
57+
}

0 commit comments

Comments
 (0)