Skip to content

Commit

Permalink
FilterSyntaxMatcher: added support for named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Mar 23, 2024
1 parent a3aa589 commit 84ee03a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
3 changes: 3 additions & 0 deletions docs/testdox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ Attribute Filter Chain (s9e\TextFormatter\Tests\Configurator\Collections\Attribu
[x] PHP array callbacks are normalized to an instance of AttributeFilter
[x] Default filters such as "#int" are normalized to an instance of the corresponding AttributeFilter
[x] Default filters accept positional constructor arguments
[x] Default filters accept named constructor arguments
[x] Instances of AttributeFilter are added as-is
[x] Automatically parses callback parameters

Expand Down Expand Up @@ -635,6 +636,8 @@ Filter Syntax Matcher (s9e\TextFormatter\Tests\Configurator\Helpers\FilterSyntax
[x] parse() tests with data set #33
[x] parse() tests with data set #34
[x] parse() tests with data set #35
[x] parse() tests with data set #36
[x] parse() tests with data set #37

Node Locator (s9e\TextFormatter\Tests\Configurator\Helpers\NodeLocator)
[x] getObjectParamsByRegexp() tests with data set #0
Expand Down
41 changes: 27 additions & 14 deletions src/Configurator/Helpers/FilterSyntaxMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getMatchers(): array
{
return [
'Array' => [
'groups' => ['FilterArg', 'Literal'],
'groups' => ['FilterArgValue', 'Literal'],
'regexp' => '\\[ ((?&ArrayElements))? \\]',
],
'ArrayElement' => [
Expand All @@ -29,45 +29,48 @@ public function getMatchers(): array
'regexp' => '((?&ArrayElement))(?: , ((?&ArrayElements)))?',
],
'DoubleQuotedString' => [
'groups' => ['FilterArg', 'Literal', 'Scalar'],
'groups' => ['FilterArgValue', 'Literal', 'Scalar'],
'regexp' => '"((?:[^\\\\"]|\\\\.)*)"',
],
'False' => [
'groups' => ['FilterArg', 'Literal', 'Scalar'],
'groups' => ['FilterArgValue', 'Literal', 'Scalar'],
'regexp' => '[Ff][Aa][Ll][Ss][Ee]',
],
'FilterArg' => [
'regexp' => '(?:(\\w+) : )?((?&FilterArgValue))',
],
'FilterArgs' => [
'regexp' => '((?&FilterArg))(?: , ((?&FilterArgs)))?',
],
'FilterCallback' => [
'regexp' => '([#:\\\\\\w]+)(?: \\( ((?&FilterArgs)?) \\))?',
],
'Float' => [
'groups' => ['FilterArg', 'Literal', 'Scalar'],
'groups' => ['FilterArgValue', 'Literal', 'Scalar'],
'regexp' => '([-+]?(?:\\.[0-9]+(?:_[0-9]+)*|[0-9]+(?:_[0-9]+)*\\.(?!_)[0-9]*(?:_[0-9]+)*|[0-9]+(?:_[0-9]+)*(?=[Ee]))(?:[Ee]-?[0-9]+(?:_[0-9]+)*)?)',
],
'Integer' => [
'groups' => ['FilterArg', 'Literal', 'Scalar'],
'groups' => ['FilterArgValue', 'Literal', 'Scalar'],
'regexp' => '(-?(?:0[Bb][01]+(?:_[01]+)*|0[Oo][0-7]+(?:_[0-7]+)*|0[Xx][0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*|[0-9]+(?:_[0-9]+)*))',
],
'Null' => [
'groups' => ['FilterArg', 'Literal', 'Scalar'],
'groups' => ['FilterArgValue', 'Literal', 'Scalar'],
'regexp' => '[Nn][Uu][Ll][Ll]',
],
'Param' => [
'groups' => ['FilterArg'],
'groups' => ['FilterArgValue'],
'regexp' => '\\$(\\w+(?:\\.\\w+)*)',
],
'Regexp' => [
'groups' => ['FilterArg', 'Literal'],
'groups' => ['FilterArgValue', 'Literal'],
'regexp' => '(/(?:[^\\\\/]|\\\\.)*/)([Sgimsu]*)',
],
'SingleQuotedString' => [
'groups' => ['FilterArg', 'Literal', 'Scalar'],
'groups' => ['FilterArgValue', 'Literal', 'Scalar'],
'regexp' => "'((?:[^\\\\']|\\\\.)*)'",
],
'True' => [
'groups' => ['FilterArg', 'Literal', 'Scalar'],
'groups' => ['FilterArgValue', 'Literal', 'Scalar'],
'regexp' => '[Tt][Rr][Uu][Ee]'
]
];
Expand Down Expand Up @@ -182,17 +185,27 @@ public function parseFilterCallback(string $callback, ?string $args = null): arr
return $config;
}

public function parseFilterArg(string $argName = null, string $argValue = null): array
{
$arg = (str_starts_with($argValue, '$'))
? ['Name', substr($argValue, 1) ]
: ['Value', $this->recurse($argValue, 'FilterArgValue')];
if ($argName !== '')
{
$arg[] = $argName;
}

return $arg;
}

/**
* @param string $firstArg
* @param string $otherArgs
* @return array
*/
public function parseFilterArgs(string $firstArg, ?string $otherArgs = null)
{
$parsedArg = $this->parser->parse($firstArg, 'FilterArg');

$type = ($parsedArg['match'] === 'Param') ? 'Name' : 'Value';
$args = [[$type, $parsedArg['value']]];
$args = [$this->parser->parse($firstArg, 'FilterArg')['value']];
if (isset($otherArgs))
{
$args = array_merge($args, $this->recurse($otherArgs, 'FilterArgs'));
Expand Down
20 changes: 10 additions & 10 deletions tests/Configurator/Collections/AttributeFilterChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ public function testDefaultFilterPositionalConstructorArguments()
$this->assertEquals(['min' => 1, 'max' => 12], $filter->getVars());
}

// /**
// * @testdox Default filters accept named constructor arguments
// */
// public function testDefaultFilterNamedConstructorArguments()
// {
// $filterChain = new AttributeFilterChain;
// $filter = $filterChain->append('#range(max: 12, min: 3)');
//
// $this->assertEquals(['min' => 3, 'max' => 12], $filter->getVars());
// }
/**
* @testdox Default filters accept named constructor arguments
*/
public function testDefaultFilterNamedConstructorArguments()
{
$filterChain = new AttributeFilterChain;
$filter = $filterChain->append('#range(max: 12, min: 3)');

$this->assertEquals(['min' => 3, 'max' => 12], $filter->getVars());
}

/**
* @testdox Instances of AttributeFilter are added as-is
Expand Down
17 changes: 17 additions & 0 deletions tests/Configurator/Helpers/FilterSyntaxMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ public static function getParseTests()
'params' => [['Name', 'attrValue']]
]
],
[
'strtolower(string: $attrValue)',
[
'filter' => 'strtolower',
'params' => [['Name', 'attrValue', 'string']]
]
],
[
'#range(min: 1, max: 55)',
[
'filter' => '#range',
'params' => [
['Value', 1, 'min'],
['Value', 55, 'max']
]
]
],
[
'foo\\bar($attrValue)',
[
Expand Down

0 comments on commit 84ee03a

Please sign in to comment.