6
6
7
7
use drupol \phptree \Node \NodeInterface ;
8
8
use drupol \phptree \Node \ValueNode ;
9
- use drupol \phptree \Node \ValueNodeInterface ;
10
9
11
10
/**
12
11
* Class Text
13
12
*/
14
- class Text implements ImporterInterface
13
+ class Text extends SimpleArray
15
14
{
16
15
/**
17
16
* {@inheritdoc}
@@ -20,30 +19,25 @@ public function import($data): NodeInterface
20
19
{
21
20
$ parsed = $ this ->parse ($ data );
22
21
22
+ if ([] === $ parsed ) {
23
+ throw new \InvalidArgumentException ('Unable to import the given data. ' );
24
+ }
25
+
23
26
return $ this ->arrayToTree ($ parsed [0 ]);
24
27
}
25
28
26
29
/**
27
- * Convert an array into a tree .
30
+ * Create a node .
28
31
*
29
- * @param array $data
32
+ * @param mixed $arguments
33
+ * The arguments.
30
34
*
31
- * @return \drupol\phptree\Node\ValueNodeInterface
32
- * The tree .
35
+ * @return \drupol\phptree\Node\Node
36
+ * The node .
33
37
*/
34
- private function arrayToTree ( array $ data ): ValueNodeInterface
38
+ protected function createNode ( $ arguments ): NodeInterface
35
39
{
36
- $ data += [
37
- 'children ' => [],
38
- ];
39
-
40
- $ node = new ValueNode ($ data ['value ' ]);
41
-
42
- foreach ($ data ['children ' ] as $ key => $ child ) {
43
- $ node ->add ($ this ->arrayToTree ($ child ));
44
- }
45
-
46
- return $ node ;
40
+ return new ValueNode ($ arguments );
47
41
}
48
42
49
43
/**
@@ -57,7 +51,7 @@ private function arrayToTree(array $data): ValueNodeInterface
57
51
*/
58
52
private function parse (string $ subject )
59
53
{
60
- $ result = false ;
54
+ $ result = [] ;
61
55
62
56
\preg_match_all ('~[^\[\]]+|\[(?<nested>(?R)*)\]~ ' , $ subject , $ matches );
63
57
@@ -71,7 +65,7 @@ private function parse(string $subject)
71
65
$ item ['value ' ] = $ match ;
72
66
}
73
67
74
- if (false !== $ children = $ this ->parse ($ match )) {
68
+ if ([] !== $ children = $ this ->parse ($ match )) {
75
69
$ item ['children ' ] = $ children ;
76
70
}
77
71
0 commit comments