1 | <?php |
||
13 | class QueryImpl implements Query { |
||
14 | /** |
||
15 | * @var Graph |
||
16 | */ |
||
17 | protected $graph; |
||
18 | |||
19 | /** |
||
20 | * @var array[] |
||
21 | */ |
||
22 | protected $steps; |
||
23 | |||
24 | 50 | public function __construct(Graph $graph) { |
|
28 | |||
29 | /** |
||
30 | * @inheritdocs |
||
31 | */ |
||
32 | 25 | public function expand(\Closure $expander) { |
|
38 | |||
39 | /** |
||
40 | * @inheritdocs |
||
41 | */ |
||
42 | 50 | public function extract(\Closure $extractor) { |
|
48 | |||
49 | /** |
||
50 | * @inheritdocs |
||
51 | */ |
||
52 | 44 | public function filter(\Closure $filter) { |
|
58 | |||
59 | /** |
||
60 | * @inheritdocs |
||
61 | */ |
||
62 | 50 | public function run($result) { |
|
63 | 50 | $nodes = $this->add_result($this->graph->nodes(), $result); |
|
64 | |||
65 | 50 | foreach ($this->steps as $step) { |
|
66 | 50 | if (count($nodes) == 0) { |
|
67 | 14 | return []; |
|
68 | } |
||
69 | |||
70 | 49 | list($cmd,$clsr) = $step; |
|
71 | 49 | if ($cmd == "expand") { |
|
72 | 22 | $nodes = $this->run_expand($nodes, $clsr); |
|
73 | 22 | } |
|
74 | 48 | elseif ($cmd == "extract") { |
|
75 | 36 | $this->run_extract($nodes, $clsr); |
|
76 | 36 | } |
|
77 | 44 | elseif ($cmd == "filter") { |
|
78 | 44 | $nodes = $this->run_filter($nodes, $clsr); |
|
79 | 44 | } |
|
80 | else { |
||
81 | throw new \LogicException("Unknown command: $cmd"); |
||
82 | } |
||
83 | 49 | } |
|
84 | |||
85 | return array_values(array_map(function($r) { |
||
86 | 36 | return $r[1]; |
|
87 | 36 | }, $nodes)); |
|
88 | } |
||
89 | |||
90 | 22 | protected function run_expand(array &$nodes, \Closure $clsr) { |
|
101 | |||
102 | 36 | protected function run_extract(array &$nodes, \Closure $clsr) { |
|
114 | |||
115 | 44 | protected function run_filter(array &$nodes, \Closure $clsr) { |
|
125 | |||
126 | 50 | protected function add_result(array $nodes, &$result) { |
|
133 | |||
134 | // Convenience Functions |
||
135 | |||
136 | /** |
||
137 | * @inheritdocs |
||
138 | */ |
||
139 | 12 | public function filter_by_types(array $types) { |
|
144 | |||
145 | /** |
||
146 | * @inheritdocs |
||
147 | */ |
||
148 | 20 | public function expand_relations(array $types) { |
|
157 | |||
158 | /** |
||
159 | * @inheritdocs |
||
160 | */ |
||
161 | public function expand_target() { |
||
166 | } |
||
167 |