Skip to content

Commit

Permalink
Merge pull request #12 from php-etl/fix/builder-function
Browse files Browse the repository at this point in the history
Fixed build expression
  • Loading branch information
gplanchat authored May 25, 2023
2 parents f683348 + 3a3c76d commit 9bf38aa
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 50 deletions.
8 changes: 4 additions & 4 deletions src/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ 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 $input, string ...$values)
private function compile(string ...$values): string
{
return sprintf('array_values(array_merge(%s))', implode(', ', $values));
}

private function evaluate(array $context, array ...$values)
private function evaluate(array $context, array ...$values): array
{
return array_values(array_merge(...$values));
}
Expand Down
25 changes: 14 additions & 11 deletions src/Value.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Akeneo;

final class Value
{
public function __construct(
private mixed $value,
private readonly mixed $value,
private ?string $scope = null,
private ?string $locale = null,
) {}

) {
}

public function withScope(string $scope): self
{
$this->scope = $scope;

return $this;
}

public function withoutScope(): self
{
$this->scope = null;

return $this;
}

public function withLocale(string $locale): self
{
$this->locale = $locale;

return $this;
}

public function withoutLocale(): self
{
$this->locale = null;

return $this;
}

Expand Down
37 changes: 17 additions & 20 deletions src/WithMultipleOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@ 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 $codes, string $attribute, string $labels, string $locale, string $scope)
private function compile(string $codes, string $attribute, string $labels, string $locale, string $scope): string
{
return <<<PHP
(function() {
\$linkedData = array_map(
function (string \$code) {
\$labels = $labels;
}
static \$labels = {$labels};
return [
'attribute' => $attribute,
'attribute' => {$attribute},
'code' => \$code,
'labels' => \$labels[\$code] ?? [],
];
},
$codes,
{$codes},
);
return [
[
'data' => ($codes),
'locale' => $locale,
'scope' => $scope,
'data' => ({$codes}),
'locale' => {$locale},
'scope' => {$scope},
'linked_data' => \$linkedData,
],
];
Expand All @@ -46,23 +46,20 @@ function (string \$code) {
}

/**
* @var list<string> $codes
* @var array<string, array<string, string>> $labels
* @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)
private function evaluate(array $context, array $codes, string $attribute, array $labels, ?string $locale = null, ?string $scope = null): array
{
return [[
'locale' => $locale,
'scope' => $scope,
'data' => $codes,
'linked_data' => array_map(
function (string $code) use ($attribute, $labels) {
return [
'attribute' => $attribute,
'code' => $code,
'labels' => $labels[$code] ?? [],
];
},
fn (string $code) => [
'attribute' => $attribute,
'code' => $code,
'labels' => $labels[$code] ?? [],
],
$codes
),
]];
Expand Down
22 changes: 11 additions & 11 deletions src/WithSimpleOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ 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 $code, string $attribute, string $labels, string $locale = 'null', string $scope = 'null')
private function compile(string $code, string $attribute, string $labels, string $locale = 'null', string $scope = 'null'): string
{
return <<<PHP
([
[
'data' => ($code),
'locale' => $locale,
'scope' => $scope,
'data' => ({$code}),
'locale' => {$locale},
'scope' => {$scope},
'linked_data' => [
'attribute' => ($attribute),
'code' => ($code),
'labels' => ($labels),
'attribute' => ({$attribute}),
'code' => ({$code}),
'labels' => ({$labels}),
],
],
])
PHP;
}

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

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

0 comments on commit 9bf38aa

Please sign in to comment.