Completed
Push — master ( 9c6776...f3b91b )
by Jonathan
02:32
created

Registry::builder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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
 * A filter registry.
25
 */
26
class Registry
27
{
28
    /**
29
     * @var array<string,callable> Associative array of definition name to function callback
30
     */
31
    protected $definitions = [];
32
33
    /**
34
     * @var array<string,callable> Associative array of definition name to function callback
35
     */
36
    private static $defaultDefinitions = [
37
        'trim' => ['Caridea\Filter\Strings', 'trim'],
38
        'lowercase' => ['Caridea\Filter\Strings', 'lowerCase'],
39
        'uppercase' => ['Caridea\Filter\Strings', 'upperCase'],
40
        'titlecase' => ['Caridea\Filter\Strings', 'titleCase'],
41
        'string' => ['Caridea\Filter\Strings', 'toString'],
42
        'replace' => ['Caridea\Filter\Strings', 'replace'],
43
        'regex' => ['Caridea\Filter\Strings', 'regex'],
44
        'alnum' => ['Caridea\Filter\Strings', 'alnum'],
45
        'alpha' => ['Caridea\Filter\Strings', 'alpha'],
46
        'numeric' => ['Caridea\Filter\Strings', 'numeric'],
47
        'nl' => ['Caridea\Filter\Strings', 'unixNewlines'],
48
        'compactnl' => ['Caridea\Filter\Strings', 'compactNewlines'],
49
        'bool' => ['Caridea\Filter\Casts', 'toBoolean'],
50
        'int' => ['Caridea\Filter\Casts', 'toInteger'],
51
        'float' => ['Caridea\Filter\Casts', 'toFloat'],
52
        'array' => ['Caridea\Filter\Casts', 'toArray'],
53
        'default' => ['Caridea\Filter\Casts', 'toDefault'],
54
    ];
55
56
    /**
57
     * Creates a new filter Builder.
58
     */
59 4
    public function __construct()
60
    {
61 4
        $this->definitions = array_merge([], self::$defaultDefinitions);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge(array(), self::$defaultDefinitions) of type array is incompatible with the declared type array<string,callable> of property $definitions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62 4
    }
63
64
    /**
65
     * Registers rule definitions.
66
     *
67
     * ```php
68
     * $builder = new \Caridea\Filter\Builder();
69
     * $builder->register([
70
     *     'gzip' => ['My\Gzip', 'compressor'],
71
     *     'password' => function($hash){return new Hasher($hash);},
72
     *     'something' => 'my_function_that_returns_a_closure'
73
     * ]);
74
     * ```
75
     *
76
     * @param array<string,callable> $definitions Associative array of definition name to function callback
77
     * @return $this provides a fluent interface
78
     */
79 2
    public function register(array $definitions): self
80
    {
81 2
        foreach ($definitions as $name => $callback) {
82 2
            if (!is_callable($callback)) {
83 1
                throw new \InvalidArgumentException('Values passed to register must be callable');
84
            }
85 1
            $this->definitions[$name] = $callback;
86
        }
87 1
        return $this;
88
    }
89
90
    /**
91
     * Constructs a filter.
92
     *
93
     * @param string $name The name of the filter
94
     * @param array $args Any filter arguments
95
     * @return callable The filter
96
     * @throws \InvalidArgumentException if the filter name is not registered
97
     * @throws \UnexpectedValueException if the factory returns a non-callable value
98
     */
99 3
    public function factory(string $name, array $args): callable
100
    {
101 3
        if (!array_key_exists($name, $this->definitions)) {
102 1
            throw new \InvalidArgumentException("No filter registered with name: $name");
103
        }
104 2
        $factory = $this->definitions[$name];
105 2
        $filter = $factory(...$args);
106 2
        if (!is_callable($filter)) {
107 1
            throw new \UnexpectedValueException('Definitions must return callable');
108
        }
109 1
        return $filter;
110
    }
111
112
    /**
113
     * Creates a new Builder using this Repository.
114
     *
115
     * @return \Caridea\Filter\Builder The builder
116
     */
117 1
    public function builder(): Builder
118
    {
119 1
        return new Builder($this);
120
    }
121
}
122