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

AttributeRenderer::renderAttributes()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

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