Completed
Push — master ( 7c66eb...e496eb )
by Edgar
06:39
created

SVG::createElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
namespace nstdio\svg\container;
3
4
use DOMElement;
5
use Hoa\Mime\Mime;
6
use nstdio\svg\Base;
7
use nstdio\svg\ElementFactoryInterface;
8
use nstdio\svg\ElementStorage;
9
use nstdio\svg\output\IOFormat;
10
use nstdio\svg\output\Output;
11
use nstdio\svg\output\OutputInterface;
12
use nstdio\svg\output\SVGOutputInterface;
13
use nstdio\svg\traits\ChildTrait;
14
use nstdio\svg\traits\ElementTrait;
15
use nstdio\svg\util\KeyValueWriter;
16
use nstdio\svg\XMLDocumentInterface;
17
18
/**
19
 * Class SVG
20
 *
21
 * @property string viewBox
22
 * @package nstdio\svg
23
 * @author  Edgar Asatryan <[email protected]>
24
 */
25
class SVG extends Base implements ContainerInterface, ElementFactoryInterface, SVGOutputInterface
26
{
27
    use ElementTrait, ChildTrait;
28
29
    /**
30
     * @var XMLDocumentInterface
31
     */
32
    private $svg;
33
34
    /**
35
     * @var OutputInterface
36
     */
37
    private $outputImpl;
38
39 121
    public function __construct($width = 640, $height = 480, XMLDocumentInterface $dom = null)
40
    {
41 121
        parent::__construct($dom);
42
43 121
        $this->svg = $this->element('svg');
44 121
        $this->child = new ElementStorage();
45 121
        $this->outputImpl = new Output();
46
47 121
        $this->apply([
48 121
            'width'   => $width,
49 121
            'height'  => $height,
50 121
            'version' => '1.1',
51 121
            'xmlns'   => self::XML_NS,
52 121
            'viewBox' => sprintf("0 0 %d %d", $width, $height),
53 121
        ]);
54
55 121
        $this->domImpl->appendChild($this->svg);
56
57 121
        $this->svg->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', self::XML_NS_XLINK);
58
59 121
        new Defs($this);
60 121
    }
61
62
    /**
63
     * @param array $assoc
64
     *
65
     * @return mixed
66
     */
67 121
    public function apply(array $assoc)
68
    {
69 121
        KeyValueWriter::apply($this->svg, $assoc);
70
71 121
        return $this;
72
    }
73
74 3
    public function getRoot()
75
    {
76 3
        return $this->svg;
77
    }
78
79 2
    public function getName()
80
    {
81 2
        return 'svg';
82
    }
83
84
    /**
85
     * @return DOMElement
86
     */
87 121
    public function getElement()
88
    {
89 121
        return $this->svg->getElement();
90
    }
91
92 121
    public function createElement($name, $value = null, $attributes = [])
93
    {
94 121
        $element = $this->domImpl->createElement($name, $value);
95
96 121
        KeyValueWriter::apply($element, $attributes);
97
98 121
        return $element;
99
    }
100
101
    public function __toString()
102
    {
103
        return $this->draw();
104
    }
105
106 42
    public function draw()
107
    {
108 42
        return $this->domImpl->saveHTML();
109
    }
110
111
    public function asBase64()
112
    {
113
        $data = base64_encode($this->domImpl->saveHTML());
114
115
        return "data:image/svg+xml;base64,{$data}";
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function copy(array $apply = [], array $ignore = [], ContainerInterface $parent = null)
122
    {
123
        throw new \BadMethodCallException("You cannot copy SVG element.");
124
    }
125
126
    /**
127
     * Returns escaped svg as plain text.
128
     *
129
     * @return string
130
     */
131
    public function asString()
132
    {
133
        return htmlspecialchars($this->draw());
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139
    public function asSVG()
140
    {
141
        return $this->draw();
142
    }
143
144
    /**
145
     * @inheritdoc
146
     */
147
    public function asSVGZ($sendHeader = false)
148
    {
149
        if ($sendHeader && !headers_sent()) {
150
            header("Content-Encoding: gzip");
151
        }
152
153
        return gzencode($this->draw(), 9);
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159
    public function asDataUriBase64()
160
    {
161
        $encoded = base64_encode($this->draw());
162
        $mimeType = Mime::getMimeFromExtension('svg');
163
164
        return sprintf("data:%s;base64,%s", $mimeType, $encoded);
165
    }
166
167
    /**
168
     * @inheritdoc
169
     */
170
    public function asFile($name, $prettyPrint = false, $override = false)
171
    {
172
        return $this->outputImpl->file(
173
            $name,
174
            $this->domImpl->saveXML($prettyPrint),
175
            $override
176
        );
177
    }
178
179
    /**
180
     * @inheritdoc
181
     */
182
    public function asImageFile($name, $format = IOFormat::PNG, $override = false)
183
    {
184
        return $this->outputImpl->imageFile(
185
            $this->domImpl->saveXML(),
0 ignored issues
show
Bug introduced by
The call to saveXML() misses a required argument $formatOutput.

This check looks for function calls that miss required arguments.

Loading history...
186
            $name,
187
            $format,
188
            $override
189
        );
190
    }
191
192
    /**
193
     * @inheritdoc
194
     */
195
    public function asImage($format = IOFormat::PNG, $sendHeader = false)
196
    {
197
        return $this->outputImpl->image(
198
            $this->domImpl->saveXML(),
0 ignored issues
show
Bug introduced by
The call to saveXML() misses a required argument $formatOutput.

This check looks for function calls that miss required arguments.

Loading history...
199
            $format,
200
            $sendHeader
201
        );
202
    }
203
}
204