1
|
|
|
<?php |
2
|
|
|
namespace nstdio\svg\shape; |
3
|
|
|
|
4
|
|
|
use nstdio\svg\ElementInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Rect |
8
|
|
|
* The 'rect' element defines a rectangle which is axis-aligned with the current user coordinate system. Rounded |
9
|
|
|
* rectangles can be achieved by setting appropriate values for attributes 'rx' and 'ry'. |
10
|
|
|
* |
11
|
|
|
* @link https://www.w3.org/TR/SVG11/shapes.html#RectElement |
12
|
|
|
* @property float rx For rounded rectangles, the x-axis radius of the ellipse used to round off the corners of the |
13
|
|
|
* rectangle. A negative value is an error |
14
|
|
|
* @property float ry For rounded rectangles, the y-axis radius of the ellipse used to round off the corners of the |
15
|
|
|
* rectangle. A negative value is an error |
16
|
|
|
* @property float x The x-axis coordinate of the side of the rectangle which has the smaller x-axis coordinate value |
17
|
|
|
* in the current user coordinate system. If the attribute is not specified, the effect is as if a value of |
18
|
|
|
* "0" were specified. |
19
|
|
|
* @property float y The y-axis coordinate of the side of the rectangle which has the smaller y-axis coordinate value |
20
|
|
|
* in the current user coordinate system. If the attribute is not specified, the effect is as if a value of |
21
|
|
|
* "0" were specified. |
22
|
|
|
* @package nstdio\svg\shape |
23
|
|
|
* @author Edgar Asatryan <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Rect extends Shape |
26
|
|
|
{ |
27
|
|
|
public function __construct(ElementInterface $parent, $height, $width, $x = 0, $y = 0) |
28
|
|
|
{ |
29
|
|
|
parent::__construct($parent); |
30
|
|
|
|
31
|
|
|
$this->height = $height; |
32
|
|
|
$this->width = $width; |
33
|
|
|
$this->x = $x; |
34
|
|
|
$this->y = $y; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param float $radius |
39
|
|
|
*/ |
40
|
|
|
public function setBorderRadius($radius) |
41
|
|
|
{ |
42
|
|
|
$this->rx = $radius; |
43
|
|
|
$this->ry = $radius; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return Path |
48
|
|
|
*/ |
49
|
|
|
public function toPath() |
50
|
|
|
{ |
51
|
|
|
$path = new Path($this->getRoot(), $this->x + $this->rx, $this->y); |
|
|
|
|
52
|
|
|
list($x, $y, $width, $height, $rx, $ry) = [$this->x, $this->y, $this->width, $this->height, $this->rx, $this->ry]; |
53
|
|
|
if ($rx === null) { |
54
|
|
|
$rx = 0; |
55
|
|
|
} |
56
|
|
|
if ($ry === null) { |
57
|
|
|
$ry = 0; |
58
|
|
|
} |
59
|
|
|
$path->hLineTo($x + $width - $rx) |
60
|
|
|
->arcTo($rx, $ry, 0, false, true, $x + $width, $y + $ry) |
61
|
|
|
->lineTo($x + $width, $y + $height - $ry) |
62
|
|
|
->arcTo($rx, $ry, 0, false, true, $x + $width - $rx, $y + $height) |
63
|
|
|
->lineTo($x + $rx, $y + $height) |
64
|
|
|
->arcTo($rx, $ry, 0, false, true, $x, $y + $height - $ry) |
65
|
|
|
->lineTo($x, $y + $ry) |
66
|
|
|
->arcTo($rx, $ry, 0, false, true, $x + $rx, $y); |
67
|
|
|
|
68
|
|
|
$attributes = $this->allAttributes(['width', 'height', 'x', 'y', 'rx', 'ry']); |
69
|
|
|
|
70
|
|
|
foreach ($attributes as $key => $value) { |
71
|
|
|
$path->{$key} = $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $path; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getName() |
78
|
|
|
{ |
79
|
|
|
return 'rect'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function union(array $rect1 , array $rect2) |
83
|
|
|
{ |
84
|
|
|
$result['x'] = min($rect1['x'], $rect2['x']); |
|
|
|
|
85
|
|
|
$result['y'] = min($rect1['y'], $rect2['y']); |
86
|
|
|
|
87
|
|
|
$rect1Width = $rect1['x'] + $rect1['width'] - min($rect1['x'], $rect2['x']); |
88
|
|
|
$rect2Width = $rect2['x'] + $rect2['width'] - min($rect1['x'], $rect2['x']); |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
$rect1Height = $rect1['y'] + $rect1['height'] - min($rect1['y'], $rect2['y']); |
92
|
|
|
$rect2Height = $rect2['y'] + $rect2['height'] - min($rect1['y'], $rect2['y']); |
93
|
|
|
|
94
|
|
|
$result['width'] = max($rect1Width, $rect2Width); |
95
|
|
|
$result['height'] = max($rect1Height, $rect2Height); |
96
|
|
|
|
97
|
|
|
return $result; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public static function boxFromPoints($x1, $y1, $x2, $y2) |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
|
|
'width' => max($x1, $x2) - min($x1, $x2), |
104
|
|
|
'height' => max($y1, $y2) - min($y1, $y2), |
105
|
|
|
'x' => min($x1, $x2), |
106
|
|
|
'y' => min($y1, $y2), |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getCenterX() |
111
|
|
|
{ |
112
|
|
|
return $this->x + ($this->width / 2); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function getCenterY() |
116
|
|
|
{ |
117
|
|
|
return $this->y + ($this->width / 2); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getBoundingBox() |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
'width' => $this->width, |
124
|
|
|
'height' => $this->height, |
125
|
|
|
'x' => $this->x, |
126
|
|
|
'y' => $this->y, |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
} |
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: