1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2021 Aleksandar Panic |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ArekX\PQL; |
19
|
|
|
|
20
|
|
|
use ArekX\PQL\Contracts\StructuredQuery; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Represents high-level structured query for |
24
|
|
|
* defining a query structure for all queries. |
25
|
|
|
*/ |
26
|
|
|
class Query implements StructuredQuery |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Defined query parts |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected array $parts = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritDoc |
37
|
|
|
*/ |
38
|
190 |
|
public static function create(): static |
39
|
|
|
{ |
40
|
190 |
|
return new static(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
159 |
|
public function toArray(): array |
47
|
|
|
{ |
48
|
159 |
|
return $this->parts; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritDoc |
53
|
|
|
*/ |
54
|
150 |
|
public function use(string $part, array|string|StructuredQuery|null $value): static |
55
|
|
|
{ |
56
|
150 |
|
$this->parts[$part] = $value; |
57
|
150 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
62 |
|
public function append(string $part, array|string|StructuredQuery $value): static |
64
|
|
|
{ |
65
|
62 |
|
if (!empty($this->parts[$part]) && !is_array($this->parts[$part])) { |
66
|
1 |
|
$this->parts[$part] = [$this->parts[$part]]; |
67
|
|
|
} |
68
|
|
|
|
69
|
62 |
|
$this->parts[$part][] = $value; |
70
|
62 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
106 |
|
public function get(string $part): array|string|StructuredQuery|null |
77
|
|
|
{ |
78
|
106 |
|
return $this->parts[$part] ?? null; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|