Skip to content

Commit

Permalink
Allow to use boolean and null as filter argument
Browse files Browse the repository at this point in the history
  • Loading branch information
vidy committed Jun 10, 2022
1 parent 17e3a94 commit 6df7c4f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/BladeFilterLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class BladeFilterLexer extends AbstractLexer
public const T_COLON = 103;
public const T_COMMA = 104;
public const T_PIPE = 105;
public const T_TRUE = 106;
public const T_NULL = 107;
public const T_FALSE = 108;

/** @var array<string, int> */
protected $specials = [
'true' => self::T_TRUE,
'false' => self::T_FALSE,
'null' => self::T_NULL,
];

/**
* @inheritdoc
Expand Down Expand Up @@ -69,7 +79,11 @@ protected function getNonCatchablePatterns()
*/
protected function getType(&$value)
{

$lowerValue = strtolower($value);
if (isset($this->specials[$lowerValue])) {
return $this->specials[$lowerValue];
}

switch (true) {
/**
* Recognize numeric values
Expand Down
18 changes: 11 additions & 7 deletions src/BladeFilterParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

class BladeFilterParser
{
protected $validArgumentValueTypes = [
BladeFilterLexer::T_INTEGER,
BladeFilterLexer::T_STRING,
BladeFilterLexer::T_FLOAT,
BladeFilterLexer::T_VARIABLE_EXPRESSION,
BladeFilterLexer::T_TRUE,
BladeFilterLexer::T_FALSE,
BladeFilterLexer::T_NULL,
];

private BladeFilterLexer $lexer;

private $input;
Expand Down Expand Up @@ -148,13 +158,7 @@ private function collectFilterArgument(): array
$this->syntaxErrorIf(null === $token,
sprintf('No value specified for argument "%s"',$argumentName)
);
$isValidArgumentValue = $this->lexer->isNextTokenAny([
BladeFilterLexer::T_INTEGER,
BladeFilterLexer::T_STRING,
BladeFilterLexer::T_FLOAT,
BladeFilterLexer::T_VARIABLE_EXPRESSION,
]);

$isValidArgumentValue = $this->lexer->isNextTokenAny($this->validArgumentValueTypes);
$this->syntaxErrorIf(!$isValidArgumentValue,
sprintf(' The value of filter argument "%s" is not valid, it supposed to be string, integer, float or variable, got %s',
$argumentName,
Expand Down
14 changes: 13 additions & 1 deletion tests/BladeFilterParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,17 @@ public function test_invalid_filter_argument_name()
$parser = new BladeFilterParser();

$parser->parse($input);
}
}

/** @test */
public function test_bool_or_null_argument_value()
{
$input = '"css/carousel.css" | stylesheet_tag:media=null,preload=true';

$parser = new BladeFilterParser();

$filter = $parser->parse($input);

$this->assertEquals($filter['filters'][0]['arguments']['media'], "null");
}
}

0 comments on commit 6df7c4f

Please sign in to comment.