Skip to content

Commit d1af033

Browse files
committed
replace preg_match by a custom expression
1 parent d8711f6 commit d1af033

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/StringExpressionLanguageProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getFunctions(): array
2424
ExpressionFunction::fromPhp('strip_tags', 'stripHtml'),
2525
ExpressionFunction::fromPhp('json_decode', 'decode'),
2626
ExpressionFunction::fromPhp('preg_replace', 'replaceByExpression'),
27-
ExpressionFunction::fromPhp('preg_match', 'matchExpression'),
27+
new TestPattern('testPattern'),
2828
new CapitalizeWords('capitalizeWords'),
2929
ExpressionFunction::fromPhp('rtrim', 'removeWhitespaces'),
3030
ExpressionFunction::fromPhp('explode', 'splitIntoArray'),

src/TestPattern.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Kiboko\Component\StringExpressionLanguage;
4+
5+
class TestPattern
6+
{
7+
public function __construct($name)
8+
{
9+
parent::__construct(
10+
$name,
11+
$this->compile(...)->bindTo($this),
12+
$this->evaluate(...)->bindTo($this)
13+
);
14+
}
15+
16+
private function compile(string $pattern, string $subject): bool
17+
{
18+
return <<<PHP
19+
\$result = preg_match({$pattern}, {$subject});
20+
if (!\$result) {
21+
return false;
22+
} else {
23+
return true;
24+
}
25+
PHP;
26+
}
27+
28+
private function evaluate(array $context, string $pattern, string $subject): bool
29+
{
30+
$result = preg_match($pattern, $subject);
31+
if (!$result) {
32+
return false;
33+
} else {
34+
return true;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)