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