File tree 2 files changed +110
-0
lines changed
spec/drupol/phptree/Exporter
2 files changed +110
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types = 1 );
4
+
5
+ namespace spec \drupol \phptree \Exporter ;
6
+
7
+ use drupol \phptree \Exporter \SimpleArray ;
8
+ use drupol \phptree \Node \ValueNode ;
9
+ use PhpSpec \ObjectBehavior ;
10
+
11
+ class SimpleArraySpec extends ObjectBehavior
12
+ {
13
+ public function it_is_initializable ()
14
+ {
15
+ $ this ->shouldHaveType (SimpleArray::class);
16
+ }
17
+
18
+ public function it_can_export_to_an_array ()
19
+ {
20
+ $ tree = new ValueNode ('root ' , 2 );
21
+
22
+ $ this
23
+ ->export ($ tree )
24
+ ->shouldReturn (['value ' => 'root ' ]);
25
+
26
+ $ nodes = [];
27
+ foreach (\range ('A ' , 'J ' ) as $ value ) {
28
+ $ nodes [$ value ] = new ValueNode ($ value );
29
+ }
30
+
31
+ $ tree ->add (...\array_values ($ nodes ));
32
+
33
+ $ return = [
34
+ 'value ' => 'root ' ,
35
+ 'children ' => [
36
+ 0 => [
37
+ 'value ' => 'A ' ,
38
+ 'children ' => [
39
+ 0 => [
40
+ 'value ' => 'C ' ,
41
+ 'children ' => [
42
+ 0 => [
43
+ 'value ' => 'G ' ,
44
+ ],
45
+ 1 => [
46
+ 'value ' => 'H ' ,
47
+ ],
48
+ ],
49
+ ],
50
+ 1 => [
51
+ 'value ' => 'D ' ,
52
+ 'children ' => [
53
+ 0 => [
54
+ 'value ' => 'I ' ,
55
+ ],
56
+ 1 => [
57
+ 'value ' => 'J ' ,
58
+ ],
59
+ ],
60
+ ],
61
+ ],
62
+ ],
63
+ 1 => [
64
+ 'value ' => 'B ' ,
65
+ 'children ' => [
66
+ 0 => [
67
+ 'value ' => 'E ' ,
68
+ ],
69
+ 1 => [
70
+ 'value ' => 'F ' ,
71
+ ],
72
+ ],
73
+ ],
74
+ ],
75
+ ];
76
+
77
+ $ this
78
+ ->export ($ tree )
79
+ ->shouldReturn ($ return );
80
+ }
81
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types = 1 );
4
+
5
+ namespace drupol \phptree \Exporter ;
6
+
7
+ use drupol \phptree \Node \ValueNodeInterface ;
8
+
9
+ /**
10
+ * Class SimpleArray
11
+ */
12
+ class SimpleArray implements ExporterInterface
13
+ {
14
+ /**
15
+ * {@inheritdoc}
16
+ */
17
+ public function export (ValueNodeInterface $ node )
18
+ {
19
+ $ children = [];
20
+ /** @var ValueNodeInterface $child */
21
+ foreach ($ node ->children () as $ child ) {
22
+ $ children [] = $ this ->export ($ child );
23
+ }
24
+
25
+ return [] === $ children ?
26
+ ['value ' => $ node ->getValue ()]:
27
+ ['value ' => $ node ->getValue (), 'children ' => $ children ];
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments