Completed
Push — master ( 3cd4b3...0ed6c3 )
by Jonathan
02:13
created

Combiners::prefixed()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 1
nop 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Combiners.php$1 ➔ __construct() 0 5 1
A Combiners.php$1 ➔ __invoke() 0 17 4
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;
0 ignored issues
show
Bug introduced by
The property destination does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
                $this->prefix = $prefix ?? $destination;
0 ignored issues
show
Bug introduced by
The property prefix does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mine of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The property destination does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
84
                $this->prefix = $prefix ?? $destination;
0 ignored issues
show
Bug introduced by
The property prefix does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mine of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The property destination does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
120
                $this->dfield = $date;
0 ignored issues
show
Bug introduced by
The property dfield does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
121
                $this->tfield = $time;
0 ignored issues
show
Bug introduced by
The property tfield does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
122
                $this->zfield = $timezone;
0 ignored issues
show
Bug introduced by
The property zfield does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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