Skip to content

Commit 6df7c4f

Browse files
author
vidy
committed
Allow to use boolean and null as filter argument
1 parent 17e3a94 commit 6df7c4f

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/BladeFilterLexer.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ class BladeFilterLexer extends AbstractLexer
2626
public const T_COLON = 103;
2727
public const T_COMMA = 104;
2828
public const T_PIPE = 105;
29+
public const T_TRUE = 106;
30+
public const T_NULL = 107;
31+
public const T_FALSE = 108;
32+
33+
/** @var array<string, int> */
34+
protected $specials = [
35+
'true' => self::T_TRUE,
36+
'false' => self::T_FALSE,
37+
'null' => self::T_NULL,
38+
];
2939

3040
/**
3141
* @inheritdoc
@@ -69,7 +79,11 @@ protected function getNonCatchablePatterns()
6979
*/
7080
protected function getType(&$value)
7181
{
72-
82+
$lowerValue = strtolower($value);
83+
if (isset($this->specials[$lowerValue])) {
84+
return $this->specials[$lowerValue];
85+
}
86+
7387
switch (true) {
7488
/**
7589
* Recognize numeric values

src/BladeFilterParser.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
class BladeFilterParser
88
{
9+
protected $validArgumentValueTypes = [
10+
BladeFilterLexer::T_INTEGER,
11+
BladeFilterLexer::T_STRING,
12+
BladeFilterLexer::T_FLOAT,
13+
BladeFilterLexer::T_VARIABLE_EXPRESSION,
14+
BladeFilterLexer::T_TRUE,
15+
BladeFilterLexer::T_FALSE,
16+
BladeFilterLexer::T_NULL,
17+
];
18+
919
private BladeFilterLexer $lexer;
1020

1121
private $input;
@@ -148,13 +158,7 @@ private function collectFilterArgument(): array
148158
$this->syntaxErrorIf(null === $token,
149159
sprintf('No value specified for argument "%s"',$argumentName)
150160
);
151-
$isValidArgumentValue = $this->lexer->isNextTokenAny([
152-
BladeFilterLexer::T_INTEGER,
153-
BladeFilterLexer::T_STRING,
154-
BladeFilterLexer::T_FLOAT,
155-
BladeFilterLexer::T_VARIABLE_EXPRESSION,
156-
]);
157-
161+
$isValidArgumentValue = $this->lexer->isNextTokenAny($this->validArgumentValueTypes);
158162
$this->syntaxErrorIf(!$isValidArgumentValue,
159163
sprintf(' The value of filter argument "%s" is not valid, it supposed to be string, integer, float or variable, got %s',
160164
$argumentName,

tests/BladeFilterParserTest.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,17 @@ public function test_invalid_filter_argument_name()
5757
$parser = new BladeFilterParser();
5858

5959
$parser->parse($input);
60-
}
60+
}
61+
62+
/** @test */
63+
public function test_bool_or_null_argument_value()
64+
{
65+
$input = '"css/carousel.css" | stylesheet_tag:media=null,preload=true';
66+
67+
$parser = new BladeFilterParser();
68+
69+
$filter = $parser->parse($input);
70+
71+
$this->assertEquals($filter['filters'][0]['arguments']['media'], "null");
72+
}
6173
}

0 commit comments

Comments
 (0)