Skip to content

Commit

Permalink
Updated First-class Callable Syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Apr 3, 2023
1 parent e1286f6 commit 7b7bd1b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public function __construct($name)
{
parent::__construct(
$name,
\Closure::fromCallable($this->compile(...))->bindTo($this),
\Closure::fromCallable($this->evaluate(...))->bindTo($this)
$this->compile(...)->bindTo($this),
$this->evaluate(...)->bindTo($this)
);
}

private function compile(string $date, string $format, string $timezone = null)
private function compile(string $date, string $format, string $timezone = null): string
{
return <<<"PHP"
\\DateTimeImmutable::createFromFormat({$format}, {$date}, {$timezone} !== null ? new \\DateTimeZone({$timezone}) : null)
Expand Down
6 changes: 3 additions & 3 deletions src/FileName.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public function __construct($name)
{
parent::__construct(
$name,
\Closure::fromCallable($this->compile(...))->bindTo($this),
\Closure::fromCallable($this->evaluate(...))->bindTo($this)
$this->compile(...)->bindTo($this),
$this->evaluate(...)->bindTo($this)
);
}

private function compile($file)
private function compile($file): string
{
return <<<COMPILED
(!is_string({$file}) ? null : pathinfo({$file}, PATHINFO_FILENAME))
Expand Down
6 changes: 3 additions & 3 deletions src/FormatDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public function __construct($name)
{
parent::__construct(
$name,
\Closure::fromCallable($this->compile(...))->bindTo($this),
\Closure::fromCallable($this->evaluate(...))->bindTo($this)
$this->compile(...)->bindTo($this),
$this->evaluate(...)->bindTo($this)
);
}

private function compile(string $dateTime, string $format)
private function compile(string $dateTime, string $format): string
{
return <<<"PHP"
{$dateTime}->format({$format})
Expand Down
6 changes: 3 additions & 3 deletions src/Now.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public function __construct($name)
{
parent::__construct(
$name,
\Closure::fromCallable($this->compile(...))->bindTo($this),
\Closure::fromCallable($this->evaluate(...))->bindTo($this)
$this->compile(...)->bindTo($this),
$this->evaluate(...)->bindTo($this)
);
}

private function compile(string $timezone = null)
private function compile(string $timezone = null): string
{
return <<<PHP
(new \\DateTime('now', {$timezone} !== null ? new \\DateTimeZone({$timezone}) : null))
Expand Down
2 changes: 1 addition & 1 deletion src/TruncateFileName.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct($name)
);
}

private function compile(string $filename, string $length)
private function compile(string $filename, string $length): string
{
return <<<PHP
(
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/StringExpressionLanguageProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public function testFormatExpression(): void

$this->assertEquals('SKU_000001', $interpreter->evaluate('format("SKU_%06d", 1)'));
}

public function testExpression(): void
{
$interpreter = new ExpressionLanguage(null, [new StringExpressionLanguageProvider()]);

$this->assertEquals('SKU_000001', $interpreter->evaluate('fileName("SKU_000001")'));
}
}

0 comments on commit 7b7bd1b

Please sign in to comment.