diff --git a/README.md b/README.md index 865bc87..1dae177 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # QuickFort Blueprint Parser -Library for parsing QuickFort blueprints. +This is a simple library for parsing basic QuickFort blueprints. Only dig +blueprints are implemented. There are no plans to actively work on this project, +but pull requests for new features and layer types will be accepted. ## Example @@ -50,3 +52,29 @@ $layers = $parser->getLayers(); [![Build Status](https://travis-ci.com/swichers/php-quickfort-parser.svg?branch=master)](https://travis-ci.com/swichers/php-quickfort-parser) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/07a22d50e78e4b66b25d0dad19567d81)](https://www.codacy.com/app/swichers/php-quickfort-parser?utm_source=github.com&utm_medium=referral&utm_content=swichers/php-quickfort-parser&utm_campaign=Badge_Grade) [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/07a22d50e78e4b66b25d0dad19567d81)](https://www.codacy.com/app/swichers/php-quickfort-parser?utm_source=github.com&utm_medium=referral&utm_content=swichers/php-quickfort-parser&utm_campaign=Badge_Coverage) + +```text +###*:::=####*:*############################ + =++##*******++++ + +++++++++*#++ + -+++++++*++ + -#####**#+ + ...... -##*+*#+#* + .:=====+--===.. :-#*+*#*#* + . := .*###*+#+ + := .####### + := =#*++* + := @@@%: -+#### + -##@@@@@@: -++++* + -##@####. =####+ + -###@@@%-. =###++ + -######%@: =###+* + -#####%%@-. =*##+* + -####%%@@@#+. .=***#++ + -######@@@@%=. .***#*++ + -##=:::-#=---. +#**++** + .=#*.. .*#- -*######## + .+##**. .*##+..+**#####*+## +#-.:-+###.*################################ +########################################### +``` diff --git a/src/Enums/DigCommands.php b/src/Enums/DigCommands.php new file mode 100644 index 0000000..7c7a497 --- /dev/null +++ b/src/Enums/DigCommands.php @@ -0,0 +1,14 @@ +'; +} diff --git a/src/Parser/BlueprintParserBase.php b/src/Parser/BlueprintParserBase.php index 25eb9fc..1fa03fd 100644 --- a/src/Parser/BlueprintParserBase.php +++ b/src/Parser/BlueprintParserBase.php @@ -15,30 +15,30 @@ class BlueprintParserBase implements BlueprintParserInterface * * @var array * - * @see \QuickFort\Parser\BlueprintParserInterface::getHeader(). + * @see BlueprintParserInterface::getHeader */ - protected $blueprintHeader; + protected array $blueprintHeader; /** * Original blueprint text. * * @var string */ - protected $originalBlueprint; + protected string $originalBlueprint; /** * The lines of the blueprint, minus the header. * * @var string[] */ - protected $blueprintLines; + protected array $blueprintLines; /** * BlueprintParserBase constructor. * - * @param string $blueprintText A blueprint to initialize with. + * @param null|string $blueprintText A blueprint to initialize with. */ - public function __construct(string $blueprintText = null) + public function __construct(?string $blueprintText = null) { if (!empty($blueprintText)) { $this->setBlueprint($blueprintText); @@ -94,7 +94,7 @@ protected function textToLines(string $text): array * @return array * An array of header information. * - * @see \QuickFort\Parser\BlueprintParserInterface::getHeader(). + * @see BlueprintParserInterface::getHeader */ protected function parseLineAsHeader(string $line): array { @@ -105,7 +105,7 @@ protected function parseLineAsHeader(string $line): array ]; $line = trim($line); - if (0 !== stripos($line, '#')) { + if (!str_starts_with($line, '#')) { return $header; } @@ -133,7 +133,7 @@ protected function parseLineAsHeader(string $line): array $line = trim(substr($line, $command_end_pos)); // Parse out starting position information. - if (0 === strpos($line, 'start(')) { + if (str_starts_with($line, 'start(')) { $start_len = strlen('start('); $closing_paren_pos = strpos($line, ')'); diff --git a/src/Parser/BlueprintParserInterface.php b/src/Parser/BlueprintParserInterface.php index cfd9959..c663c7c 100644 --- a/src/Parser/BlueprintParserInterface.php +++ b/src/Parser/BlueprintParserInterface.php @@ -28,7 +28,7 @@ interface BlueprintParserInterface * * @return void */ - public function setBlueprint(string $blueprintText); + public function setBlueprint(string $blueprintText): void; /** * Get the processed blueprint layers. diff --git a/src/Parser/Command.php b/src/Parser/Command.php index d08b9b4..78da704 100644 --- a/src/Parser/Command.php +++ b/src/Parser/Command.php @@ -2,6 +2,9 @@ namespace QuickFort\Parser; +use QuickFort\Enums\DigCommands; +use QuickFort\Enums\LayerCommands; + /** * Class Command * @@ -15,14 +18,14 @@ class Command * * @var string */ - protected $command; + protected string $command; /** * A keyed array (x,y) of expansion data. * * @var array */ - protected $expansion; + protected array $expansion; /** * Command constructor. @@ -65,26 +68,38 @@ protected function parseTextToCommand(string $text): void 'y' => 1, ]; - if (strpos($text, '(') !== false) { - $parts = explode('(', trim($text, ')')); - $xy_values = explode('x', $parts[1]); - $this->command = $parts[0]; - $this->expansion = [ - 'x' => $xy_values[0], - 'y' => $xy_values[1], - ]; + if (str_contains($text, '(')) { + $this->parseTextWithExpansion($text); } } + /** + * Parses command text that contains an expansion marker + * + * @param string $text The text to parse into command data. + * + * @return void + */ + protected function parseTextWithExpansion(string $text): void + { + $parts = explode('(', trim($text, ')')); + $xy_values = explode('x', $parts[1]); + $this->command = $parts[0]; + $this->expansion = [ + 'x' => $xy_values[0], + 'y' => $xy_values[1], + ]; + } + /** * Check if the command is an up layer navigation. * - * @return boolean + * @return bool * Returns true if the command moves up a layer. */ public function isLayerUp(): bool { - return $this->command == '#<'; + return $this->command === LayerCommands::UP->value; } /** @@ -95,39 +110,37 @@ public function isLayerUp(): bool */ public function isLayerDown(): bool { - return $this->command == '#>'; + return $this->command === LayerCommands::DOWN->value; } /** * Check if the command is allowed. * - * @return boolean + * @return bool * Returns true if the command is an allowed command. */ public function isAllowedCommand(): bool { - $commands = 'djuihrx'; - - return in_array($this->command, str_split($commands)); + return DigCommands::tryFrom($this->command) !== null; } /** * Check if the command results in no operation. * - * @return boolean + * @return bool * Returns true if there is no operation to perform. */ public function isNoOp(): bool { $noops = '#~`'; - return in_array($this->command, str_split($noops)); + return in_array($this->command, str_split($noops), true); } /** * Check if the command was a comment. * - * @return boolean + * @return bool * Returns true if the command is a comment. */ public function isComment(): bool @@ -162,7 +175,7 @@ public function getFormatted(): string /** * Check if the command has expansion information. * - * @return boolean + * @return bool * Returns true if the command has worthwhile expansion information. */ public function hasExpansion(): bool diff --git a/src/Parser/Dig.php b/src/Parser/Dig.php index ad9427e..e991679 100644 --- a/src/Parser/Dig.php +++ b/src/Parser/Dig.php @@ -13,16 +13,12 @@ class Dig extends BlueprintParserBase /** * Check if the header is valid for this type of parser. * - * @return boolean + * @return bool * Returns true if the blueprint is a dig command. */ public function checkHeader(): bool { - $header = $this->blueprintHeader; - if ($header['command'] !== 'dig') { - return false; - } - - return true; + $command = $this->blueprintHeader['command'] ?? null; + return $command === 'dig'; } } diff --git a/tests/Unit/DataProviders/CommandDataProviders.php b/tests/Unit/DataProviders/CommandDataProviders.php index 1fb26cf..0dc178e 100644 --- a/tests/Unit/DataProviders/CommandDataProviders.php +++ b/tests/Unit/DataProviders/CommandDataProviders.php @@ -2,6 +2,9 @@ namespace QuickFort\tests\Unit\DataProviders; +use QuickFort\Enums\DigCommands; +use QuickFort\Enums\LayerCommands; + class CommandDataProviders { public static function commandWithExpansion(): array @@ -23,14 +26,11 @@ public static function commandHasExpansion():array { public static function allowedCommands(): array { - return [ - ['d'], - ['j'], - ['u'], - ['h'], - ['r'], - ['x'], - ]; + $data = []; + foreach (DigCommands::cases() as $case) { + $data[] = [$case->value]; + } + return $data; } public static function disallowedCommands(): array @@ -42,16 +42,16 @@ public static function disallowedCommands(): array public static function complexCommandWithBase():array { return [ - ['d', 'd'], - ['d(3x3)', 'd'], - ['d(1x3)', 'd'], + ['d',DigCommands::DIG->value], + ['d(3x3)',DigCommands::DIG->value], + ['d(1x3)',DigCommands::DIG->value], ]; } public static function layerShifting():array { return [ - 'layer shift up' => ['#<', true], - 'layer shift down' => ['#>', false], + 'layer shift up' => [LayerCommands::UP->value, true], + 'layer shift down' => [LayerCommands::DOWN->value, false], ]; }