Skip to content

add preg_match expression #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Now.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public function __construct($name)
);
}

private function compile(string $timezone = null): string
private function compile(?string $timezone = null): string
{
return <<<PHP
(new \\DateTime('now', {$timezone} !== null ? new \\DateTimeZone({$timezone}) : null))
PHP;
}

private function evaluate(array $context, string $timezone = null)
private function evaluate(array $context, ?string $timezone = null)
{
return new \DateTime('now', null !== $timezone ? new \DateTimeZone($timezone) : null);
}
Expand Down
1 change: 1 addition & 0 deletions src/StringExpressionLanguageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function getFunctions(): array
ExpressionFunction::fromPhp('strip_tags', 'stripHtml'),
ExpressionFunction::fromPhp('json_decode', 'decode'),
ExpressionFunction::fromPhp('preg_replace', 'replaceByExpression'),
new TestPattern('testPattern'),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On pourrait pas nommer la fonction isMatchingPattern ?
Je trouve ça plus explicite que le testPattern

new CapitalizeWords('capitalizeWords'),
ExpressionFunction::fromPhp('rtrim', 'removeWhitespaces'),
ExpressionFunction::fromPhp('explode', 'splitIntoArray'),
Expand Down
41 changes: 41 additions & 0 deletions src/TestPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\StringExpressionLanguage;

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): string
{
return <<<PHP
\$result = preg_match({$pattern}, {$subject});
if (!\$result) {
return false;
} else {
return true;
}
PHP;
}

private function evaluate(array $context, string $pattern, string $subject): bool
{
$result = preg_match($pattern, $subject);
if (!$result) {
return false;
}

return true;
}
}
Loading