1
|
|
|
<?php |
2
|
|
|
namespace nstdio\svg\shape; |
3
|
|
|
|
4
|
|
|
|
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
|
|
|
public function addPointArray(array $coordinatePairs) |
21
|
|
|
{ |
22
|
|
|
foreach ($coordinatePairs as $pair) { |
23
|
|
|
|
24
|
|
|
if (!is_array($pair) || count($pair) !== 2) { |
25
|
|
|
continue; |
26
|
|
|
} |
27
|
|
|
$pair = array_values($pair); |
28
|
|
|
$this->addPoint($pair[0], $pair[1]); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function addPoint($x, $y) |
35
|
|
|
{ |
36
|
|
|
if ($this->points === null) { |
37
|
|
|
$this->points = "$x,$y"; |
38
|
|
|
} else { |
39
|
|
|
$this->points .= " $x,$y"; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return Path | null |
47
|
|
|
*/ |
48
|
|
|
public function toPath() |
49
|
|
|
{ |
50
|
|
|
$pts = $this->pointsAsArray(); |
51
|
|
|
if (!empty($pts)) { |
52
|
|
|
$path = new Path($this->getRoot(), $pts[0][0], $pts[0][1]); |
|
|
|
|
53
|
|
|
unset($pts[0]); |
54
|
|
|
foreach ($pts as $point) { |
55
|
|
|
$path->lineTo($point[0], $point[1]); |
56
|
|
|
} |
57
|
|
|
return $path; |
58
|
|
|
} |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getName() |
63
|
|
|
{ |
64
|
|
|
return 'polygon'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getCenterX() |
68
|
|
|
{ |
69
|
|
|
return $this->getBoundingBox()['width'] / 2; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function getCenterY() |
73
|
|
|
{ |
74
|
|
|
return $this->getBoundingBox()['height'] / 2; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
private function pointsAsArray() |
81
|
|
|
{ |
82
|
|
|
$pts = []; |
83
|
|
|
foreach (explode(' ', $this->points) as $item) { |
84
|
|
|
$tmp = explode(',', $item); |
85
|
|
|
if (count($tmp) !== 2) { |
86
|
|
|
continue; |
87
|
|
|
} |
88
|
|
|
$pts[] = $tmp; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $pts; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getBoundingBox() |
95
|
|
|
{ |
96
|
|
|
$x1 = $y1 = PHP_INT_MAX; |
97
|
|
|
$x2 = $y2 = -PHP_INT_MAX; |
98
|
|
|
|
99
|
|
|
foreach ($this->pointsAsArray() as $value) { |
100
|
|
|
$x1 = min($x1, $value[0]); |
101
|
|
|
$x2 = max($x2, $value[0]); |
102
|
|
|
$y1 = min($y1, $value[1]); |
103
|
|
|
$y2 = max($y2, $value[1]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return Rect::boxFromPoints($x1, $y1, $x2, $y2); |
107
|
|
|
} |
108
|
|
|
} |
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: