Skip to content

Commit

Permalink
Merge pull request #348 from hydephp/create-static-array-parser
Browse files Browse the repository at this point in the history
Create static array parser hydephp/develop@09a038b
  • Loading branch information
github-actions committed Aug 5, 2022
1 parent 980d64e commit 7280f86
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
51 changes: 49 additions & 2 deletions src/Actions/BladeMatterParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,62 @@ public static function extractValue(string $line): string
return trim($key);
}

/** @internal */
/** @internal Return the proper type for the string */
public static function normalizeValue($value): mixed
{
$value = trim($value);

if ($value === 'null') {
return null;
}

if (static::isValueArrayString($value)) {
return static::parseArrayString($value);
}

// This will cast integers, floats, and booleans to their respective types
// Still working on a way to handle arrays and objects
// Still working on a way to handle multidimensional arrays and objects
return json_decode($value) ?? $value;
}

/** @internal */
public static function parseArrayString(string $string): array
{
$array = [];

// Trim input string
$string = trim($string);

// Check if string is an array
if (! static::isValueArrayString($string)) {
throw new \RuntimeException('Failed parsing BladeMatter array. Input string must follow array syntax.');
}

// Check if string is multidimensional (not yet supported)
if (substr_count($string, '[') > 1 || substr_count($string, ']') > 1) {
throw new \RuntimeException('Failed parsing BladeMatter array. Multidimensional arrays are not supported yet.');
}

// Remove opening and closing brackets
$string = substr($string, 1, strlen($string) - 2);

// tokenize string between commas
$tokens = explode(',', $string);

// Parse each token
foreach ($tokens as $entry) {
// Split string into key/value pairs
$pair = explode('=>', $entry);

// Add key/value pair to array
$array[static::normalizeValue(trim(trim($pair[0]), "'"))] = static::normalizeValue(trim(trim($pair[1]), "'"));
}

return $array;
}

protected static function isValueArrayString(string $string): bool
{
return str_starts_with($string, '[') && str_ends_with($string, ']');
}
}
34 changes: 33 additions & 1 deletion tests/Feature/BladeMatterParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public function test_can_parse_front_matter_with_various_formats()
}
}

public function test_can_parse_front_matter_with_array()
{
$document = "@php(\$foo = ['bar' => 'baz'])";
$this->assertEquals(['foo' => ['bar' => 'baz']], BladeMatterParser::parseString($document));
}

public function test_line_matches_front_matter()
{
$this->assertTrue(BladeMatterParser::lineMatchesFrontMatter('@php($foo = "bar")'));
Expand Down Expand Up @@ -97,6 +103,32 @@ public function test_normalize_value()
$this->assertSame(1.0, BladeMatterParser::normalizeValue('1.0'));
$this->assertSame(0.0, BladeMatterParser::normalizeValue('0.0'));
$this->assertSame(null, BladeMatterParser::normalizeValue('null'));
$this->assertSame('["foo" => "bar"]', BladeMatterParser::normalizeValue('["foo" => "bar"]'));
$this->assertSame(['foo' => 'bar'], BladeMatterParser::normalizeValue('["foo" => "bar"]'));
$this->assertSame(['foo' => 'bar'], BladeMatterParser::normalizeValue("['foo' => 'bar']"));
}

public function test_parse_array_string()
{
$this->assertSame(['foo' => 'bar'], BladeMatterParser::parseArrayString('["foo" => "bar"]'));
$this->assertSame(['foo' => 'bar'], BladeMatterParser::parseArrayString('["foo" => "bar"]'));
$this->assertSame(['foo' => 'bar'], BladeMatterParser::parseArrayString("['foo' => 'bar']"));

$this->assertSame(['foo' => 'bar', 'bar' => 'baz'], BladeMatterParser::parseArrayString('["foo" => "bar", "bar" => "baz"]'));
$this->assertSame(['foo' => 'true'], BladeMatterParser::parseArrayString('["foo" => "true"]'));
$this->assertSame(['foo' => true], BladeMatterParser::parseArrayString('["foo" => true]'));
$this->assertSame(['foo' => '1'], BladeMatterParser::parseArrayString('["foo" => "1"]'));
$this->assertSame(['foo' => 1], BladeMatterParser::parseArrayString('["foo" => 1]'));
}

public function test_parse_invalid_array_string()
{
$this->expectException(\RuntimeException::class);
BladeMatterParser::parseArrayString('foo');
}

public function test_parse_multidimensional_array_string()
{
$this->expectException(\RuntimeException::class);
BladeMatterParser::parseArrayString('["foo" => ["bar" => "baz"]]');
}
}

0 comments on commit 7280f86

Please sign in to comment.