|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of byrokrat\autogiro. |
|
4
|
|
|
* |
|
5
|
|
|
* byrokrat\autogiro is free software: you can redistribute it and/or |
|
6
|
|
|
* modify it under the terms of the GNU General Public License as published |
|
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* byrokrat\autogiro is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
* |
|
18
|
|
|
* Copyright 2016 Hannes Forsgård |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
|
22
|
|
|
|
|
23
|
|
|
namespace byrokrat\autogiro; |
|
24
|
|
|
|
|
25
|
|
|
use byrokrat\autogiro\Tree\Node; |
|
26
|
|
|
use byrokrat\autogiro\Exception\LogicException; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Tool to enumerate tree nodes of specified types |
|
30
|
|
|
*/ |
|
31
|
|
|
class Enumerator |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Magic method to register enumeration callbacks |
|
35
|
|
|
* |
|
36
|
|
|
* Usage: $enumerator->onNodeType($callback); |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $name Name of node type to capure prefixed with on (eg onNodeType) |
|
39
|
|
|
* @param array $args First argument must be a callable to be called with each capured node |
|
40
|
|
|
* |
|
41
|
|
|
* @throws LogicException If node type or callback is not specified |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __call(string $name, array $args) |
|
44
|
|
|
{ |
|
45
|
|
|
if (!preg_match('/^on[a-zA-Z0-9_]+$/', $name)) { |
|
46
|
|
|
throw new LogicException("Unknown method $name"); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!is_callable($args[0])) { |
|
50
|
|
|
throw new LogicException('Enumeration callback must be callable'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->callbacks[substr($name, 2)] = $args[0]; |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Enumerate nodes in tree |
|
58
|
|
|
*/ |
|
59
|
|
|
public function enumerate(Node $tree) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->dispatch($tree); |
|
62
|
|
|
|
|
63
|
|
|
foreach ($tree->getChildren() as $child) { |
|
64
|
|
|
$this->enumerate($child); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Invoke callback registered with node type |
|
70
|
|
|
*/ |
|
71
|
|
|
private function dispatch(Node $node) |
|
72
|
|
|
{ |
|
73
|
|
|
if (isset($this->callbacks[$node->getType()])) { |
|
74
|
|
|
$this->callbacks[$node->getType()]($node); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: