Completed
Push — master ( 62c01b...6e9edd )
by Kevin
02:26
created

Configuration::isAllowedElement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Groundskeeper;
4
5
use Groundskeeper\Tokens\Token;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
class Configuration
9
{
10
    const CLEAN_STRATEGY_NONE       = 'none';
11
    const CLEAN_STRATEGY_STANDARD   = 'standard';
12
    const CLEAN_STRATEGY_AGGRESSIVE = 'aggressive';
13
14
    const ELEMENT_BLACKLIST_NONE = 'none';
15
16
    const ERROR_STRATEGY_NONE   = 'none';
17
    const ERROR_STRATEGY_THROW  = 'throw';
18
    const ERROR_STRATEGY_FIX    = 'fix';
19
    const ERROR_STRATEGY_REMOVE = 'remove';
20
21
    const OUTPUT_COMPACT = 'compact';
22
    const OUTPUT_PRETTY  = 'pretty';
23
24
    const TYPE_BLACKLIST_NONE = 'none';
25
26
    /** @var array */
27
    private $options;
28
29
    /**
30
     * Constructor
31
     */
32 66
    public function __construct(array $options = array())
33
    {
34 66
        $resolver = new OptionsResolver();
35 66
        $this->configureOptions($resolver);
36
37 66
        $this->options = $resolver->resolve($options);
38 54
        $this->setDependentOptions();
39 54
    }
40
41 41
    public function has($key)
42
    {
43 41
        return array_key_exists($key, $this->options);
44
    }
45
46 41
    public function get($key)
47
    {
48 41
        if (!$this->has($key)) {
49 1
            throw new \InvalidArgumentException('Invalid configuration key: ' . $key);
50
        }
51
52 41
        return $this->options[$key];
53
    }
54
55 5
    public function isAllowedElement($element)
56
    {
57 5
        $disallowedElementArray = explode(',', $this->options['element-blacklist']);
58 5
        foreach ($disallowedElementArray as $disallowedElement) {
59 5
            if (strtolower(trim($disallowedElement)) == $element) {
60 5
                return false;
61
            }
62 5
        }
63
64 4
        return true;
65
    }
66
67 15
    public function isAllowedType($type)
68
    {
69 15
        $disallowedTypeArray = explode(',', $this->options['type-blacklist']);
70 15
        foreach ($disallowedTypeArray as $disallowedType) {
71 15
            if (strtolower(trim($disallowedType)) == $type) {
72 10
                return false;
73
            }
74 10
        }
75
76 10
        return true;
77
    }
78
79 66
    protected function configureOptions(OptionsResolver $resolver)
80
    {
81
        // Set default options.
82 66
        $resolver->setDefaults(array(
83 66
            'clean-strategy' => self::CLEAN_STRATEGY_STANDARD,
84 66
            'element-blacklist' => self::ELEMENT_BLACKLIST_NONE,
85 66
            'error-strategy' => self::ERROR_STRATEGY_FIX,
86 66
            'indent-spaces' => 4,
87 66
            'output' => self::OUTPUT_COMPACT,
88 66
            'type-blacklist' => Token::CDATA . ',' . Token::COMMENT
89 66
        ));
90
91
        // Validation
92
93
        // clean-strategy
94 66
        $resolver->setAllowedTypes('clean-strategy', 'string');
95 66
        $resolver->setAllowedValues(
96 66
            'clean-strategy',
97
            array(
98 66
                self::CLEAN_STRATEGY_NONE,
99 66
                self::CLEAN_STRATEGY_STANDARD,
100
                self::CLEAN_STRATEGY_AGGRESSIVE
101 66
            )
102 66
        );
103
104
        // element-blacklist
105 66
        $resolver->setAllowedTypes('element-blacklist', 'string');
106
        $resolver->setAllowedValues('element-blacklist', function ($value) {
107 63
                return strlen($value) > 0;
108
            }
109 66
        );
110
111
        // error-strategy
112 66
        $resolver->setAllowedTypes('error-strategy', 'string');
113 66
        $resolver->setAllowedValues(
114 66
            'error-strategy',
115
            array(
116 66
                self::ERROR_STRATEGY_NONE,
117 66
                self::ERROR_STRATEGY_THROW,
118 66
                self::ERROR_STRATEGY_FIX,
119
                self::ERROR_STRATEGY_REMOVE
120 66
            )
121 66
        );
122
123
        // indent-spaces
124 66
        $resolver->setAllowedTypes('indent-spaces', 'int');
125
        $resolver->setAllowedValues('indent-spaces', function ($value) {
126 59
                return $value >= 0;
127
            }
128 66
        );
129
130
        // output
131 66
        $resolver->setAllowedTypes('output', 'string');
132 66
        $resolver->setAllowedValues(
133 66
            'output',
134 66
            array(self::OUTPUT_COMPACT, self::OUTPUT_PRETTY)
135 66
        );
136
137
        // type-blacklist
138 66
        $resolver->setAllowedTypes('type-blacklist', 'string');
139 66
        $resolver->setAllowedValues(
140 66
            'type-blacklist',
141 55
            function ($value) {
142 55
                if ($value == self::TYPE_BLACKLIST_NONE) {
143 19
                    return true;
144
                }
145
146
                $acceptedValues = array(
147 36
                    Token::CDATA,
148 36
                    Token::COMMENT,
149 36
                    Token::DOCTYPE,
150 36
                    Token::ELEMENT,
151
                    Token::TEXT
152 36
                );
153 36
                $valueArray = explode(',', $value);
154 36
                foreach ($valueArray as $val) {
155 36
                    if (array_search(trim(strtolower($val)), $acceptedValues) === false) {
156 1
                        return false;
157
                    }
158 35
                }
159
160 35
                return true;
161
            }
162 66
        );
163 66
    }
164
165 54
    protected function setDependentOptions()
166
    {
167 54
        if ($this->options['output'] == self::OUTPUT_COMPACT) {
168 51
            $this->options['indent-spaces'] = 0;
169 51
        }
170 54
    }
171
}
172