Skip to content

Commit

Permalink
Merge pull request #13 from php-etl/feature/reference-entities
Browse files Browse the repository at this point in the history
Created new functions to manage reference entities
  • Loading branch information
gplanchat authored Jun 23, 2023
2 parents 9bf38aa + 7f79a66 commit 6507cb3
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan-5.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan level 5
on: push
jobs:
phpstan:
phpstan5:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan-6.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan level 6
on: push
jobs:
phpstan:
phpstan6:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan-7.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan level 7
on: push
jobs:
phpstan:
phpstan7:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan-8.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan level 8
on: push
jobs:
phpstan:
phpstan8:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 2 additions & 0 deletions src/AkeneoBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function getFunctions(): array
return [
new Build('build'),
new WithValue('withValue'),
new WithReferenceEntityValue('withReferenceEntityValue'),
new WithReferenceEntitySimpleOption('withReferenceEntitySimpleOption'),
new WithSimpleOption('withSimpleOption'),
new WithMultipleOption('withMultipleOption'),
];
Expand Down
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 $format = null): string
private function compile(string $date, string|null $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 $format = null): string
return sprintf('\DateTimeImmutable::createFromFormat(%s, %s, new \DateTimeZone("UTC"))', $date, $format);
}

private function evaluate(array $context, string $date, ?string $format = null): \DateTimeInterface
private function evaluate(array $context, string $date, string|null $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 $format = null): string
private function compile(string $date, string $zone, string|null $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 $format = null): st
return sprintf('\DateTimeImmutable::createFromFormat(%s, %s, new \DateTimeZone(%s))', $date, $format, $zone);
}

private function evaluate(array $context, string $date, string $zone, ?string $format = null): \DateTimeInterface
private function evaluate(array $context, string $date, string $zone, string|null $format = null): \DateTimeInterface
{
if (null === $format) {
return new \DateTimeImmutable($date, new \DateTimeZone($zone));
Expand Down
10 changes: 5 additions & 5 deletions src/WithMultipleOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public function __construct($name)
);
}

private function compile(string $codes, string $attribute, string $labels, string $locale, string $scope): string
private function compile(string $codes, string $attribute, string $labels, string $locale = 'null', string $scope = 'null'): string
{
return <<<PHP
(function() {
(function() use(\$input) {
\$linkedData = array_map(
function (string \$code) {
function (string \$code) use(\$input) {
static \$labels = {$labels};
return [
Expand All @@ -41,14 +41,14 @@ function (string \$code) {
'linked_data' => \$linkedData,
],
];
)()
})()
PHP;
}

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

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Akeneo;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

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

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

private function evaluate(array $context, string $code, string|null $locale = null, string|null $channel = null): array
{
return [[
'locale' => $locale,
'channel' => $channel,
'data' => [
$code,
],
]];
}
}
33 changes: 33 additions & 0 deletions src/WithReferenceEntityValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Akeneo;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

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

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

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

0 comments on commit 6507cb3

Please sign in to comment.