Passed
Push — main ( 64d07c...38d056 )
by Oscar
02:39
created

AspectRatioProcessor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
eloc 17
c 1
b 1
f 0
dl 0
loc 34
ccs 11
cts 14
cp 0.7856
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 29 6
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 AspectRatioProcessor
15
{
16
    const ATTRIBUTE_NAME = 'preserveAspectRatio';
17
    const DEFAULT_VALUE = ['xMidYMid', 'meet'];
18
19 4
    public function __invoke(\DOMElement $svg, array $options = []): \DOMElement
20
    {
21 4
        $value = preg_split('@\s+@Uis', $svg->getAttribute(self::ATTRIBUTE_NAME));
22 4
        $value = array_slice($value, 0, 2);
23
24 4
        foreach (self::DEFAULT_VALUE as $key => $val) {
25 4
            $value[$key] = mb_strtolower($value[$key] ?? '') === mb_strtolower(self::DEFAULT_VALUE[$key])
26 4
                ? null
27
                : ($value[$key] ?? null);
28
        }
29
30
        // Restore default align if meet is set
31 4
        if (null === $value[0] && null !== $value[1]) {
32
            $value[0] = self::DEFAULT_VALUE[0];
33
        }
34
35
        // Format new value
36 4
        $value = trim(($value[0] ?? '').' '.($value[1] ?? ''));
37
38
        // Apply attribute value or remove if empty
39 4
        if (empty($value)) {
40
            // Remove attribute if contains default value
41 4
            $svg->removeAttribute(self::ATTRIBUTE_NAME);
42
        } else {
43
            // Set the value
44
            $svg->setAttribute(self::ATTRIBUTE_NAME, $value);
45
        }
46
47 4
        return $svg;
48
    }
49
}
50