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 LibreWorks contributors |
19
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License |
20
|
|
|
*/ |
21
|
|
|
namespace Caridea\Filter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Come Together Right Now. |
25
|
|
|
* |
26
|
|
|
* This class provides some Reducers. |
27
|
|
|
*/ |
28
|
|
|
class Combiners |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* A combiner that takes any field with a given prefix and adds it to a List |
32
|
|
|
* |
33
|
|
|
* For example, `value1`, `value2`, and `value3` can be combined into an |
34
|
|
|
* outgoing field `value` with an array that contains 3 entries. |
35
|
|
|
* |
36
|
|
|
* @param string $destination The outgoing field name |
37
|
|
|
* @param string $prefix The prefix to find, uses the destination by default |
38
|
|
|
* @return \Caridea\Filter\Reducer The created filter |
39
|
|
|
*/ |
40
|
|
|
public static function appender(string $destination, string $prefix = null): Reducer |
41
|
|
|
{ |
42
|
|
|
return new class($destination, $prefix) implements Reducer { |
43
|
|
|
public function __construct($destination, $prefix = null) |
44
|
|
|
{ |
45
|
|
|
$this->destination = $destination; |
|
|
|
|
46
|
|
|
$this->prefix = $prefix ?? $destination; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
public function __invoke(array $input): array |
49
|
|
|
{ |
50
|
|
|
$subl = strlen($this->prefix); |
51
|
|
|
$out = []; |
52
|
|
|
$mine = []; |
53
|
|
|
foreach ($input as $k => $v) { |
54
|
|
|
if (substr($k, 0, $subl) === $this->prefix) { |
55
|
|
|
$mine[] = $v; |
56
|
|
|
} else { |
57
|
|
|
$out[$k] = $v; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
if ($mine) { |
|
|
|
|
61
|
|
|
$out[$this->destination] = $mine; |
62
|
|
|
} |
63
|
|
|
return $out; |
64
|
|
|
} |
65
|
|
|
}; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* A combiner that combines prefixed fields into a Map. |
70
|
|
|
* |
71
|
|
|
* For example, `address-street` and `address-city` can be combined into an |
72
|
|
|
* outgoing field `address` with `street` and `city` keys. |
73
|
|
|
* |
74
|
|
|
* @param string $destination The outgoing field name |
75
|
|
|
* @param string $prefix The prefix to find, uses the destination by default |
76
|
|
|
* @return \Caridea\Filter\Reducer The created filter |
77
|
|
|
*/ |
78
|
|
|
public static function prefixed(string $destination, string $prefix = null): Reducer |
79
|
|
|
{ |
80
|
|
|
return new class($destination, $prefix) implements Reducer { |
81
|
|
|
public function __construct($destination, $prefix) |
82
|
|
|
{ |
83
|
|
|
$this->destination = $destination; |
|
|
|
|
84
|
|
|
$this->prefix = $prefix ?? $destination; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
public function __invoke(array $input): array |
87
|
|
|
{ |
88
|
|
|
$subl = strlen($this->prefix); |
89
|
|
|
$out = []; |
90
|
|
|
$mine = []; |
91
|
|
|
foreach ($input as $k => $v) { |
92
|
|
|
if (substr($k, 0, $subl) === $this->prefix) { |
93
|
|
|
$mine[substr($k, $subl)] = $v; |
94
|
|
|
} else { |
95
|
|
|
$out[$k] = $v; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
if ($mine) { |
|
|
|
|
99
|
|
|
$out[$this->destination] = $mine; |
100
|
|
|
} |
101
|
|
|
return $out; |
102
|
|
|
} |
103
|
|
|
}; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Creates a combiner that combines datetime values. |
108
|
|
|
* |
109
|
|
|
* @param string $date The field to find date (e.g. `2016-09-15`) |
110
|
|
|
* @param string $time The field to find time (e.g. `T12:04:06`) |
111
|
|
|
* @param string $timezone The field to find timezone name (e.g. `America/New_York`) |
112
|
|
|
* @return \Caridea\Filter\Reducer The created filter |
113
|
|
|
*/ |
114
|
|
|
public static function datetime(string $destination, string $date, string $time, string $timezone = null): Reducer |
115
|
|
|
{ |
116
|
|
|
return new class($destination, $date, $time, $timezone) implements Reducer { |
117
|
|
|
public function __construct($destination, $date, $time, $timezone = null) |
118
|
|
|
{ |
119
|
|
|
$this->destination = $destination; |
|
|
|
|
120
|
|
|
$this->dfield = $date; |
|
|
|
|
121
|
|
|
$this->tfield = $time; |
|
|
|
|
122
|
|
|
$this->zfield = $timezone; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
public function __invoke(array $input): array |
125
|
|
|
{ |
126
|
|
|
$date = $input[$this->dfield] ?? ''; |
127
|
|
|
$time = $input[$this->tfield] ?? ''; |
128
|
|
|
$zone = $this->zfield === null ? null : ($input[$this->zfield] ?? null); |
129
|
|
|
$out = array_filter($input, function ($k) { |
130
|
|
|
return $k !== $this->dfield && $k !== $this->tfield && $k !== $this->zfield; |
131
|
|
|
}, ARRAY_FILTER_USE_KEY); |
132
|
|
|
if ($date || $time) { |
133
|
|
|
$out[$this->destination] = new \DateTime( |
134
|
|
|
"{$date}{$time}", |
135
|
|
|
!$this->zfield ? null : new \DateTimeZone($zone) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
return $out; |
139
|
|
|
} |
140
|
|
|
}; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: