KeyValueWriter::allAttributes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
namespace nstdio\svg\util;
3
4
use nstdio\svg\XMLDocumentInterface;
5
6
/**
7
 * Class KeyValueWriter
8
 *
9
 * @package nstdio\svg\util
10
 * @author  Edgar Asatryan <[email protected]>
11
 */
12
class KeyValueWriter
13
{
14 150
    public static function apply(XMLDocumentInterface $element, array $attributes)
15
    {
16 150
        foreach ($attributes as $key => $value) {
17 150
            $element->setAttribute($key, $value);
18 150
        }
19 150
    }
20
21
    /**
22
     * @param \DOMNode $element
23
     * @param array    $except
24
     *
25
     * @return array
26
     */
27 10
    public static function allAttributes(\DOMNode $element, array $except = [])
28
    {
29 10
        $attributes = [];
30 10
        $length = $element->attributes->length;
0 ignored issues
show
Bug introduced by
The property length does not seem to exist in DOMNamedNodeMap.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
31 10
        for ($i = 0; $i < $length; $i++) {
32 10
            $node = $element->attributes->item($i);
33 10
            if (!in_array($node->nodeName, $except)) {
34 9
                $attributes[$node->nodeName] = $node->nodeValue;
35 9
            }
36 10
        }
37
38 10
        return $attributes;
39
    }
40
41 3
    public static function styleArrayToString(array $input)
42
    {
43 3
        return self::array2String($input, ':', ';');
44
    }
45
46
    /**
47
     * @param array  $input
48
     * @param string $keyValueDelimiter
49
     * @param string $valueEndDelimiter
50
     *
51
     * @return string
52
     */
53 4
    public static function array2String(array $input, $keyValueDelimiter, $valueEndDelimiter)
54
    {
55 4
        $ret = '';
56 4
        foreach ($input as $key => $value) {
57 4
            if (is_array($value)) {
58 1
                $ret .= self::array2String($value, $keyValueDelimiter, $valueEndDelimiter);
59 4
            } elseif ($key) {
60 4
                $ret .= $key . $keyValueDelimiter . $value . $valueEndDelimiter;
61
62 4
            }
63 4
        }
64
65 4
        return $ret;
66
    }
67
}