From d8711f6c7c412e2514640eac354f7fa361283cc0 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 7 Nov 2024 16:52:35 +0100 Subject: [PATCH 1/5] add preg_match expression --- src/StringExpressionLanguageProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/StringExpressionLanguageProvider.php b/src/StringExpressionLanguageProvider.php index b0c768b..18b621b 100644 --- a/src/StringExpressionLanguageProvider.php +++ b/src/StringExpressionLanguageProvider.php @@ -24,6 +24,7 @@ public function getFunctions(): array ExpressionFunction::fromPhp('strip_tags', 'stripHtml'), ExpressionFunction::fromPhp('json_decode', 'decode'), ExpressionFunction::fromPhp('preg_replace', 'replaceByExpression'), + ExpressionFunction::fromPhp('preg_match', 'matchExpression'), new CapitalizeWords('capitalizeWords'), ExpressionFunction::fromPhp('rtrim', 'removeWhitespaces'), ExpressionFunction::fromPhp('explode', 'splitIntoArray'), From d1af03385d22602e1f93abdc28106c682cb7e84f Mon Sep 17 00:00:00 2001 From: Jonathan Date: Tue, 12 Nov 2024 11:00:29 +0100 Subject: [PATCH 2/5] replace preg_match by a custom expression --- src/StringExpressionLanguageProvider.php | 2 +- src/TestPattern.php | 37 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/TestPattern.php diff --git a/src/StringExpressionLanguageProvider.php b/src/StringExpressionLanguageProvider.php index 18b621b..61c5f03 100644 --- a/src/StringExpressionLanguageProvider.php +++ b/src/StringExpressionLanguageProvider.php @@ -24,7 +24,7 @@ public function getFunctions(): array ExpressionFunction::fromPhp('strip_tags', 'stripHtml'), ExpressionFunction::fromPhp('json_decode', 'decode'), ExpressionFunction::fromPhp('preg_replace', 'replaceByExpression'), - ExpressionFunction::fromPhp('preg_match', 'matchExpression'), + new TestPattern('testPattern'), new CapitalizeWords('capitalizeWords'), ExpressionFunction::fromPhp('rtrim', 'removeWhitespaces'), ExpressionFunction::fromPhp('explode', 'splitIntoArray'), diff --git a/src/TestPattern.php b/src/TestPattern.php new file mode 100644 index 0000000..461c82e --- /dev/null +++ b/src/TestPattern.php @@ -0,0 +1,37 @@ +compile(...)->bindTo($this), + $this->evaluate(...)->bindTo($this) + ); + } + + private function compile(string $pattern, string $subject): bool + { + return << Date: Tue, 12 Nov 2024 10:01:05 +0000 Subject: [PATCH 3/5] [rector] Rector fixes --- src/TestPattern.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/TestPattern.php b/src/TestPattern.php index 461c82e..5475781 100644 --- a/src/TestPattern.php +++ b/src/TestPattern.php @@ -6,11 +6,6 @@ class TestPattern { public function __construct($name) { - parent::__construct( - $name, - $this->compile(...)->bindTo($this), - $this->evaluate(...)->bindTo($this) - ); } private function compile(string $pattern, string $subject): bool From 87075c604201f3eefd86ee4d41007670cf4227db Mon Sep 17 00:00:00 2001 From: Jonathan Date: Tue, 12 Nov 2024 11:08:24 +0100 Subject: [PATCH 4/5] cs-fixer --- src/DateTime.php | 2 +- src/Now.php | 4 ++-- src/TestPattern.php | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/DateTime.php b/src/DateTime.php index bf0f94a..c35d449 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -24,7 +24,7 @@ private function compile(string $date, string $format, string $timezone = 'null' PHP; } - private function evaluate(array $context, string $date, string $format, string $timezone = null) + private function evaluate(array $context, string $date, string $format, ?string $timezone = null) { return \DateTimeImmutable::createFromFormat($format, $date, null !== $timezone ? new \DateTimeZone($timezone) : null); } diff --git a/src/Now.php b/src/Now.php index a4d4b15..02951f3 100644 --- a/src/Now.php +++ b/src/Now.php @@ -17,14 +17,14 @@ public function __construct($name) ); } - private function compile(string $timezone = null): string + private function compile(?string $timezone = null): string { return << Date: Tue, 12 Nov 2024 11:10:56 +0100 Subject: [PATCH 5/5] replace preg_match by a custom expression --- src/TestPattern.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/TestPattern.php b/src/TestPattern.php index f496ac4..5e48f83 100644 --- a/src/TestPattern.php +++ b/src/TestPattern.php @@ -4,13 +4,20 @@ namespace Kiboko\Component\StringExpressionLanguage; -class TestPattern +use Symfony\Component\ExpressionLanguage\ExpressionFunction; + +class TestPattern extends ExpressionFunction { public function __construct($name) { + parent::__construct( + $name, + $this->compile(...)->bindTo($this), + $this->evaluate(...)->bindTo($this) + ); } - private function compile(string $pattern, string $subject): bool + private function compile(string $pattern, string $subject): string { return <<