Completed
Push — master ( e3192d...546b1b )
by Edgar
04:08
created

SVG::getElement()   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\container;
3
4
use DOMElement;
5
use nstdio\svg\Base;
6
use nstdio\svg\ElementFactoryInterface;
7
use nstdio\svg\ElementStorage;
8
use nstdio\svg\traits\ChildTrait;
9
use nstdio\svg\traits\ElementTrait;
10
use nstdio\svg\util\KeyValueWriter;
11
use nstdio\svg\XMLDocumentInterface;
12
13
/**
14
 * Class SVG
15
 *
16
 * @property string viewBox
17
 * @package nstdio\svg
18
 * @author  Edgar Asatryan <[email protected]>
19
 */
20
class SVG extends Base implements ContainerInterface, ElementFactoryInterface
21
{
22
    use ElementTrait, ChildTrait;
23
24
    /**
25
     * @var XMLDocumentInterface
26
     */
27
    private $svg;
28
29
    public function __construct($width = 640, $height = 480, XMLDocumentInterface $dom = null)
30
    {
31
        parent::__construct($dom);
32
33
        $this->svg = $this->element('svg');
34
        $this->child = new ElementStorage();
35
36
        $this->apply([
37
            'width' => $width,
38
            'height' => $height,
39
            'version' => '1.1',
40
            'xmlns' => self::XML_NS,
41
            'viewBox' => sprintf("0 0 %d %d", $width, $height)
42
        ]);
43
44
        $this->domImpl->appendChild($this->svg);
45
46
        $this->svg->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', self::XML_NS_XLINK);
47
48
        new Defs($this);
49
    }
50
51
    /**
52
     * @param array $assoc
53
     *
54
     * @return mixed
55
     */
56
    public function apply(array $assoc)
57
    {
58
        KeyValueWriter::apply($this->svg, $assoc);
59
60
        return $this;
61
    }
62
63
    public function getRoot()
64
    {
65
        return $this->svg;
66
    }
67
68
    public function getName()
69
    {
70
        return 'svg';
71
    }
72
73
    /**
74
     * @return DOMElement
75
     */
76
    public function getElement()
77
    {
78
        return $this->svg->getElement();
79
    }
80
81
    public function createElement($name, $value = null, $attributes = [])
82
    {
83
        $element = $this->domImpl->createElement($name, $value);
84
85
        KeyValueWriter::apply($element, $attributes);
86
87
        return $element;
88
    }
89
90
    public function __toString()
91
    {
92
        return $this->draw();
93
    }
94
95
    public function draw()
96
    {
97
        return $this->domImpl->saveHTML();
98
    }
99
100
    public function asBase64()
101
    {
102
        $data = base64_encode($this->domImpl->saveHTML());
103
        return "data:image/svg+xml;base64,{$data}";
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109
    public function copy(array $apply = [], array $ignore = [], ContainerInterface $parent = null)
110
    {
111
        throw new \BadMethodCallException("You cannot copy SVG element.");
112
    }
113
}
114