|
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
|
|
|
* 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
|
2 |
|
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
|
2 |
|
$this->destination = $destination; |
|
|
|
|
|
|
46
|
2 |
|
$this->prefix = $prefix ?? $destination; |
|
|
|
|
|
|
47
|
2 |
|
} |
|
48
|
|
|
public function __invoke(array $input): array |
|
49
|
|
|
{ |
|
50
|
2 |
|
$subl = strlen($this->prefix); |
|
51
|
2 |
|
$out = []; |
|
52
|
2 |
|
$mine = []; |
|
53
|
2 |
|
foreach ($input as $k => $v) { |
|
54
|
2 |
|
if (substr($k, 0, $subl) === $this->prefix) { |
|
55
|
1 |
|
$mine[] = $v; |
|
56
|
|
|
} else { |
|
57
|
2 |
|
$out[$k] = $v; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
2 |
|
if ($mine) { |
|
|
|
|
|
|
61
|
1 |
|
$out[$this->destination] = $mine; |
|
62
|
|
|
} |
|
63
|
2 |
|
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
|
2 |
|
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
|
2 |
|
$this->destination = $destination; |
|
|
|
|
|
|
84
|
2 |
|
$this->prefix = $prefix ?? $destination; |
|
|
|
|
|
|
85
|
2 |
|
} |
|
86
|
|
|
public function __invoke(array $input): array |
|
87
|
|
|
{ |
|
88
|
2 |
|
$subl = strlen($this->prefix); |
|
89
|
2 |
|
$out = []; |
|
90
|
2 |
|
$mine = []; |
|
91
|
2 |
|
foreach ($input as $k => $v) { |
|
92
|
2 |
|
if (substr($k, 0, $subl) === $this->prefix) { |
|
93
|
1 |
|
$mine[substr($k, $subl)] = $v; |
|
94
|
|
|
} else { |
|
95
|
2 |
|
$out[$k] = $v; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
2 |
|
if ($mine) { |
|
|
|
|
|
|
99
|
1 |
|
$out[$this->destination] = $mine; |
|
100
|
|
|
} |
|
101
|
2 |
|
return $out; |
|
102
|
|
|
} |
|
103
|
|
|
}; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Creates a combiner that combines datetime values. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $destination The outgoing field name |
|
110
|
|
|
* @param string $date The field to find date (e.g. `2016-09-15`) |
|
111
|
|
|
* @param string $time The field to find time (e.g. `T12:04:06`) |
|
112
|
|
|
* @param string $timezone The field to find timezone name (e.g. `America/New_York`) |
|
113
|
|
|
* @return \Caridea\Filter\Reducer The created filter |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function datetime(string $destination, string $date, string $time, string $timezone = null): Reducer |
|
116
|
|
|
{ |
|
117
|
|
|
return new class($destination, $date, $time, $timezone) implements Reducer { |
|
118
|
3 |
|
public function __construct($destination, $date, $time, $timezone = null) |
|
119
|
|
|
{ |
|
120
|
3 |
|
$this->destination = $destination; |
|
|
|
|
|
|
121
|
3 |
|
$this->dfield = $date; |
|
|
|
|
|
|
122
|
3 |
|
$this->tfield = $time; |
|
|
|
|
|
|
123
|
3 |
|
$this->zfield = $timezone; |
|
|
|
|
|
|
124
|
3 |
|
} |
|
125
|
3 |
|
public function __invoke(array $input): array |
|
126
|
|
|
{ |
|
127
|
3 |
|
$date = $input[$this->dfield] ?? ''; |
|
128
|
3 |
|
$time = $input[$this->tfield] ?? ''; |
|
129
|
3 |
|
$zone = $this->zfield === null ? null : ($input[$this->zfield] ?? null); |
|
130
|
3 |
|
$out = array_filter($input, function ($k) { |
|
131
|
3 |
|
return $k !== $this->dfield && $k !== $this->tfield && $k !== $this->zfield; |
|
132
|
3 |
|
}, ARRAY_FILTER_USE_KEY); |
|
133
|
3 |
|
if ($date || $time) { |
|
134
|
2 |
|
$out[$this->destination] = new \DateTime( |
|
135
|
2 |
|
"{$date}{$time}", |
|
136
|
2 |
|
!$this->zfield ? null : new \DateTimeZone($zone) |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
3 |
|
return $out; |
|
140
|
|
|
} |
|
141
|
|
|
}; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
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: