Completed
Push — master ( a9adfd...b66e97 )
by Kevin
02:14
created

Groundskeeper::clean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 15
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Groundskeeper;
4
5
use Groundskeeper\Tokens\Tokenizer;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
class Groundskeeper
9
{
10
    /** @var array */
11
    private $options;
12
13
    /**
14
     * Constructor
15
     */
16 16
    public function __construct(array $options = array())
17
    {
18 16
        $resolver = new OptionsResolver();
19 16
        $this->configureOptions($resolver);
20
21 16
        $this->options = $resolver->resolve($options);
22 11
    }
23
24 10
    public function clean($html)
25
    {
26
        // Tokenize
27 10
        $tokenizer = new Tokenizer($this->options);
28 10
        $tokens = $tokenizer->tokenize($html);
29
30
        // Clean
31
32
        // Output
33
        $outputClassName = 'Groundskeeper\\Output\\' .
34 10
            ucfirst($this->options['output']);
35 10
        $output = new $outputClassName();
36
37 10
        return $output->printTokens($tokens);
38
    }
39
40 1
    public function getOptions()
41
    {
42 1
        return $this->options;
43
    }
44
45 16
    protected function configureOptions(OptionsResolver $resolver)
46
    {
47
        // Set default options.
48 16
        $resolver->setDefaults(array(
49 16
            'indent-spaces' => 4,
50 16
            'output' => 'compact',
51
            'throw-on-error' => false
52 16
        ));
53
54
        // Validation
55 16
        $resolver->setAllowedValues('indent-spaces', function ($value) {
56 16
            if (!is_int($value)) {
57 1
                return false;
58
            }
59
60 15
            return $value >= 0;
61 16
        });
62 16
        $resolver->setAllowedValues('output', array('compact', 'pretty'));
63 16
        $resolver->setAllowedTypes('throw-on-error', 'bool');
64 16
    }
65
}
66