Skip to content
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

remove a static on multiple option expression #14

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/AkeneoBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function getFunctions(): array
new WithReferenceEntitySimpleOption('withReferenceEntitySimpleOption'),
new WithSimpleOption('withSimpleOption'),
new WithMultipleOption('withMultipleOption'),
new WithProductValueMediaFile('withProductValueMediaFile'),
];
}
}
4 changes: 2 additions & 2 deletions src/DateTime.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 $date, string|null $format = null): string
private function compile(string $date, ?string $format = null): string
{
if (null === $format) {
return sprintf('new \DateTimeImmutable(%s, new \DateTimeZone("UTC"))', $date);
Expand All @@ -26,7 +26,7 @@ private function compile(string $date, string|null $format = null): string
return sprintf('\DateTimeImmutable::createFromFormat(%s, %s, new \DateTimeZone("UTC"))', $date, $format);
}

private function evaluate(array $context, string $date, string|null $format = null): \DateTimeInterface
private function evaluate(array $context, string $date, ?string $format = null): \DateTimeInterface
{
if (null === $format) {
return new \DateTimeImmutable($date, new \DateTimeZone('UTC'));
Expand Down
4 changes: 2 additions & 2 deletions src/DateTimeZone.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 $date, string $zone, string|null $format = null): string
private function compile(string $date, string $zone, ?string $format = null): string
{
if (null === $format) {
return sprintf('new \DateTimeImmutable(%s, new \DateTimeZone(%s))', $date, $zone);
Expand All @@ -26,7 +26,7 @@ private function compile(string $date, string $zone, string|null $format = null)
return sprintf('\DateTimeImmutable::createFromFormat(%s, %s, new \DateTimeZone(%s))', $date, $format, $zone);
}

private function evaluate(array $context, string $date, string $zone, string|null $format = null): \DateTimeInterface
private function evaluate(array $context, string $date, string $zone, ?string $format = null): \DateTimeInterface
{
if (null === $format) {
return new \DateTimeImmutable($date, new \DateTimeZone($zone));
Expand Down
2 changes: 1 addition & 1 deletion src/FormatMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private function compile($attribute, $locale): string
PHP;
}

private function evaluate(array $context, array $attribut, string $locale): string|null
private function evaluate(array $context, array $attribut, string $locale): ?string
{
return !\is_array($attribut)
|| !\array_key_exists('amount', $attribut) || !\array_key_exists('unit', $attribut) ? null : (function ($unit, $amount, $locale) {
Expand Down
2 changes: 1 addition & 1 deletion src/MetricAmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private function compile(string $value, int $decimalRound = 4): string
PATTERN;
}

private function evaluate(array $context, array $value, int $decimalRound): float|null
private function evaluate(array $context, array $value, int $decimalRound): ?float
{
return !\is_array($value) || !\array_key_exists('amount', $value) ? null : round((float) $value['amount'], $decimalRound);
}
Expand Down
2 changes: 1 addition & 1 deletion src/MetricUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private function compile(string $value): string
PATTERN;
}

private function evaluate(array $context, array $value): string|null
private function evaluate(array $context, array $value): ?string
{
return !\is_array($value) || !\array_key_exists('unit', $value) ? null : $value['unit'];
}
Expand Down
4 changes: 2 additions & 2 deletions src/WithMultipleOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private function compile(string $codes, string $attribute, string $labels, strin
(function() use(\$input) {
\$linkedData = array_map(
function (string \$code) use(\$input) {
static \$labels = {$labels};
\$labels = {$labels};

return [
'attribute' => {$attribute},
Expand All @@ -48,7 +48,7 @@ function (string \$code) use(\$input) {
/**
* @return array<int, array<string, array|string|null>>
*/
private function evaluate(array $context, array $codes, string $attribute, array $labels, string|null $locale = null, string|null $scope = null): array
private function evaluate(array $context, array $codes, string $attribute, array $labels, ?string $locale = null, ?string $scope = null): array
{
return [[
'locale' => $locale,
Expand Down
44 changes: 44 additions & 0 deletions src/WithProductValueMediaFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Akeneo;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

final class WithProductValueMediaFile extends ExpressionFunction
{
public function __construct($name)
{
parent::__construct(
$name,
$this->compile(...)->bindTo($this),
$this->evaluate(...)->bindTo($this)
);
}

private function compile(string $code, string $attribute, string $locale = 'null', string $scope = 'null'): string
{
return <<<PHP
[
'identifier' => {$code},
'attribute' => {$attribute},
'locale' => {$locale},
'scope' => {$scope},
]
PHP;
}

/**
* @return array<string, string>
*/
private function evaluate(array $context, string $code, string $attribute, ?string $locale = null, ?string $scope = null): array
{
return [
'identifier' => $code,
'attribute' => $attribute,
'locale' => $locale,
'scope' => $scope,
];
}
}
2 changes: 1 addition & 1 deletion src/WithReferenceEntitySimpleOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private function compile(string $code, string $locale = 'null', string $channel
PHP;
}

private function evaluate(array $context, string $code, string|null $locale = null, string|null $channel = null): array
private function evaluate(array $context, string $code, ?string $locale = null, ?string $channel = null): array
{
return [[
'locale' => $locale,
Expand Down
2 changes: 1 addition & 1 deletion src/WithReferenceEntityValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private function compile(string $value, string $locale = 'null', string $channel
return sprintf('([["data" => (%s), "locale" => (%s), "channel" => (%s)]])', $value, $locale, $channel);
}

private function evaluate(array $context, string $value, string|null $locale = null, string|null $channel = null): array
private function evaluate(array $context, string $value, ?string $locale = null, ?string $channel = null): array
{
return [[
'locale' => $locale,
Expand Down
Loading