ClassProcessor::getClasses()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 2
crap 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