ClassProcessor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 19
c 1
b 0
f 0
dl 0
loc 43
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 24 5
A getClasses() 0 12 1
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 13
    public function __invoke(\DOMElement $svg, array $options = []): \DOMElement
17
    {
18 13
        $blocked = array_map('mb_strtolower', $options['class_block'] ?? []);
19
20 13
        $classes = [];
21 13
        foreach ($this->getClasses($svg, $options) as $class) {
22 13
            $class = trim(/* @scrutinizer ignore-type */ $class);
23
24 13
            if (!empty($class) && !in_array(mb_strtolower($class), $blocked)) {
25 6
                $classes[$class] = $class;
26
            }
27
        }
28
29 13
        if (count($classes) > 0) {
30 6
            $svg->setAttribute('class', implode(' ', $classes));
31
        } else {
32 10
            $svg->removeAttribute('class');
33
        }
34
35
        // Remove extra options attributes
36 13
        $svg->removeAttribute('class_default');
37 13
        $svg->removeAttribute('class_block');
38
39 13
        return $svg;
40
    }
41
42
    /**
43
     * @return iterable<string>
44
     */
45 13
    private function getClasses(\DOMElement $svg, array $options): iterable
46
    {
47
        // Yield default classes (will always be added)
48 13
        yield from $options['class_default'] ?? [];
49
50
        // Yield options classes
51 13
        yield from $options['class'] ?? [];
52
53
        // Yield element classes
54 13
        yield from preg_split(
55 13
            '@\s+@',
56 13
            $svg->getAttribute('class')
57 13
        );
58
    }
59
}
60