Passed
Push — main ( 77e259...371264 )
by Oscar
08:09 queued 06:34
created

ClassProcessor::__invoke()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 33
ccs 21
cts 21
cp 1
rs 9.2888
cc 5
nc 6
nop 2
crap 5
1
<?php
2
3
/*
4
 * This file is part of ocubom/twig-svg-extension
5
 *
6
 * © Oscar Cubo Medina <https://ocubom.github.io>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ocubom\Twig\Extension\Svg\Processor;
13
14
class ClassProcessor implements ProcessorInterface
15
{
16 5
    public function __invoke(\DOMElement $svg, array $options = []): \DOMElement
17
    {
18 5
        $generator = function () use ($svg, $options): iterable {
19 5
            yield from $options['class'] ?? [];
20
21 5
            yield from preg_split(
22 5
                '@\s+@',
23 5
                $svg->getAttribute('class')
24 5
            );
25 5
        };
26
27 5
        $classes = [];
28 5
        foreach ($generator() as $class) {
29 5
            $class = trim($class);
30
            if (
31 5
                !empty($class)
32
                &&
33 5
                !in_array(
34 5
                    mb_strtolower($class),
35 5
                    $options['class_block'] ?? []
36 5
                )
37
            ) {
38 1
                $classes[$class] = $class;
39
            }
40
        }
41
42 5
        if (count($classes) > 0) {
43 1
            $svg->setAttribute('class', implode(' ', $classes));
44
        } else {
45 5
            $svg->removeAttribute('class');
46
        }
47
48 5
        return $svg;
49
    }
50
}
51