|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* (c) 2018 Douglas Reith. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Reith\ToyRobot\Messaging\Command; |
|
12
|
|
|
|
|
13
|
|
|
use Assert\Assertion; |
|
14
|
|
|
|
|
15
|
|
|
class PlaceRobot |
|
16
|
|
|
{ |
|
17
|
|
|
private $coordinates; |
|
18
|
|
|
|
|
19
|
|
|
private $direction; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param array $coordinates |
|
23
|
|
|
* @param string $direction |
|
24
|
|
|
*/ |
|
25
|
8 |
|
public function __construct(array $coordinates, string $direction) |
|
26
|
|
|
{ |
|
27
|
8 |
|
$this->setCoordinates($coordinates); |
|
28
|
8 |
|
$this->setDirection($direction); |
|
29
|
8 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $coordinates |
|
33
|
|
|
*/ |
|
34
|
8 |
|
private function setCoordinates(array $coordinates): void |
|
35
|
|
|
{ |
|
36
|
|
|
// Sanitize command input, ensure |
|
37
|
|
|
// int[] |
|
38
|
8 |
|
$this->coordinates = array_map( |
|
39
|
|
|
function ($coord) { |
|
40
|
8 |
|
Assertion::numeric($coord); |
|
41
|
|
|
|
|
42
|
8 |
|
return (int) $coord; |
|
43
|
8 |
|
}, |
|
44
|
8 |
|
$coordinates |
|
45
|
|
|
); |
|
46
|
8 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $direction |
|
50
|
|
|
*/ |
|
51
|
8 |
|
private function setDirection(string $direction): void |
|
52
|
|
|
{ |
|
53
|
|
|
// Sanitize direction, 1 char capital |
|
54
|
8 |
|
$direction = strtoupper(trim($direction)); |
|
55
|
8 |
|
Assertion::length($direction, 1); |
|
56
|
8 |
|
Assertion::choice($direction, ['N', 'E', 'S', 'W']); |
|
57
|
8 |
|
$this->direction = $direction; |
|
58
|
8 |
|
} |
|
59
|
|
|
|
|
60
|
6 |
|
public function getCoordinates(): array |
|
61
|
|
|
{ |
|
62
|
6 |
|
return $this->coordinates; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
6 |
|
public function getDirection(): string |
|
66
|
|
|
{ |
|
67
|
6 |
|
return $this->direction; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|