1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace BitWasp\Bitcoin\Script\Path; |
||
6 | |||
7 | class LogicOpNode |
||
8 | { |
||
9 | /** |
||
10 | * @var LogicOpNode|null |
||
11 | */ |
||
12 | private $parent; |
||
13 | |||
14 | /** |
||
15 | * @var bool |
||
16 | */ |
||
17 | private $value; |
||
18 | |||
19 | /** |
||
20 | * @var LogicOpNode[] |
||
21 | */ |
||
22 | private $children = []; |
||
23 | |||
24 | /** |
||
25 | * MASTNode constructor. |
||
26 | * @param LogicOpNode|null $parent |
||
27 | * @param bool|null $value |
||
28 | */ |
||
29 | 164 | public function __construct(LogicOpNode $parent = null, bool $value = null) |
|
30 | { |
||
31 | 164 | $this->parent = $parent; |
|
32 | 164 | $this->value = $value; |
|
33 | 164 | } |
|
34 | |||
35 | /** |
||
36 | * @return array |
||
37 | */ |
||
38 | 155 | public function flags() |
|
39 | { |
||
40 | 155 | if (count($this->children) > 0) { |
|
41 | 35 | $values = []; |
|
42 | 35 | foreach ($this->children as $k => $child) { |
|
43 | 35 | $flags = $child->flags(); |
|
44 | 35 | foreach ($flags as $branch) { |
|
45 | 35 | $values[] = array_merge($this->isRoot() ? [] : [$this->value], is_array($branch) ? $branch : [$branch]); |
|
46 | } |
||
47 | } |
||
48 | |||
49 | 35 | return $values; |
|
50 | } else { |
||
51 | 155 | $value = $this->value; |
|
52 | 155 | if ($value === null) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
53 | 120 | return [[]]; |
|
54 | } |
||
55 | 35 | return [$value]; |
|
56 | } |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @return bool |
||
61 | */ |
||
62 | 160 | public function isRoot(): bool |
|
63 | { |
||
64 | 160 | return $this->parent == null; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return bool |
||
69 | */ |
||
70 | 138 | public function hasChildren(): bool |
|
71 | { |
||
72 | 138 | return count($this->children) > 0; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @return LogicOpNode|null |
||
77 | */ |
||
78 | 37 | public function getParent() |
|
79 | { |
||
80 | 37 | return $this->parent; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return bool|null |
||
85 | */ |
||
86 | 33 | public function getValue() |
|
87 | { |
||
88 | 33 | return $this->value; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param int $value |
||
93 | * @return LogicOpNode |
||
94 | */ |
||
95 | 34 | public function getChild(int $value): LogicOpNode |
|
96 | { |
||
97 | 34 | if (!array_key_exists($value, $this->children)) { |
|
98 | 1 | throw new \RuntimeException("Child not found"); |
|
99 | } |
||
100 | 33 | return $this->children[$value]; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * @return array |
||
105 | */ |
||
106 | 38 | public function split(): array |
|
107 | { |
||
108 | 38 | if (count($this->children) > 0) { |
|
109 | 1 | throw new \RuntimeException("Sanity check - don't split twice"); |
|
110 | } |
||
111 | |||
112 | 38 | $children = [new LogicOpNode($this, false), new LogicOpNode($this, true)]; |
|
113 | 38 | foreach ($children as $child) { |
|
114 | 38 | $this->children[] = $child; |
|
115 | } |
||
116 | 38 | return $children; |
|
117 | } |
||
118 | } |
||
119 |