Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
###*:::=####*:*############################
=++##*******++++
+++++++++*#++
-+++++++*++
-#####**#+
...... -##*+*#+#*
.:=====+--===.. :-#*+*#*#*
. := .*###*+#+
:= .#######
:= =#*++*
:= @@@%: -+####
-##@@@@@@: -++++*
-##@####. =####+
-###@@@%-. =###++
-######%@: =###+*
-#####%%@-. =*##+*
-####%%@@@#+. .=***#++
-######@@@@%=. .***#*++
-##=:::-#=---. +#**++**
.=#*.. .*#- -*########
.+##**. .*##+..+**#####*+##
#-.:-+###.*################################
###########################################
```
14 changes: 14 additions & 0 deletions src/Enums/DigCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace QuickFort\Enums;

enum DigCommands: string
{
case DIG = 'd';
case STAIR_DOWN = 'j';
case STAIR_UP = 'u';
case STAIR_UPDOWN = 'i';
case CHANNEL = 'h';
case RAMP = 'r';
case REMOVE = 'x';
}
9 changes: 9 additions & 0 deletions src/Enums/LayerCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace QuickFort\Enums;

enum LayerCommands: string
{
case UP = '#<';
case DOWN = '#>';
}
18 changes: 9 additions & 9 deletions src/Parser/BlueprintParserBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
{
Expand All @@ -105,7 +105,7 @@ protected function parseLineAsHeader(string $line): array
];

$line = trim($line);
if (0 !== stripos($line, '#')) {
if (!str_starts_with($line, '#')) {
return $header;
}

Expand Down Expand Up @@ -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, ')');

Expand Down
2 changes: 1 addition & 1 deletion src/Parser/BlueprintParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface BlueprintParserInterface
*
* @return void
*/
public function setBlueprint(string $blueprintText);
public function setBlueprint(string $blueprintText): void;

/**
* Get the processed blueprint layers.
Expand Down
55 changes: 34 additions & 21 deletions src/Parser/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace QuickFort\Parser;

use QuickFort\Enums\DigCommands;
use QuickFort\Enums\LayerCommands;

/**
* Class Command
*
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions src/Parser/Dig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
26 changes: 13 additions & 13 deletions tests/Unit/DataProviders/CommandDataProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace QuickFort\tests\Unit\DataProviders;

use QuickFort\Enums\DigCommands;
use QuickFort\Enums\LayerCommands;

class CommandDataProviders
{
public static function commandWithExpansion(): array
Expand All @@ -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
Expand All @@ -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],
];
}

Expand Down