Passed
Push — main ( 42b4d9...c70814 )
by Frank
01:56
created

AttributeRenderer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 31
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A renderAttributes() 0 20 5
A renderAttribute() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PrinsFrank\PhpGeoSVG\Html\Rendering;
6
7
use PrinsFrank\PhpGeoSVG\Exception\InvalidArgumentException;
8
9
class AttributeRenderer
10
{
11
    /**
12
     * @param array<string, string> $attributes
13
     * @throws InvalidArgumentException
14
     */
15 3
    public static function renderAttributes(array $attributes): ?string
16
    {
17 3
        $attributesString = null;
18 3
        foreach ($attributes as $attributeName => $attributeValue) {
19 3
            if (false === is_string($attributeName)) {
20 1
                throw new InvalidArgumentException('Attribute names have to be of type string, "' . gettype($attributeName) . '"(' . $attributeName . ') given.');
21
            }
22
23 2
            if (false === is_string($attributeValue)) {
24 1
                throw new InvalidArgumentException('Attribute values have to be of type string, "' . gettype($attributeValue) . '"(' . $attributeValue . ') given.');
25
            }
26
27 1
            if (null !== $attributesString) {
28 1
                $attributesString .= ' ';
29
            }
30
31 1
            $attributesString .= self::renderAttribute($attributeName, $attributeValue);
32
        }
33
34 1
        return $attributesString;
35
    }
36
37 1
    public static function renderAttribute(string $attributeName, string $attributeValue): string
38
    {
39 1
        return $attributeName . '="' . str_replace(['\'', '"'], ['\\\'', '\''], $attributeValue) . '"';
40
    }
41
}
42