|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Caridea |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
|
8
|
|
|
* the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15
|
|
|
* License for the specific language governing permissions and limitations under |
|
16
|
|
|
* the License. |
|
17
|
|
|
* |
|
18
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
|
19
|
|
|
* @license Apache-2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Caridea\Filter; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* We Built this Filter on Rock and Roll. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Builder |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var \Caridea\Filter\Registry - The filter registry |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $registry; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array<string,\Caridea\Filter\Chain> - The chains by field name |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $chains = []; |
|
36
|
|
|
/** |
|
37
|
|
|
* |
|
38
|
|
|
* @var \Caridea\Filter\Reducer[] |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $reducers = []; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var \Caridea\Filter\Chain|null |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $otherwiseChain; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Creates a new Builder. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Caridea\Filter\Registry $registry The filter registry |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function __construct(Registry $registry) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$this->registry = $registry; |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Creates a new Chain for a field |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $name The field name |
|
60
|
|
|
* @return \Caridea\Filter\Chain the chain |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function field(string $name): Chain |
|
63
|
|
|
{ |
|
64
|
1 |
|
$f = new Chain($this->registry); |
|
65
|
1 |
|
$this->chains[$name] = $f; |
|
66
|
1 |
|
return $f; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Creates a new *required* Chain for a field. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $name The field name |
|
73
|
|
|
* @return \Caridea\Filter\Chain the chain |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function always(string $name): Chain |
|
76
|
|
|
{ |
|
77
|
1 |
|
$f = new Chain($this->registry, true); |
|
78
|
1 |
|
$this->chains[$name] = $f; |
|
79
|
1 |
|
return $f; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Adds a multiple filter to the builder. |
|
84
|
|
|
* |
|
85
|
|
|
* @param \Caridea\Filter\Reducer $multi |
|
86
|
|
|
* @return self provides a fluent interface |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function reducer(Reducer $multi): self |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->reducers[] = $multi; |
|
91
|
1 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Adds a chain that runs for any non-mentioned fields. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $name The filter name |
|
98
|
|
|
* @param mixed $args The filter arguments |
|
99
|
|
|
* @return \Caridea\Filter\Chain the chain |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function otherwise(string $name, ...$args): Chain |
|
102
|
|
|
{ |
|
103
|
1 |
|
$chain = new Chain($this->registry, false); |
|
104
|
1 |
|
$this->otherwiseChain = $chain; |
|
105
|
1 |
|
return $chain->then($name, ...$args); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Builds a new Filter. |
|
110
|
|
|
* |
|
111
|
|
|
* @return \Caridea\Filter\Filter the created Filter. |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function build(): Filter |
|
114
|
|
|
{ |
|
115
|
1 |
|
return new Filter($this->chains, $this->reducers, $this->otherwiseChain); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|