1 | <?php |
||
19 | class Robot implements \Serializable |
||
20 | { |
||
21 | /** |
||
22 | * Purchased by Rupert, then tanked :). |
||
23 | * |
||
24 | * @var SpaceInterface |
||
25 | */ |
||
26 | private $mySpace; |
||
27 | |||
28 | private $position; |
||
29 | |||
30 | private $facingDirection; |
||
31 | |||
32 | 13 | private function __construct( |
|
41 | |||
42 | /** |
||
43 | * @param SpaceInterface $space |
||
44 | * @param Vector $position |
||
45 | * @param string|null $facingDirection |
||
46 | * |
||
47 | * @return Robot |
||
48 | * |
||
49 | * @throws Assert\AssertionFailedException |
||
50 | */ |
||
51 | 14 | public static function create( |
|
65 | |||
66 | /** |
||
67 | * @return Robot |
||
68 | */ |
||
69 | 1 | public function move(): Robot |
|
70 | { |
||
71 | 1 | $this->validateCanMove(); |
|
72 | |||
73 | 1 | $this->position = $this->mySpace->move( |
|
74 | 1 | $this->position, |
|
75 | 1 | $this->facingDirection->getDirectionAsVector() |
|
76 | ); |
||
77 | |||
78 | 1 | return $this; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return Robot |
||
83 | */ |
||
84 | 1 | public function left(): Robot |
|
85 | { |
||
86 | 1 | $this->facingDirection->rotateLeft(); |
|
87 | |||
88 | 1 | return $this; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return Robot |
||
93 | */ |
||
94 | 1 | public function right(): Robot |
|
95 | { |
||
96 | 1 | $this->facingDirection->rotateRight(); |
|
97 | |||
98 | 1 | return $this; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return Robot |
||
103 | */ |
||
104 | 2 | public function moveNorthward(): Robot |
|
115 | |||
116 | /** |
||
117 | * @return Robot |
||
118 | */ |
||
119 | 1 | public function moveEastward(): Robot |
|
130 | |||
131 | /** |
||
132 | * @return Robot |
||
133 | */ |
||
134 | 2 | public function moveSouthward(): Robot |
|
145 | |||
146 | /** |
||
147 | * @return Robot |
||
148 | */ |
||
149 | 2 | public function moveWestward(): Robot |
|
160 | |||
161 | /** |
||
162 | * @return Vector |
||
163 | */ |
||
164 | 1 | public function getPosition(): Vector |
|
168 | |||
169 | /** |
||
170 | * @return string In the form 'X,Y,F' |
||
171 | */ |
||
172 | 8 | public function getReportAsString(): string |
|
178 | |||
179 | /** |
||
180 | * Simplify the serialization of the Robot to |
||
181 | * scalars |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | public function serialize(): string |
||
193 | |||
194 | /** |
||
195 | * Re-hydrate the Robot |
||
196 | * |
||
197 | * @param string $data |
||
198 | */ |
||
199 | public function unserialize($data) |
||
206 | |||
207 | /** |
||
208 | * @return string |
||
209 | */ |
||
210 | 8 | private function getFacingDirectionAsString(): string |
|
214 | |||
215 | /** |
||
216 | * validateCanMove. |
||
217 | * |
||
218 | * The robot requires a space during construction, however, |
||
219 | * this is extra precautionary in case the space is removed |
||
220 | * |
||
221 | * @throws NotPlacedInSpaceException |
||
222 | */ |
||
223 | 5 | private function validateCanMove(): void |
|
231 | } |
||
232 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.