Registry   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 101
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A register() 0 10 3
A factory() 0 12 3
A builder() 0 4 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-2018 LibreWorks contributors
19
 * @license   Apache-2.0
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
        'cut' => ['Caridea\Filter\Strings', 'cut'],
45
        'alnum' => ['Caridea\Filter\Strings', 'alnum'],
46
        'alpha' => ['Caridea\Filter\Strings', 'alpha'],
47
        'numeric' => ['Caridea\Filter\Strings', 'numeric'],
48
        'nl' => ['Caridea\Filter\Strings', 'unixNewlines'],
49
        'compactnl' => ['Caridea\Filter\Strings', 'compactNewlines'],
50
        'bool' => ['Caridea\Filter\Casts', 'toBoolean'],
51
        'int' => ['Caridea\Filter\Casts', 'toInteger'],
52
        'float' => ['Caridea\Filter\Casts', 'toFloat'],
53
        'array' => ['Caridea\Filter\Casts', 'toArray'],
54
        'default' => ['Caridea\Filter\Casts', 'toDefault'],
55
        'split' => ['Caridea\Filter\Arrays', 'split'],
56
        'explode' => ['Caridea\Filter\Arrays', 'explode'],
57
        'join' => ['Caridea\Filter\Arrays', 'join'],
58
        'slice' => ['Caridea\Filter\Arrays', 'slice'],
59
    ];
60
61
    /**
62
     * Creates a new filter Builder.
63
     */
64 4
    public function __construct()
65
    {
66 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...
67 4
    }
68
69
    /**
70
     * Registers rule definitions.
71
     *
72
     * ```php
73
     * $registry = new \Caridea\Filter\Registry();
74
     * $registry->register([
75
     *     'gzip' => ['My\Gzip', 'compressor'],
76
     *     'password' => function($hash){return new Hasher($hash);},
77
     *     'something' => 'my_function_that_returns_a_closure'
78
     * ]);
79
     * ```
80
     *
81
     * @param array<string,callable> $definitions Associative array of definition name to function callback
82
     * @return $this provides a fluent interface
83
     */
84 2
    public function register(array $definitions): self
85
    {
86 2
        foreach ($definitions as $name => $callback) {
87 2
            if (!is_callable($callback)) {
88 1
                throw new \InvalidArgumentException('Values passed to register must be callable');
89
            }
90 1
            $this->definitions[$name] = $callback;
91
        }
92 1
        return $this;
93
    }
94
95
    /**
96
     * Constructs a filter.
97
     *
98
     * @param string $name The name of the filter
99
     * @param array $args Any filter arguments
100
     * @return callable The filter
101
     * @throws \InvalidArgumentException if the filter name is not registered
102
     * @throws \UnexpectedValueException if the factory returns a non-callable value
103
     */
104 3
    public function factory(string $name, array $args): callable
105
    {
106 3
        if (!array_key_exists($name, $this->definitions)) {
107 1
            throw new \InvalidArgumentException("No filter registered with name: $name");
108
        }
109 2
        $factory = $this->definitions[$name];
110 2
        $filter = $factory(...$args);
111 2
        if (!is_callable($filter)) {
112 1
            throw new \UnexpectedValueException('Definitions must return callable');
113
        }
114 1
        return $filter;
115
    }
116
117
    /**
118
     * Creates a new Builder using this Repository.
119
     *
120
     * @return \Caridea\Filter\Builder The builder
121
     */
122 1
    public function builder(): Builder
123
    {
124 1
        return new Builder($this);
125
    }
126
}
127