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

KeyValueWriter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 0
cbo 1
dl 0
loc 49
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 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
}