1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nayjest\Querying; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use Exception; |
7
|
|
|
use IteratorAggregate; |
8
|
|
|
use Nayjest\Querying\Handler\AbstractFactory; |
9
|
|
|
use Nayjest\Querying\Handler\HandlerInterface; |
10
|
|
|
use Nayjest\Querying\Operation\HydrateOperation; |
11
|
|
|
use Nayjest\Querying\Operation\OperationInterface; |
12
|
|
|
use Nayjest\Querying\Row\DynamicFieldsRegistry; |
13
|
|
|
use Nayjest\Querying\Row\RowInterface; |
14
|
|
|
|
15
|
|
|
abstract class AbstractQuery implements IteratorAggregate |
16
|
|
|
{ |
17
|
|
|
private $dataSource; |
18
|
|
|
/** |
19
|
|
|
* @var OperationInterface[] |
20
|
|
|
*/ |
21
|
|
|
private $operations; |
22
|
|
|
|
23
|
|
|
private $dynamicFields; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return AbstractFactory |
27
|
|
|
*/ |
28
|
|
|
abstract protected function getHandlerFactory(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* AbstractQuery constructor. |
32
|
|
|
* @param $dataSource |
33
|
|
|
* @param string $rowClass |
34
|
|
|
*/ |
35
|
11 |
|
public function __construct($dataSource, $rowClass) |
36
|
|
|
{ |
37
|
11 |
|
$this->dataSource = $dataSource; |
38
|
11 |
|
$this->dynamicFields = new DynamicFieldsRegistry(); |
39
|
11 |
|
$this->operations = [new HydrateOperation($rowClass, $this->dynamicFields)]; |
40
|
11 |
|
$this->initialize(); |
41
|
11 |
|
} |
42
|
|
|
|
43
|
7 |
|
public function getIterator() |
44
|
|
|
{ |
45
|
7 |
|
$data = $this->get(); |
46
|
2 |
|
if (is_array($data)) { |
47
|
|
|
return new ArrayIterator($data); |
48
|
|
|
} else { |
49
|
2 |
|
return $data; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
5 |
|
protected function initialize() |
54
|
|
|
{ |
55
|
|
|
|
56
|
5 |
|
} |
57
|
|
|
|
58
|
10 |
|
protected function execute($data) |
59
|
|
|
{ |
60
|
10 |
|
foreach ($this->getHandlers() as $handler) { |
61
|
10 |
|
$data = $handler->apply($data); |
62
|
10 |
|
} |
63
|
10 |
|
return $data; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return HandlerInterface[] |
68
|
|
|
*/ |
69
|
10 |
|
protected function getHandlers() |
70
|
|
|
{ |
71
|
10 |
|
$handlers = []; |
72
|
10 |
|
$factory = $this->getHandlerFactory(); |
73
|
10 |
|
foreach ($this->operations as $operation) { |
74
|
10 |
|
$handlers[] = $factory->makeHandler($operation); |
75
|
10 |
|
} |
76
|
10 |
|
usort($handlers, function (HandlerInterface $a, HandlerInterface $b) { |
77
|
10 |
|
$aPos = $a->getPriority(); |
78
|
10 |
|
$bPos = $b->getPriority(); |
79
|
10 |
|
if ($aPos == $bPos) { |
80
|
6 |
|
return 0; |
81
|
|
|
} |
82
|
10 |
|
return ($aPos < $bPos) ? -1 : 1; |
83
|
10 |
|
}); |
84
|
10 |
|
return $handlers; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return RowInterface[]|\Traversable |
89
|
|
|
*/ |
90
|
10 |
|
public function get() |
91
|
|
|
{ |
92
|
10 |
|
return $this->execute($this->dataSource); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return RowInterface[] |
97
|
|
|
*/ |
98
|
8 |
|
public function getArray() |
99
|
|
|
{ |
100
|
8 |
|
$data = $this->get(); |
101
|
8 |
|
return is_array($data) ? $data : iterator_to_array($data); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function getRaw() |
108
|
|
|
{ |
109
|
|
|
$data = $this->get(); |
110
|
|
|
$res = []; |
111
|
|
|
foreach ($data as $row) { |
112
|
|
|
$res[] = $row->getSrc(); |
113
|
|
|
} |
114
|
|
|
return $res; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param OperationInterface $operation |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
10 |
|
public function addOperation(OperationInterface $operation) |
122
|
|
|
{ |
123
|
10 |
|
$this->operations[] = $operation; |
124
|
10 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
7 |
|
public function addOperations(array $operations) |
128
|
|
|
{ |
129
|
7 |
|
foreach ($operations as $operation) { |
130
|
7 |
|
$this->addOperation($operation); |
131
|
7 |
|
} |
132
|
7 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
public function hasOperation(OperationInterface $operation) |
136
|
|
|
{ |
137
|
2 |
|
return in_array($operation, $this->operations); |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
public function removeOperation(OperationInterface $operation) |
141
|
|
|
{ |
142
|
2 |
|
if ($this->hasOperation($operation)) { |
143
|
2 |
|
$key = array_search($operation, $this->operations); |
144
|
2 |
|
unset($this->operations[$key]); |
145
|
2 |
|
} else { |
146
|
|
|
throw new Exception("Trying to remove unexisting operation from query"); |
147
|
|
|
} |
148
|
2 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getSrc() |
152
|
|
|
{ |
153
|
|
|
return $this->dataSource; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|