StaticFilter::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of slick/filter package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Filter;
11
12
use Slick\Filter\Exception\InvalidArgumentException;
13
14
/**
15
 * Static Filter: Filter factory utility
16
 *
17
 * @package Slick\Filter
18
 * @author  Filipe Silva <[email protected]>
19
 */
20
final class StaticFilter
21
{
22
23
    /**
24
     * @var array List of available filter
25
     */
26
    public static $filters = [
27
        'text'         => 'Slick\Filter\Text',
28
        'htmlEntities' => 'Slick\Filter\HtmlEntities',
29
        'number'       => 'Slick\Filter\Number',
30
        'url'          => 'Slick\Filter\Url'
31
    ];
32
33
    /**
34
     * Creates the filter in the alias or class name and applies it to the
35
     * provided value.
36
     *
37
     * @param string $alias Filter alias or FQ class name
38
     * @param mixed  $value Value to filter
39
     *
40
     * @throws InvalidArgumentException If the class does not exists or if
41
     *      class does not implements the FilterInterface.
42
     *
43
     * @return mixed
44
     */
45 2
    public static function filter($alias, $value)
46
    {
47 2
        $filter = self::create($alias);
48 2
        return $filter->filter($value);
49
    }
50
51
    /**
52
     * Creates the filter object for provided alias or FQ class name
53
     *
54
     * @param string $filter Filter alias od FQ class name
55
     *
56
     * @throws InvalidArgumentException If the class does not exists or if
57
     *      class does not implements the FilterInterface.
58
     *
59
     * @return FilterInterface
60
     */
61 8
    public static function create($filter)
62
    {
63 8
        $class = self::getClass($filter);
64 6
        self::checkFilter($class);
65 4
        return new $class();
66
    }
67
68
    /**
69
     * Returns the class name for provided alias
70
     *
71
     * @param string $alias The FQ class name or one of known filter alias
72
     * @return string
73
     *
74
     * @throws InvalidArgumentException If the class does not exists.
75
     */
76 8
    protected static function getClass($alias)
77
    {
78 8
        if (array_key_exists($alias, self::$filters)) {
79 4
            return self::$filters[$alias];
80
        }
81
82 4
        if (!class_exists($alias)) {
83 2
            throw new InvalidArgumentException(
84 2
                "Class {$alias} does not exists."
85 2
            );
86
        }
87 2
        return $alias;
88
    }
89
90
    /**
91
     * Verifies is provided class implements the FilterInterface interface
92
     *
93
     * @param string $class
94
     *
95
     * @throws InvalidArgumentException
96
     *      If the class does not implements the interface.
97
     */
98 6
    protected static function checkFilter($class)
99
    {
100 6
        if (!is_subclass_of($class, 'Slick\Filter\FilterInterface')) {
101 2
            throw new InvalidArgumentException(
102 2
                "Class {$class} does not implements the ".
103
                "Slick\\Filter\\FilterInterface interface."
104 2
            );
105
        }
106 4
    }
107
}
108