We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f9ae68d commit 5645d7bCopy full SHA for 5645d7b
src/Latte/Compiler/Nodes/Php/Expression/InRangeNode.php src/Latte/Compiler/Nodes/Php/Expression/InNode.php
@@ -14,7 +14,7 @@
14
use Latte\Compiler\PrintContext;
15
16
17
-class InRangeNode extends ExpressionNode
+class InNode extends ExpressionNode
18
{
19
public function __construct(
20
public ExpressionNode $needle,
@@ -26,11 +26,11 @@ public function __construct(
26
27
public function print(PrintContext $context): string
28
29
- return 'in_array('
+ return 'LR\Filters::contains('
30
. $this->needle->print($context)
31
. ', '
32
. $this->haystack->print($context)
33
- . ', true)';
+ . ')';
34
}
35
36
src/Latte/Compiler/TagParserData.php
@@ -482,7 +482,7 @@ protected function reduce(int $rule, int $pos): void
482
124 => fn() => $this->semValue = new Expression\BinaryOpNode($this->semStack[$pos - 2], '<<', $this->semStack[$pos], $this->startTokenStack[$pos - 2]->position),
483
125 => fn() => $this->semValue = new Expression\BinaryOpNode($this->semStack[$pos - 2], '>>', $this->semStack[$pos], $this->startTokenStack[$pos - 2]->position),
484
126 => fn() => $this->semValue = new Expression\BinaryOpNode($this->semStack[$pos - 2], '**', $this->semStack[$pos], $this->startTokenStack[$pos - 2]->position),
485
- 127 => fn() => $this->semValue = new Expression\InRangeNode($this->semStack[$pos - 2], $this->semStack[$pos], $this->startTokenStack[$pos - 2]->position),
+ 127 => fn() => $this->semValue = new Expression\InNode($this->semStack[$pos - 2], $this->semStack[$pos], $this->startTokenStack[$pos - 2]->position),
486
128 => fn() => $this->semValue = new Expression\UnaryOpNode($this->semStack[$pos], '+', $this->startTokenStack[$pos - 1]->position),
487
129 => fn() => $this->semValue = new Expression\UnaryOpNode($this->semStack[$pos], '-', $this->startTokenStack[$pos - 1]->position),
488
130, 131 => fn() => $this->semValue = new Expression\NotNode($this->semStack[$pos], $this->startTokenStack[$pos - 1]->position),
src/Latte/Runtime/Filters.php
@@ -174,6 +174,17 @@ public static function escapeHtmlRawText($s): string
174
175
176
177
+ /**
178
+ * Determine if a string or array contains a given needle.
179
+ */
180
+ public static function contains(mixed $needle, array|string $haystack): bool
181
+ {
182
+ return is_array($haystack)
183
+ ? in_array($needle, $haystack, true)
184
+ : str_contains($haystack, (string) $needle);
185
+ }
186
+
187
188
/**
189
* Converts ... to ...
190
*/
tests/common/TagParser.parseArguments().phpt
@@ -94,10 +94,10 @@ test('inline modifiers', function () {
94
95
96
test('in operator', function () {
97
- Assert::same("in_array(\$a, ['a', 'b'], true), 1", formatArgs('$a in [a, b], 1'));
98
- Assert::same('$a, in_array($b->func(), [1, 2], true)', formatArgs('$a, $b->func() in [1, 2]'));
99
- Assert::same('$a, in_array($b[1], [1, 2], true)', formatArgs('$a, $b[1] in [1, 2]'));
100
- Assert::same('in_array($b, [1, [2], 3], true)', formatArgs('$b in [1, [2], 3]'));
+ Assert::same("LR\\Filters::contains(\$a, ['a', 'b']), 1", formatArgs('$a in [a, b], 1'));
+ Assert::same('$a, LR\Filters::contains($b->func(), [1, 2])', formatArgs('$a, $b->func() in [1, 2]'));
+ Assert::same('$a, LR\Filters::contains($b[1], [1, 2])', formatArgs('$a, $b[1] in [1, 2]'));
+ Assert::same('LR\Filters::contains($b, [1, [2], 3])', formatArgs('$b in [1, [2], 3]'));
101
});
102
103
tests/filters/contains.phpt
@@ -0,0 +1,26 @@
1
+<?php
2
3
+/**
4
+ * Test: Latte\Runtime\Filters::contains
5
6
7
+declare(strict_types=1);
8
9
+use Latte\Runtime\Filters;
10
+use Tester\Assert;
11
12
+require __DIR__ . '/../bootstrap.php';
13
+Assert::false(Filters::contains(null, []));
+Assert::true(Filters::contains(null, [null]));
+Assert::false(Filters::contains(1, ['1']));
+Assert::true(Filters::contains(1, [1]));
+Assert::true(Filters::contains('', ''));
21
+Assert::true(Filters::contains('', 'abcd'));
22
+Assert::false(Filters::contains('bc', ''));
23
+Assert::true(Filters::contains('bc', 'abcd'));
24
+Assert::true(Filters::contains(null, ''));
25
+Assert::true(Filters::contains(1, '123'));
+Assert::false(Filters::contains(1, '23'));
tests/phpParser/in.phpt
@@ -27,7 +27,7 @@ __halt_compiler();
Latte\Compiler\Nodes\Php\Expression\ArrayNode
items: array (3)
| 0 => Latte\Compiler\Nodes\Php\Expression\ArrayItemNode
- | | value: Latte\Compiler\Nodes\Php\Expression\InRangeNode
+ | | value: Latte\Compiler\Nodes\Php\Expression\InNode
| | | needle: Latte\Compiler\Nodes\Php\Expression\VariableNode
| | | | name: 'a'
| | | | position: 1:1 (offset 0)
@@ -41,7 +41,7 @@ Latte\Compiler\Nodes\Php\Expression\ArrayNode
41
| | position: 1:1 (offset 0)
42
| 1 => Latte\Compiler\Nodes\Php\Expression\ArrayItemNode
43
| | value: Latte\Compiler\Nodes\Php\Expression\BinaryOpNode
44
- | | | left: Latte\Compiler\Nodes\Php\Expression\InRangeNode
+ | | | left: Latte\Compiler\Nodes\Php\Expression\InNode
45
| | | | needle: Latte\Compiler\Nodes\Php\Expression\VariableNode
46
| | | | | name: 'a'
47
| | | | | position: 4:1 (offset 28)
@@ -50,7 +50,7 @@ Latte\Compiler\Nodes\Php\Expression\ArrayNode
50
| | | | | position: 4:7 (offset 34)
51
| | | | position: 4:1 (offset 28)
52
| | | operator: '||'
53
- | | | right: Latte\Compiler\Nodes\Php\Expression\InRangeNode
+ | | | right: Latte\Compiler\Nodes\Php\Expression\InNode
54
55
| | | | | name: 'c'
56
| | | | | position: 4:13 (offset 40)
@@ -69,7 +69,7 @@ Latte\Compiler\Nodes\Php\Expression\ArrayNode
69
70
| | | | position: 5:1 (offset 50)
71
| | | expr: Latte\Compiler\Nodes\Php\Expression\NotNode
72
- | | | | expr: Latte\Compiler\Nodes\Php\Expression\InRangeNode
+ | | | | expr: Latte\Compiler\Nodes\Php\Expression\InNode
73
| | | | | needle: Latte\Compiler\Nodes\Php\Expression\BinaryOpNode
74
| | | | | | left: Latte\Compiler\Nodes\Php\Scalar\IntegerNode
75
| | | | | | | value: 10
tests/phpPrint/operators.phpt
@@ -154,7 +154,7 @@ $a xor $b,
154
$a or $b,
155
$a instanceof Foo,
156
$a instanceof $b,
157
-in_array($a, $b, true),
158
-in_array(!$a, $b, true) && !in_array($a + 2, $b, true),
+LR\Filters::contains($a, $b),
+LR\Filters::contains(!$a, $b) && !LR\Filters::contains($a + 2, $b),
159
!$a,
160
!($a > $b) && !($c == !$d)
0 commit comments