1 | <?php |
||
14 | abstract class AbstractQuery implements QueryInterface |
||
15 | { |
||
16 | private $dataSource; |
||
17 | /** |
||
18 | * @var OperationInterface[] |
||
19 | */ |
||
20 | private $operations; |
||
21 | |||
22 | private $dynamicFields; |
||
23 | |||
24 | /** |
||
25 | * @return AbstractFactory |
||
26 | */ |
||
27 | abstract protected function getHandlerFactory(); |
||
28 | |||
29 | /** |
||
30 | * AbstractQuery constructor. |
||
31 | * @param $dataSource |
||
32 | * @param string $rowClass |
||
33 | */ |
||
34 | 12 | public function __construct($dataSource, $rowClass) |
|
35 | { |
||
36 | 12 | $this->dataSource = $dataSource; |
|
37 | 12 | $this->dynamicFields = new DynamicFieldsRegistry(); |
|
38 | 12 | $this->operations = [new InitializeRowsOperation($rowClass, $this->dynamicFields)]; |
|
39 | 12 | } |
|
40 | |||
41 | 9 | public function getIterator() |
|
42 | { |
||
43 | 2 | $data = $this->get(); |
|
44 | 3 | if (is_array($data)) { |
|
45 | return new ArrayIterator($data); |
||
46 | } else { |
||
47 | 9 | return $data; |
|
48 | 1 | } |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param $data |
||
53 | * @return RowInterface[]|\Traversable |
||
54 | */ |
||
55 | 11 | protected function execute($data) |
|
56 | { |
||
57 | 11 | foreach ($this->getHandlers() as $handler) { |
|
58 | 11 | $data = $handler->apply($data); |
|
59 | 11 | } |
|
60 | 11 | return $data; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return HandlerInterface[] |
||
65 | */ |
||
66 | 11 | protected function getHandlers() |
|
67 | { |
||
68 | 11 | $handlers = []; |
|
69 | 11 | $factory = $this->getHandlerFactory(); |
|
70 | 11 | foreach ($this->operations as $operation) { |
|
71 | 11 | $handlers[] = $factory->makeHandler($operation); |
|
72 | 11 | } |
|
73 | 11 | usort($handlers, function (HandlerInterface $a, HandlerInterface $b) { |
|
74 | 10 | $aPos = $a->getPriority(); |
|
75 | 10 | $bPos = $b->getPriority(); |
|
76 | 10 | if ($aPos == $bPos) { |
|
77 | 6 | return 0; |
|
78 | } |
||
79 | 10 | return ($aPos < $bPos) ? 1 : -1; |
|
80 | 11 | }); |
|
81 | 11 | return $handlers; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return RowInterface[] |
||
86 | */ |
||
87 | 9 | public function getArray() |
|
92 | |||
93 | /** |
||
94 | * @return RowInterface[]|\Traversable |
||
95 | */ |
||
96 | 11 | public function get() |
|
100 | |||
101 | public function getRaw() |
||
109 | |||
110 | /** |
||
111 | * @param OperationInterface $operation |
||
112 | * @return $this |
||
113 | */ |
||
114 | 10 | public function addOperation(OperationInterface $operation) |
|
119 | |||
120 | 7 | public function addOperations(array $operations) |
|
127 | |||
128 | 4 | public function hasOperation(OperationInterface $operation) |
|
132 | |||
133 | 4 | public function removeOperation(OperationInterface $operation) |
|
143 | |||
144 | /** |
||
145 | * @return Operation\OperationInterface[] |
||
146 | */ |
||
147 | 2 | public function getOperations() |
|
151 | |||
152 | public function getSrc() |
||
156 | } |
||
157 |