Passed
Push — develop ( 067c9c...bf1d25 )
by nguereza
04:45
created

FilterCollection::addFilter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 16
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Template
5
 *
6
 * Platine Template is a template engine that has taken a lot of inspiration from Django.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Template
11
 * Copyright (c) 2014 Guz Alexander, http://guzalexander.com
12
 * Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com
13
 * Copyright (c) 2006 Mateo Murphy
14
 *
15
 * Permission is hereby granted, free of charge, to any person obtaining a copy
16
 * of this software and associated documentation files (the "Software"), to deal
17
 * in the Software without restriction, including without limitation the rights
18
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
 * copies of the Software, and to permit persons to whom the Software is
20
 * furnished to do so, subject to the following conditions:
21
 *
22
 * The above copyright notice and this permission notice shall be included in all
23
 * copies or substantial portions of the Software.
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
 * SOFTWARE.
32
 */
33
34
/**
35
 *  @file FilterCollection.php
36
 *
37
 *  The Template Filter collection class
38
 *
39
 *  @package    Platine\Template\Parser
40
 *  @author Platine Developers Team
41
 *  @copyright  Copyright (c) 2020
42
 *  @license    http://opensource.org/licenses/MIT  MIT License
43
 *  @link   http://www.iacademy.cf
44
 *  @version 1.0.0
45
 *  @filesource
46
 */
47
48
declare(strict_types=1);
49
50
namespace Platine\Template\Parser;
51
52
use Platine\Template\Filter\ArrayFilter;
53
use Platine\Template\Filter\DatetimeFilter;
54
use Platine\Template\Filter\HtmlFilter;
55
use Platine\Template\Filter\NumberFilter;
56
use Platine\Template\Filter\StringFilter;
57
use Platine\Template\Util\Helper;
58
use ReflectionClass;
59
use ReflectionMethod;
60
61
/**
62
 * Class FilterCollection
63
 * @package Platine\Template\Parser
64
 */
65
class FilterCollection
66
{
67
68
    /**
69
     * The list of filter with their name and class
70
     * @var array<string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
71
     */
72
    protected array $filters = [];
73
74
    /**
75
     * Create new instance
76
     */
77
    public function __construct()
78
    {
79
        $this->addFilter(ArrayFilter::class);
80
        $this->addFilter(DatetimeFilter::class);
81
        $this->addFilter(HtmlFilter::class);
82
        $this->addFilter(NumberFilter::class);
83
        $this->addFilter(StringFilter::class);
84
    }
85
86
    /**
87
     * Add filter
88
     * @param class-string $filter a class the filters will be
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
89
     *  called statically
90
     *
91
     * @return bool
92
     */
93
    public function addFilter(string $filter): bool
94
    {
95
        // register all its static methods
96
        $reflection = new ReflectionClass($filter);
97
        $methods = $reflection->getMethods(ReflectionMethod::IS_STATIC);
98
        if (!empty($methods)) {
99
            foreach ($methods as $method) {
100
                /** @var class-string $className */
101
                $className = $method->class;
102
                $this->filters[$method->name] = $className;
103
            }
104
105
            return true;
106
        }
107
108
        return false;
109
    }
110
111
    /**
112
     * Invokes the filter with the given name
113
     * @param string $name
114
     * @param mixed $value
115
     * @param array<int, mixed> $args
116
     * @return mixed
117
     */
118
    public function invoke(string $name, $value, array $args = [])
119
    {
120
        // workaround for a single standard filter being a reserved
121
        // keyword - we can't use overloading for static calls
122
        if ($name === 'default') {
123
            $name = 'defaultValue';
124
        }
125
126
        //convert underscore value to camelcase
127
        // like sort_key => sortKey
128
        $methodName = Helper::dashesToCamelCase($name);
129
130
        array_unshift($args, $value);
131
132
        // Consult the mapping
133
        if (!isset($this->filters[$methodName])) {
134
            return $value;
135
        }
136
137
        $className = $this->filters[$methodName];
138
139
        $callback = $className . '::' . $methodName;
140
        if (is_callable($callback)) {
141
            // Call a class method statically
142
            return call_user_func_array($callback, $args);
143
        }
144
145
        return $value;
146
    }
147
}
148