KeyValueWriter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 56
c 0
b 0
f 0
wmc 10
lcom 0
cbo 1
ccs 27
cts 27
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 6 2
A allAttributes() 0 13 3
A styleArrayToString() 0 4 1
A array2String() 0 14 4
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
}