1 | <?php |
||
2 | |||
3 | namespace Bdf\Prime\Query; |
||
4 | |||
5 | use Bdf\Prime\Collection\CollectionFactory; |
||
6 | use Bdf\Prime\Collection\CollectionInterface; |
||
7 | use Bdf\Prime\Connection\ConnectionInterface; |
||
8 | use Bdf\Prime\Connection\Result\ResultSetInterface; |
||
9 | use Bdf\Prime\Query\Compiler\CompilerInterface; |
||
10 | use Bdf\Prime\Query\Compiler\CompilerState; |
||
11 | use Bdf\Prime\Query\Compiler\Preprocessor\PreprocessorInterface; |
||
12 | use Bdf\Prime\Query\Extension\CachableTrait; |
||
13 | use Bdf\Prime\Query\Extension\ExecutableTrait; |
||
14 | |||
15 | /** |
||
16 | * Abstract class for read operations |
||
17 | * |
||
18 | * @template C as ConnectionInterface |
||
19 | * @template R as object|array |
||
20 | * |
||
21 | * @implements ReadCommandInterface<C, R> |
||
22 | */ |
||
23 | abstract class AbstractReadCommand extends CompilableClause implements ReadCommandInterface |
||
24 | { |
||
25 | use CachableTrait; |
||
26 | use ExecutableTrait; |
||
27 | |||
28 | /** |
||
29 | * The DBAL Connection. |
||
30 | * |
||
31 | * @var C |
||
32 | */ |
||
33 | protected $connection; |
||
34 | |||
35 | /** |
||
36 | * The collection class name that wrap query result |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $wrapper; |
||
41 | |||
42 | /** |
||
43 | * The listeners processor. |
||
44 | * |
||
45 | * @var array{ |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
46 | * post: callable(\Bdf\Prime\Connection\Result\ResultSetInterface<array<string, mixed>>):array|null, |
||
47 | * each: callable(array<string, mixed>):mixed|null |
||
48 | * } |
||
49 | */ |
||
50 | protected $listeners = [ |
||
51 | 'post' => null, |
||
52 | 'each' => null, |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * The SQL compiler |
||
57 | * |
||
58 | * @var object |
||
59 | */ |
||
60 | protected $compiler; |
||
61 | |||
62 | /** |
||
63 | * @var CollectionFactory |
||
64 | */ |
||
65 | private $collectionFactory; |
||
66 | |||
67 | /** |
||
68 | * @var object |
||
69 | */ |
||
70 | protected $extension; |
||
71 | |||
72 | |||
73 | /** |
||
74 | * AbstractReadCommand constructor. |
||
75 | * |
||
76 | * @param C $connection |
||
77 | * @param PreprocessorInterface $preprocessor |
||
78 | */ |
||
79 | 1042 | public function __construct(ConnectionInterface $connection, PreprocessorInterface $preprocessor) |
|
80 | { |
||
81 | 1042 | parent::__construct($preprocessor, new CompilerState()); |
|
82 | |||
83 | 1042 | $this->on($connection); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 967 | public function compiler(): object |
|
90 | { |
||
91 | 967 | return $this->compiler; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 9 | public function collectionFactory(): CollectionFactory |
|
98 | { |
||
99 | 9 | if ($this->collectionFactory === null) { |
|
100 | 1 | $this->collectionFactory = CollectionFactory::forDbal(); |
|
101 | } |
||
102 | |||
103 | 9 | return $this->collectionFactory; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 668 | public function setCollectionFactory(CollectionFactory $collectionFactory) |
|
110 | { |
||
111 | 668 | $this->collectionFactory = $collectionFactory; |
|
112 | |||
113 | 668 | return $this; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | 15 | public function connection(): ConnectionInterface |
|
120 | { |
||
121 | 15 | return $this->connection; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | 1041 | public function on(ConnectionInterface $connection) |
|
128 | { |
||
129 | 1041 | $this->connection = $connection; |
|
0 ignored issues
–
show
It seems like
$connection of type Bdf\Prime\Connection\ConnectionInterface is incompatible with the declared type Bdf\Prime\Query\C of property $connection .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. ![]() |
|||
130 | /** @psalm-suppress InvalidArgument */ |
||
131 | 1041 | $this->compiler = $connection->factory()->compiler(get_class($this)); |
|
132 | |||
133 | 1041 | return $this; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | 671 | public function post(callable $processor, bool $forEach = true) |
|
140 | { |
||
141 | 671 | $this->listeners[$forEach ? 'each' : 'post'] = $processor; |
|
142 | |||
143 | 671 | return $this; |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 9 | public function wrapAs(string $wrapperClass) |
|
150 | { |
||
151 | 9 | $this->wrapper = $wrapperClass; |
|
152 | |||
153 | 9 | return $this; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | * |
||
159 | * @return array|CollectionInterface |
||
160 | */ |
||
161 | 525 | public function postProcessResult(ResultSetInterface $data): iterable |
|
162 | { |
||
163 | 525 | if ($this->listeners['post'] !== null) { |
|
164 | 465 | $proceed = $this->listeners['post']($data); |
|
165 | 62 | } elseif (($listener = $this->listeners['each']) !== null) { |
|
166 | 1 | $proceed = []; |
|
167 | |||
168 | 1 | foreach ($data as $row) { |
|
169 | 1 | $proceed[] = $listener($row); |
|
170 | } |
||
171 | } else { |
||
172 | 61 | $proceed = $data->all(); |
|
173 | } |
||
174 | |||
175 | 522 | if ($this->wrapper !== null) { |
|
176 | 9 | return $this->collectionFactory()->wrap($proceed, $this->wrapper); |
|
177 | } |
||
178 | |||
179 | 513 | return $proceed; |
|
180 | } |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | 711 | public function setExtension($extension): void |
|
186 | { |
||
187 | 711 | $this->extension = $extension; |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * Extension call |
||
192 | * run the query extension, set by the builder |
||
193 | * |
||
194 | * @param string $name |
||
195 | * @param array $arguments |
||
196 | * |
||
197 | * @return mixed |
||
198 | */ |
||
199 | 354 | public function __call($name, $arguments) |
|
200 | { |
||
201 | 354 | return $this->extension->$name($this, ...$arguments); |
|
202 | } |
||
203 | } |
||
204 |