Completed
Pull Request — master (#5)
by Dan
04:20
created

Box::getX()   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 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SixtyNine\Cloud\Model;
4
5
use Imagine\Image\BoxInterface;
6
use Imagine\Image\Point;
7
use Imagine\Image\PointInterface;
8
use JMS\Serializer\Annotation as JMS;
9
10
/**
11
 * An axis-aligned rectangle with collision detection
12
 */
13
class Box
14
{
15
    /**
16
     * @var float
17
     * @JMS\Type("float")
18
     */
19
    protected $x;
20
21
    /**
22
     * @var float
23
     * @JMS\Type("float")
24
     */
25
    protected $y;
26
27
    /**
28
     * @var float
29
     * @JMS\Type("float")
30
     */
31
    protected $width;
32
33
    /**
34
     * @var float
35
     * @JMS\Type("float")
36
     */
37
    protected $height;
38
39
    /**
40
     * @var float
41
     * @JMS\Exclude()
42
     */
43
    protected $top;
44
45
    /**
46
     * @var float
47
     * @JMS\Exclude()
48
     */
49
    protected $bottom;
50
51
    /**
52
     * @var float
53
     * @JMS\Exclude()
54
     */
55
    protected $left;
56
57
    /**
58
     * @var float
59
     * @JMS\Exclude()
60
     */
61
    protected $right;
62
63 29
    public function __construct($x, $y, $width, $height)
64
    {
65 29
        $this->x = $x;
66 29
        $this->y = $y;
67 29
        $this->width = $width;
68 29
        $this->height = $height;
69
70 29
        $this->update();
71 29
    }
72
73 1
    public static function constructFromImagine(PointInterface $point, BoxInterface $box)
74
    {
75 1
        return new self($point->getX(), $point->getY(), $box->getWidth(), $box->getHeight());
76
    }
77
78 29
    public function update()
79
    {
80 29
        $this->left = $this->x;
81 29
        $this->right = $this->x + $this->width;
82 29
        $this->top = $this->y;
83 29
        $this->bottom = $this->y + $this->height;
84 29
    }
85
86
    /**
87
     * Detect box collision
88
     * This algorithm only works with Axis-Aligned boxes!
89
     * @param Box $box The other rectangle to test collision with
90
     * @return boolean True is the boxes collide, false otherwise
91
     */
92 12
    public function intersects(Box $box)
93
    {
94 12
        return ($this->getLeft() < $box->getRight()
95 12
            && $this->getRight() > $box->getLeft()
96 12
            && $this->getTop() < $box->getBottom()
97 12
            && $this->getBottom() > $box->getTop()
98
        );
99
    }
100
101
    /**
102
     * @param Box $box
103
     * @return bool
104
     */
105 5
    public function inside(Box $box)
106
    {
107 5
        return ($this->getLeft() >= $box->getLeft()
108 5
            && $this->getRight() <= $box->getRight()
109 5
            && $this->getTop() >= $box->getTop()
110 5
            && $this->getBottom() <= $box->getBottom()
111
        );
112
    }
113
114
    /**
115
     * @param float $deltaX
116
     * @param float $deltaY
117
     * @return \SixtyNine\Cloud\Model\Box
118
     */
119 5
    public function move($deltaX, $deltaY)
120
    {
121 5
        return new self($this->getX() + $deltaX, $this->getY() + $deltaY, $this->getWidth(), $this->getHeight());
122
    }
123
124
    public function resize($count)
125
    {
126
        return new self(
127
            $this->getX() - $count,
128
            $this->getY() - $count,
129
            $this->getWidth() + 2 * $count,
130
            $this->getHeight() + 2 * $count
131
        );
132
    }
133
134
    /**
135
     * @return float
136
     */
137 10
    public function getBottom()
138
    {
139 10
        return $this->bottom;
140
    }
141
142
    /**
143
     * @return float
144
     */
145 25
    public function getHeight()
146
    {
147 25
        return $this->height;
148
    }
149
150
    /**
151
     * @return float
152
     */
153 12
    public function getLeft()
154
    {
155 12
        return $this->left;
156
    }
157
158
    /**
159
     * @return float
160
     */
161 12
    public function getRight()
162
    {
163 12
        return $this->right;
164
    }
165
166
    /**
167
     * @return float
168
     */
169 10
    public function getTop()
170
    {
171 10
        return $this->top;
172
    }
173
174
    /**
175
     * @return float
176
     */
177 25
    public function getWidth()
178
    {
179 25
        return $this->width;
180
    }
181
182
    /**
183
     * @return float
184
     */
185 22
    public function getX()
186
    {
187 22
        return $this->x;
188
    }
189
190
    /**
191
     * @return float
192
     */
193 22
    public function getY()
194
    {
195 22
        return $this->y;
196
    }
197
198
    /**
199
     * @return Point
200
     */
201 1
    public function getPosition()
202
    {
203 1
        return new Point($this->getX(), $this->getY());
204
    }
205
206
    /**
207
     * @return \Imagine\Image\Box
208
     */
209 5
    public function getDimensions()
210
    {
211 5
        return new \Imagine\Image\Box($this->getWidth(), $this->getHeight());
212
    }
213
214 5
    function __toString()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
215
    {
216 5
        return sprintf('(%s, %s) x (%s, %s)', $this->x, $this->y, $this->width, $this->height);
217
    }
218
}
219
220