@@ -21,7 +21,7 @@ public function export(NodeInterface $node): string
21
21
new \RecursiveArrayIterator (
22
22
$ this ->doExportAsArray ($ node )
23
23
),
24
- \RecursiveTreeIterator::BYPASS_KEY ,
24
+ \RecursiveTreeIterator::SELF_FIRST ,
25
25
\CachingIterator::CATCH_GET_CHILD ,
26
26
\RecursiveTreeIterator::SELF_FIRST
27
27
);
@@ -46,6 +46,24 @@ public function export(NodeInterface $node): string
46
46
return $ output ;
47
47
}
48
48
49
+ /**
50
+ * Get a string representation of the node.
51
+ *
52
+ * @param \drupol\phptree\Node\NodeInterface $node
53
+ * The node.
54
+ *
55
+ * @return string
56
+ * The node representation.
57
+ */
58
+ protected function getNodeRepresentation (NodeInterface $ node ): string
59
+ {
60
+ if ($ node instanceof ValueNodeInterface) {
61
+ return $ node ->getValue ();
62
+ }
63
+
64
+ return \sha1 (\spl_object_hash ($ node ));
65
+ }
66
+
49
67
/**
50
68
* Export the tree in an array.
51
69
*
@@ -57,10 +75,6 @@ public function export(NodeInterface $node): string
57
75
*/
58
76
private function doExportAsArray (NodeInterface $ node ): array
59
77
{
60
- if (!$ this ->isValidNode ($ node )) {
61
- throw new \InvalidArgumentException ('Must implements ValueNodeInterface ' );
62
- }
63
-
64
78
$ children = [];
65
79
/** @var ValueNodeInterface $child */
66
80
foreach ($ node ->children () as $ child ) {
@@ -71,32 +85,4 @@ private function doExportAsArray(NodeInterface $node): array
71
85
[$ this ->getNodeRepresentation ($ node )] :
72
86
[$ this ->getNodeRepresentation ($ node ), $ children ];
73
87
}
74
-
75
- /**
76
- * Check if a node is valid for being exported.
77
- *
78
- * @param \drupol\phptree\Node\NodeInterface $node
79
- * The node.
80
- *
81
- * @return bool
82
- * True if it's valid, false otherwise.
83
- */
84
- protected function isValidNode (NodeInterface $ node ): bool
85
- {
86
- return $ node instanceof ValueNodeInterface;
87
- }
88
-
89
- /**
90
- * Get a string representation of the node.
91
- *
92
- * @param \drupol\phptree\Node\NodeInterface $node
93
- * The node.
94
- *
95
- * @return string
96
- * The node representation.
97
- */
98
- protected function getNodeRepresentation (NodeInterface $ node ): string
99
- {
100
- return $ node ->getValue ();
101
- }
102
88
}
0 commit comments