Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Filter
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
7 / 7
15
100.00% covered (success)
100.00%
1 / 1
 getFilter
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 fromString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __callStatic
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 registerNamespace
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 resetNamespaces
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Verja;
4
5use Verja\Exception\FilterNotFound;
6
7/**
8 * Class Filter
9 *
10 * @package Verja
11 * @author  Thomas Flori <thflori@gmail.com>
12 *
13 * @method static Filter\Boolean boolean(array $stringTrue = [], array $stringFalse = [], $overwrite = false)
14 * @method static Filter\Callback callback($filter)
15 * @method static Filter\ConvertCase convertCase($mode)
16 * @method static Filter\Escape escape(bool $doubleEncode, bool $specialChars)
17 * @method static Filter\Integer integer()
18 * @method static Filter\Numeric numeric(string $decimalPoint = '.')
19 * @method static Filter\PregReplace pregReplace($pattern, $replace)
20 * @method static Filter\Replace replace($search, $replace)
21 * @method static Filter\Trim trim(string $characterMask = " \t\n\r\0\x0B")
22 */
23abstract class Filter implements FilterInterface
24{
25    use WithAssignedField;
26
27    /** @var string[] */
28    protected static $namespaces = [ '\\Verja\\Filter' ];
29
30    /** @var ValidatorInterface */
31    protected $validatedBy;
32
33    /**
34     * Get a filter instance
35     *
36     * @param string|callable|FilterInterface $filter
37     * @return Filter\Callback|FilterInterface
38     * @throws FilterNotFound
39     */
40    public static function getFilter($filter)
41    {
42        if (is_string($filter)) {
43            $filter = Filter::fromString($filter);
44        } elseif (is_callable($filter) && !$filter instanceof FilterInterface) {
45            $filter = new Filter\Callback($filter);
46        }
47
48        if (!$filter instanceof FilterInterface) {
49            throw new \InvalidArgumentException('$filter has to be an instance of FilterInterface');
50        }
51
52        return $filter;
53    }
54
55    /**
56     * Create a Filter from $str
57     *
58     * This method uses Parser::parseClassNameWIthParameters. This method has some limitations for parameters - look
59     * at it's description to learn more.
60     *
61     * @param string $definition
62     * @return FilterInterface
63     * @throws FilterNotFound
64     * @see Parser::parseClassNameWithParameters() to learn how to pass parameters
65     */
66    public static function fromString(string $definition): FilterInterface
67    {
68        return static::create(...Parser::parseClassNameWithParameters($definition));
69    }
70
71    /**
72     * Create a filter dynamically
73     *
74     * @param string $name
75     * @param array  $arguments
76     * @return FilterInterface
77     * @throws FilterNotFound
78     */
79    public static function __callStatic($name, array $arguments)
80    {
81        return static::create(ucfirst($name), $arguments);
82    }
83
84    /**
85     * Call the filter is an alias for filter
86     *
87     * @param mixed $value
88     * @param array $context
89     * @return mixed
90     */
91    public function __invoke($value, array $context = [])
92    {
93        return $this->filter($value, $context);
94    }
95
96
97    /**
98     * Create a filter dynamically
99     *
100     * @param string $shortName
101     * @param array  $parameters
102     * @return FilterInterface
103     * @throws FilterNotFound
104     */
105    public static function create(string $shortName, array $parameters = []): FilterInterface
106    {
107        foreach (self::$namespaces as $namespace) {
108            $class = $namespace . '\\' . $shortName;
109            if (!class_exists($class)) {
110                continue;
111            }
112            $filter = new $class(...$parameters);
113            if (!$filter instanceof FilterInterface) {
114                continue;
115            }
116            return $filter;
117        }
118
119        throw new FilterNotFound($shortName);
120    }
121
122    /**
123     * Register an additional namespace
124     *
125     * @param string $namespace
126     */
127    public static function registerNamespace(string $namespace)
128    {
129        array_unshift(self::$namespaces, $namespace);
130
131        // Untestable - required to reduce performance impact
132        // @codeCoverageIgnoreStart
133        if (count(self::$namespaces) > 2) {
134            self::$namespaces = array_unique(self::$namespaces);
135        }
136        // @codeCoverageIgnoreEnd
137    }
138
139    /**
140     * Reset namespaces to defaults
141     */
142    public static function resetNamespaces()
143    {
144        self::$namespaces = [ '\\Verja\\Filter' ];
145    }
146}