Completed
Push — master ( dc2236...d117bd )
by Edgar
03:20
created

SVGElement::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace nstdio\svg;
3
4
use nstdio\svg\container\ContainerInterface;
5
use nstdio\svg\container\SVG;
6
use nstdio\svg\traits\ChildTrait;
7
use nstdio\svg\traits\ElementTrait;
8
use nstdio\svg\util\Identifier;
9
use nstdio\svg\util\Inflector;
10
use nstdio\svg\util\KeyValueWriter;
11
use nstdio\svg\xml\NotImplementedException;
12
13
/**
14
 * Class SVGElement
15
 *
16
 * @property string $id
17
 * @property string $fill The fill color.
18
 *
19
 * @package nstdio\svg
20
 * @author  Edgar Asatryan <[email protected]>
21
 */
22
abstract class SVGElement implements ContainerInterface, ElementFactoryInterface
23
{
24
    use ElementTrait, ChildTrait;
25
26
    private static $notConvertable = ['diffuseConstant', 'pointsAtX', 'pointsAtY', 'pointsAtZ', 'limitingConeAngle', 'tableValues', 'filterUnits', 'gradientUnits', 'viewBox', 'repeatCount', 'attributeName', 'attributeType', 'stdDeviation'];
27
28
    /**
29
     * @var XMLDocumentInterface | ElementInterface | ElementFactoryInterface | ContainerInterface
30
     */
31
    protected $root;
32
33
    /**
34
     * @var XMLDocumentInterface | ElementInterface | ElementFactoryInterface | ContainerInterface
35
     */
36
    protected $element;
37
38
    public function __construct(ElementInterface $parent)
39
    {
40
        $this->child = new ElementStorage();
41
        $this->root = $parent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parent of type object<nstdio\svg\ElementInterface> is incompatible with the declared type object<nstdio\svg\XMLDocumentInterface> of property $root.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->element = $this->createElement($this->getName());
43
        $this->root->append($this);
44
    }
45
46
    public function createElement($name, $value = null, $attributes = [])
47
    {
48
        return $this->root->createElement($name, $value, $attributes);
0 ignored issues
show
Unused Code introduced by
The call to XMLDocumentInterface::createElement() has too many arguments starting with $attributes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
49
    }
50
51
    abstract public function getName();
52
53
    final public function getRoot()
54
    {
55
        return $this->root;
56
    }
57
58
    final public function getElement()
59
    {
60
        return $this->element;
61
    }
62
63
    public function allAttributes(array $except = [])
64
    {
65
        return $this->element->attributes($except);
66
    }
67
68
    public function __get($name)
69
    {
70
        $name = Inflector::camel2dash($name);
71
        $value = $this->element->getAttribute($name);
72
        if ($value === '') {
73
            $value = $this->getXLinkAttribute($name);
74
        }
75
76
        return $value === '' ? null : $value;
77
    }
78
79
    public function __set($name, $value)
80
    {
81
        if ($name === 'id' && $value === null) {
82
            $this->element->setAttribute($name, Identifier::random('__' . $this->getName(), 5));
83
84
            return;
85
        }
86
87
        if ($value === null || $value === false || $value === '') {
88
            return;
89
        }
90
        $name = $this->convertAttributeName($name);
91
        $this->element->setAttribute($name, $value);
92
    }
93
94
    public function getXLinkAttribute($name)
95
    {
96
        return $this->element->getAttributeNS('xlink', $name);
97
    }
98
99
    /**
100
     * @param $name
101
     *
102
     * @return string
103
     */
104
    private function convertAttributeName($name)
105
    {
106
        return !in_array($name, self::$notConvertable) ? Inflector::camel2dash($name) : $name;
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112
    public function apply(array $assoc)
113
    {
114
        $filtered = [];
115
        foreach ($assoc as $attribute => $value) {
116
            $filtered[$this->convertAttributeName($attribute)] = $value;
117
        }
118
        KeyValueWriter::apply($this->element, $filtered);
119
120
        return $this;
121
    }
122
123
    protected function setAttribute($name, $value, $xLink = false)
124
    {
125
        if ($xLink === true) {
126
            $this->element->setAttributeNS('xlink', $name, $value);
127
        } else {
128
            $this->element->setAttribute($name, $value);
129
        }
130
    }
131
132
    public function getSVG()
133
    {
134
        if ($this->root instanceof SVG) {
135
            return $this->root;
136
        }
137
        $element = $this->root;
138
139
        do {
140
            $element = $element->getRoot();
141
        } while (!($element instanceof SVG));
142
143
        return $element;
144
    }
145
146
    protected function createDefs()
147
    {
148
        throw NotImplementedException::newInstance();
149
    }
150
}