Skip to content

Commit 9eecefa

Browse files
committed
Add new method NodeInterface::label() so I can fix the MerkleNode properly.
1 parent a282a07 commit 9eecefa

File tree

7 files changed

+68
-21
lines changed

7 files changed

+68
-21
lines changed

spec/drupol/phptree/Node/MerkleNodeSpec.php

+30-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,32 @@ public function it_can_get_a_hash()
5454

5555
$this
5656
->add(...$nodes)
57-
->getValue()
57+
->hash()
5858
->shouldReturn('c689102cdf2a5b30c2e21fdad85e4bb401085227aff672a7240ceb3410ff1fb6');
5959
}
6060

61+
public function it_can_get_its_label()
62+
{
63+
$this
64+
->label()
65+
->shouldReturn('root');
66+
67+
$nodes = [
68+
new MerkleNode(null, 2, new DummyHasher()),
69+
new MerkleNode(null, 2, new DummyHasher()),
70+
new MerkleNode('a', 2, new DummyHasher()),
71+
new MerkleNode('b', 2, new DummyHasher()),
72+
new MerkleNode('c', 2, new DummyHasher()),
73+
];
74+
75+
$this
76+
->add(...$nodes);
77+
78+
$this
79+
->label()
80+
->shouldReturn('abcc');
81+
}
82+
6183
public function it_can_get_the_value_of_a_tree_with_a_single_node()
6284
{
6385
$this
@@ -83,7 +105,7 @@ public function it_can_get_the_value_of_a_tree_with_four_nodes()
83105
->shouldReturn(5);
84106

85107
$this
86-
->getValue()
108+
->hash()
87109
->shouldReturn('abcc');
88110

89111
$this
@@ -100,7 +122,7 @@ public function it_can_get_the_value_of_a_tree_with_three_nodes()
100122

101123
$this
102124
->add(...$nodes)
103-
->getValue()
125+
->hash()
104126
->shouldReturn('ab');
105127
}
106128

@@ -110,7 +132,7 @@ public function it_can_get_the_value_of_a_tree_with_two_nodes()
110132

111133
$this
112134
->add($node)
113-
->getValue()
135+
->hash()
114136
->shouldReturn('aa');
115137
}
116138

@@ -134,6 +156,10 @@ public function it_can_handle_null_values()
134156

135157
$this
136158
->getValue()
159+
->shouldReturn('root');
160+
161+
$this
162+
->hash()
137163
->shouldReturn('abcc');
138164

139165
$this

src/Exporter/AbstractExporter.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace drupol\phptree\Exporter;
66

77
use drupol\phptree\Node\NodeInterface;
8-
use drupol\phptree\Node\ValueNodeInterface;
98

109
/**
1110
* Class AbstractExporter.
@@ -23,10 +22,6 @@ abstract class AbstractExporter implements ExporterInterface
2322
*/
2423
protected function getNodeRepresentation(NodeInterface $node): string
2524
{
26-
if ($node instanceof ValueNodeInterface) {
27-
return (string) $node->getValue();
28-
}
29-
30-
return sha1(spl_object_hash($node));
25+
return $node->label();
3126
}
3227
}

src/Node/MerkleNode.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,18 @@ public function __construct(
4949
/**
5050
* {@inheritdoc}
5151
*/
52-
public function getValue()
52+
public function hash(): string
53+
{
54+
return $this->hasher->unpack($this->doHash($this->normalize()));
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function label(): string
5361
{
5462
if (true === $this->isLeaf()) {
55-
return parent::getValue();
63+
return $this->getValue();
5664
}
5765

5866
return $this->hash();
@@ -79,7 +87,7 @@ private function doHash(MerkleNodeInterface $node): string
7987
{
8088
// If node is a leaf, then compute its hash from its value.
8189
if (true === $node->isLeaf()) {
82-
return $this->hasher->hash((string) $node->getValue());
90+
return $this->hasher->hash($node->getValue());
8391
}
8492

8593
$hash = '';
@@ -90,12 +98,4 @@ private function doHash(MerkleNodeInterface $node): string
9098

9199
return $this->hasher->hash($hash);
92100
}
93-
94-
/**
95-
* {@inheritdoc}
96-
*/
97-
private function hash(): string
98-
{
99-
return $this->hasher->unpack($this->doHash($this->normalize()));
100-
}
101101
}

src/Node/MerkleNodeInterface.php

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
*/
1010
interface MerkleNodeInterface extends ValueNodeInterface
1111
{
12+
/**
13+
* @return string
14+
*/
15+
public function hash(): string;
16+
1217
/**
1318
* @return \drupol\phptree\Node\MerkleNodeInterface
1419
*/

src/Node/Node.php

+8
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,14 @@ public function isRoot(): bool
229229
return null === $this->parent;
230230
}
231231

232+
/**
233+
* {@inheritdoc}
234+
*/
235+
public function label(): string
236+
{
237+
return sha1(spl_object_hash($this));
238+
}
239+
232240
/**
233241
* {@inheritdoc}
234242
*/

src/Node/NodeInterface.php

+5
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ public function isLeaf(): bool;
138138
*/
139139
public function isRoot(): bool;
140140

141+
/**
142+
* @return string
143+
*/
144+
public function label(): string;
145+
141146
/**
142147
* @param int $level
143148
*

src/Node/ValueNode.php

+8
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ public function getValue()
4242
{
4343
return $this->value;
4444
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function label(): string
50+
{
51+
return (string) $this->getValue();
52+
}
4553
}

0 commit comments

Comments
 (0)