Test Failed
Push — master ( 483f83...a6b1f2 )
by Edgar
03:01
created

SVG::draw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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