|
1
|
|
|
<?php |
|
2
|
|
|
namespace nstdio\svg\shape; |
|
3
|
|
|
|
|
4
|
|
|
use nstdio\svg\ElementInterface; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Polygon |
|
9
|
|
|
* |
|
10
|
|
|
* The 'polygon' element defines a closed shape consisting of a set of connected straight line segments. |
|
11
|
|
|
* |
|
12
|
|
|
* @link https://www.w3.org/TR/SVG11/shapes.html#PolygonElement |
|
13
|
|
|
* @property string $points The points that make up the polygon. All coordinate values are in the user coordinate system. |
|
14
|
|
|
* |
|
15
|
|
|
* @package nstdio\svg\shape |
|
16
|
|
|
* @author Edgar Asatryan <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class Polygon extends Shape |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param ElementInterface $parent |
|
22
|
|
|
* @param array $coordinatePairs |
|
23
|
|
|
* |
|
24
|
|
|
* @return Polygon |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function createWithPairs(ElementInterface $parent, array $coordinatePairs) |
|
27
|
|
|
{ |
|
28
|
|
|
return self::create($parent)->addPointArray($coordinatePairs); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
public function addPointArray(array $coordinatePairs) |
|
32
|
|
|
{ |
|
33
|
3 |
|
foreach ($coordinatePairs as $pair) { |
|
34
|
|
|
|
|
35
|
3 |
|
if (!is_array($pair) || count($pair) !== 2) { |
|
36
|
1 |
|
continue; |
|
37
|
|
|
} |
|
38
|
3 |
|
$pair = array_values($pair); |
|
39
|
3 |
|
$this->addPoint($pair[0], $pair[1]); |
|
40
|
3 |
|
} |
|
41
|
|
|
|
|
42
|
3 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
5 |
|
public function addPoint($x, $y) |
|
46
|
|
|
{ |
|
47
|
5 |
|
if ($this->points === null) { |
|
48
|
5 |
|
$this->points = "$x,$y"; |
|
49
|
5 |
|
} else { |
|
50
|
4 |
|
$this->points .= " $x,$y"; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
5 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param ElementInterface $parent |
|
58
|
|
|
* |
|
59
|
|
|
* @return Polygon |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function create(ElementInterface $parent) |
|
62
|
|
|
{ |
|
63
|
|
|
return new Polygon($parent); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param ElementInterface $parent |
|
68
|
|
|
* @param $x |
|
69
|
|
|
* @param $y |
|
70
|
|
|
* |
|
71
|
|
|
* @return Polygon |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function createWithPoint(ElementInterface $parent, $x, $y) |
|
74
|
|
|
{ |
|
75
|
|
|
return self::create($parent)->addPoint($x, $y); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return Path | null |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function toPath() |
|
82
|
|
|
{ |
|
83
|
2 |
|
$pts = $this->pointsAsArray(); |
|
84
|
2 |
|
if (!empty($pts)) { |
|
85
|
1 |
|
$path = new Path($this->getRoot(), $pts[0][0], $pts[0][1]); |
|
|
|
|
|
|
86
|
1 |
|
unset($pts[0]); |
|
87
|
1 |
|
foreach ($pts as $point) { |
|
88
|
1 |
|
$path->lineTo($point[0], $point[1]); |
|
89
|
1 |
|
} |
|
90
|
1 |
|
return $path; |
|
91
|
|
|
} |
|
92
|
1 |
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
3 |
|
private function pointsAsArray() |
|
99
|
|
|
{ |
|
100
|
3 |
|
$pts = []; |
|
101
|
3 |
|
foreach (explode(' ', $this->points) as $item) { |
|
102
|
3 |
|
$tmp = explode(',', $item); |
|
103
|
3 |
|
if (count($tmp) !== 2) { |
|
104
|
1 |
|
continue; |
|
105
|
|
|
} |
|
106
|
2 |
|
$pts[] = $tmp; |
|
107
|
3 |
|
} |
|
108
|
|
|
|
|
109
|
3 |
|
return $pts; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
8 |
|
public function getName() |
|
113
|
|
|
{ |
|
114
|
8 |
|
return 'polygon'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
protected function getCenterX() |
|
118
|
|
|
{ |
|
119
|
1 |
|
return $this->getBoundingBox()['width'] / 2; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
public function getBoundingBox() |
|
123
|
|
|
{ |
|
124
|
1 |
|
$x1 = $y1 = PHP_INT_MAX; |
|
125
|
1 |
|
$x2 = $y2 = -PHP_INT_MAX; |
|
126
|
|
|
|
|
127
|
1 |
|
foreach ($this->pointsAsArray() as $value) { |
|
128
|
1 |
|
$x1 = min($x1, $value[0]); |
|
129
|
1 |
|
$x2 = max($x2, $value[0]); |
|
130
|
1 |
|
$y1 = min($y1, $value[1]); |
|
131
|
1 |
|
$y2 = max($y2, $value[1]); |
|
132
|
1 |
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
return Rect::boxFromPoints($x1, $y1, $x2, $y2); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
protected function getCenterY() |
|
138
|
|
|
{ |
|
139
|
1 |
|
return $this->getBoundingBox()['height'] / 2; |
|
140
|
|
|
} |
|
141
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: