Completed
Push — master ( fb3943...fbbc4e )
by Edgar
04:02
created

KeyValueWriter::allAttributes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 8
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 149
    public static function apply(XMLDocumentInterface $element, array $attributes)
15
    {
16 149
        foreach ($attributes as $key => $value) {
17 149
            $element->setAttribute($key, $value);
18 149
        }
19 149
    }
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 4
    public static function array2String(array $input, $keyValueDelimiter, $valueEndDelimiter)
47
    {
48 4
        $ret = '';
49 4
        foreach ($input as $key => $value) {
50 4
            if (is_array($value)) {
51 1
                $ret .= self::array2String($value, $keyValueDelimiter, $valueEndDelimiter);
52 4
            } elseif ($key) {
53 4
                $ret .= $key . $keyValueDelimiter . $value . $valueEndDelimiter;
54
55 4
            }
56 4
        }
57
58 4
        return $ret;
59
    }
60
}