|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nayjest\Querying; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayIterator; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Nayjest\Querying\Handler\AbstractFactory; |
|
8
|
|
|
use Nayjest\Querying\Handler\HandlerInterface; |
|
9
|
|
|
use Nayjest\Querying\Operation\InitializeRowsOperation; |
|
10
|
|
|
use Nayjest\Querying\Operation\OperationInterface; |
|
11
|
|
|
use Nayjest\Querying\Row\DynamicFieldsRegistry; |
|
12
|
|
|
use Nayjest\Querying\Row\RowInterface; |
|
13
|
|
|
|
|
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() |
|
88
|
|
|
{ |
|
89
|
9 |
|
$data = $this->get(); |
|
90
|
9 |
|
return is_array($data) ? $data : iterator_to_array($data); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return RowInterface[]|\Traversable |
|
95
|
|
|
*/ |
|
96
|
11 |
|
public function get() |
|
97
|
|
|
{ |
|
98
|
11 |
|
return $this->execute($this->dataSource); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getRaw() |
|
102
|
|
|
{ |
|
103
|
|
|
$res = []; |
|
104
|
|
|
foreach ($this->get() as $row) { |
|
105
|
|
|
$res[] = $row->extract(); |
|
106
|
|
|
} |
|
107
|
|
|
return $res; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param OperationInterface $operation |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
10 |
|
public function addOperation(OperationInterface $operation) |
|
115
|
|
|
{ |
|
116
|
10 |
|
$this->operations[] = $operation; |
|
117
|
10 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
7 |
|
public function addOperations(array $operations) |
|
121
|
|
|
{ |
|
122
|
7 |
|
foreach ($operations as $operation) { |
|
123
|
7 |
|
$this->addOperation($operation); |
|
124
|
7 |
|
} |
|
125
|
7 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
4 |
|
public function hasOperation(OperationInterface $operation) |
|
129
|
|
|
{ |
|
130
|
4 |
|
return in_array($operation, $this->operations); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
4 |
|
public function removeOperation(OperationInterface $operation) |
|
134
|
|
|
{ |
|
135
|
4 |
|
if ($this->hasOperation($operation)) { |
|
136
|
4 |
|
$key = array_search($operation, $this->operations); |
|
137
|
4 |
|
unset($this->operations[$key]); |
|
138
|
4 |
|
} else { |
|
139
|
|
|
throw new Exception("Trying to remove unexisting operation from query"); |
|
140
|
|
|
} |
|
141
|
4 |
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return Operation\OperationInterface[] |
|
146
|
|
|
*/ |
|
147
|
2 |
|
public function getOperations() |
|
148
|
|
|
{ |
|
149
|
2 |
|
return $this->operations; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function getSrc() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->dataSource; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|