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\Console; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Reith\ToyRobot\Messaging\Command\PlaceRobot; |
18
|
|
|
|
19
|
|
|
class Place extends Command |
20
|
|
|
{ |
21
|
|
|
private const ARG = 'X,Y,F'; |
22
|
|
|
|
23
|
|
|
private const DEFAULT_INSTRUCTION = '0,0,E'; |
24
|
|
|
|
25
|
14 |
|
protected function configure() |
26
|
|
|
{ |
27
|
14 |
|
$argMsg = 'The optional placement instructions. [X,Y] is the position. [F] is the direction: [N|E|S|W]'; |
28
|
|
|
|
29
|
|
|
$this |
30
|
14 |
|
->setName('PLACE') |
31
|
14 |
|
->setDescription('Place the robot on the table') |
32
|
14 |
|
->addArgument(self::ARG, InputArgument::OPTIONAL, $argMsg) |
33
|
14 |
|
->setHelp(<<<EOT |
34
|
14 |
|
The <info>PLACE</info> instruction will set a robot on the table. |
35
|
|
|
|
36
|
|
|
<info>./toyrobot</info> <comment>PLACE X,Y,F</comment> |
37
|
|
|
|
38
|
|
|
<comment>[X,Y]</comment> is the coordinates of the starting position Robot, default is [0,0] |
39
|
|
|
<comment>[F]</comment> is the direction the robot is facing, default is [E]ast |
40
|
|
|
|
41
|
|
|
e.g. |
42
|
|
|
`./toyrobot PLACE` to place a robot at [0,0] facing East. |
43
|
|
|
`./toyrobot PLACE 1,1` to place a robot at [1,1] facing East. |
44
|
|
|
`./toyrobot PLACE 2,3,S` to place a robot at [2,3] facing South. |
45
|
|
|
|
46
|
|
|
EOT |
47
|
|
|
); |
48
|
14 |
|
} |
49
|
|
|
|
50
|
12 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
51
|
|
|
{ |
52
|
12 |
|
$instruction = $input->getArgument(self::ARG) ?: self::DEFAULT_INSTRUCTION; |
53
|
|
|
|
54
|
12 |
|
$validInstructionRegEx = '/^[0-4],[0-4](,[NESW])?$/'; |
55
|
|
|
|
56
|
12 |
|
if (1 !== preg_match($validInstructionRegEx, $instruction)) { |
57
|
7 |
|
throw new \RuntimeException( |
58
|
7 |
|
sprintf('Instruction [%s] is not valid', $instruction) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
6 |
|
$parts = explode(',', $instruction); |
63
|
|
|
|
64
|
6 |
|
[$x, $y] = $parts; |
|
|
|
|
65
|
|
|
|
66
|
6 |
|
$direction = 3 === count($parts) ? $parts[2] : 'E'; |
67
|
|
|
|
68
|
6 |
|
$output->writeln( |
69
|
6 |
|
sprintf('Using instruction [%d,%d,%s]', $x, $y, $direction) |
70
|
|
|
); |
71
|
|
|
|
72
|
6 |
|
$message = new PlaceRobot([$x, $y], $direction); |
73
|
|
|
|
74
|
6 |
|
$this->getHelper('bus')->postCommand($message); |
|
|
|
|
75
|
6 |
|
} |
76
|
|
|
} |
77
|
|
|
|
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.