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
|
4 |
|
public function __construct(array $coordinates, string $direction) |
26
|
|
|
{ |
27
|
4 |
|
$this->setCoordinates($coordinates); |
28
|
4 |
|
$this->setDirection($direction); |
29
|
4 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $coordinates |
33
|
|
|
*/ |
34
|
4 |
|
private function setCoordinates(array $coordinates): void |
35
|
|
|
{ |
36
|
|
|
// Sanitize command input, ensure |
37
|
|
|
// int[] |
38
|
4 |
|
$this->coordinates = array_map( |
39
|
4 |
|
function ($coord) { |
40
|
4 |
|
Assertion::numeric($coord); |
41
|
|
|
|
42
|
4 |
|
return (int) $coord; |
43
|
4 |
|
}, |
44
|
4 |
|
$coordinates |
45
|
|
|
); |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $direction |
50
|
|
|
*/ |
51
|
4 |
|
private function setDirection(string $direction): void |
52
|
|
|
{ |
53
|
|
|
// Sanitize direction, 1 char capital |
54
|
4 |
|
$direction = strtoupper(trim($direction)); |
55
|
4 |
|
Assertion::length($direction, 1); |
56
|
4 |
|
Assertion::choice($direction, ['N', 'E', 'S', 'W']); |
57
|
4 |
|
$this->direction = $direction; |
58
|
4 |
|
} |
59
|
|
|
|
60
|
2 |
|
public function getCoordinates(): array |
61
|
|
|
{ |
62
|
2 |
|
return $this->coordinates; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getDirection(): string |
66
|
|
|
{ |
67
|
|
|
return $this->direction; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|