Completed
Push — master ( 1df0e6...9c6776 )
by Jonathan
02:05
created

Combiners.php$2 ➔ __construct()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
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
 * Combines fields into one
25
 */
26
class Combiners
27
{
28
    /**
29
     * A combiner that takes any field with a given prefix and adds it to a List
30
     *
31
     * For example, `value1`, `value2`, and `value3` can be combined into an
32
     * outgoing field `value` with an array that contains 3 entries.
33
     *
34
     * @param string $destination The outgoing field name
35
     * @param string $prefix The prefix to find
36
     * @return Multi The created filter
37
     */
38
    public static function appender(string $destination, string $prefix): Multi
39
    {
40
        return new class($destination, $prefix) implements Multi {
41
            public function __construct($destination, $prefix)
42
            {
43
                $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...
44
                $this->prefix = $prefix;
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...
45
            }
46
            public function __invoke(array $input): array
47
            {
48
                $subl = strlen($this->prefix);
49
                $out = array_filter($input, function ($k) use ($subl) {
50
                    return substr($k, 0, $subl) === $this->prefix;
51
                }, ARRAY_FILTER_USE_KEY);
52
                return [($this->destination) => $out];
53
            }
54
        };
55
    }
56
57
    /**
58
     * A combiner that combines prefixed fields into a Map.
59
     *
60
     * For example, `address-street` and `address-city` can be combined into an
61
     * outgoing field `address` with `street` and `city` keys.
62
     *
63
     * @param string $destination The outgoing field name
64
     * @param string $prefix The prefix to find
65
     * @return Multi The created filter
66
     */
67
    public static function prefixed(string $destination, string $prefix): Multi
68
    {
69
        return new class($destination, $prefix) implements Multi {
70
            public function __construct($destination, $prefix)
71
            {
72
                $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...
73
                $this->prefix = $prefix;
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...
74
            }
75
            public function __invoke(array $input): array
76
            {
77
                $subl = strlen($this->prefix);
78
                $out = [];
79
                foreach ($input as $k => $v) {
80
                    if (substr($k, 0, $subl) === $this->prefix) {
81
                        $out[substr($k, $subl)] = $v;
82
                    }
83
                }
84
                return [($this->destination) => $out];
85
            }
86
        };
87
    }
88
89
    /**
90
     * Creates a combiner that combines datetime values.
91
     *
92
     * @param string $date The field to find date (e.g. `2016-09-15`)
93
     * @param string $time The field to find time (e.g. `12:04:06`)
94
     * @param string $timezone The field to find timezone name (e.g. `America/New_York`)
95
     * @return Multi The created filter
96
     */
97
    public static function datetime(string $destination, string $date, string $time, string $timezone = null): Multi
98
    {
99
        return new class($destination, $date, $time, $timezone) implements Multi {
100
            public function __construct($destination, $date, $time, $timezone = null)
101
            {
102
                $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...
103
                $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...
104
                $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...
105
                $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...
106
            }
107
            public function __invoke(array $input): array
108
            {
109
                $date = $input[$this->dfield] ?? '';
110
                $time = $input[$this->tfield] ?? '';
111
                $zone = $this->zfield === null ? null : ($input[$this->zfield] ?? null);
112
                return [
113
                    ($this->destination) => new \DateTime(
114
                        "{$date}T{$time}",
115
                        !$zfield ? null : new \DateTimeZone($zone)
0 ignored issues
show
Bug introduced by
The variable $zfield does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
116
                    )
117
                ];
118
            }
119
        };
120
    }
121
}
122