Skip to content

Commit

Permalink
Merge pull request #7 from php-etl/feature/format-metric
Browse files Browse the repository at this point in the history
feature/format-metric : fix FormatMetric evaluate function
  • Loading branch information
gplanchat authored Aug 5, 2021
2 parents 47db1a7 + cfb123d commit 232e546
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 155 deletions.
1 change: 1 addition & 0 deletions src/AkeneoFilterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function getFunctions()
new MetricAmount('metricAmount'),
new MetricUnit('metricUnit'),
new FormatMetric('formatMetric'),
new ConvertMetric('convertMetric'),
];
}
}
63 changes: 63 additions & 0 deletions src/ConvertMetric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Akeneo;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

class ConvertMetric extends ExpressionFunction
{
public function __construct($name)
{
parent::__construct(
$name,
\Closure::fromCallable([$this, 'compile'])->bindTo($this),
\Closure::fromCallable([$this, 'evaluate'])->bindTo($this)
);
}

private function compile($attribut)
{
return <<<"PHP"
(function () use (\$input) {
\$attribut = $attribut;
return !(is_array(\$attribut)
&& array_key_exists('amount', \$attribut)
&& array_key_exists('unit', \$attribut)) ? null : (function (\$attribut) {
if (\$attribut['unit'] !== 'MILLIMETER') {
return \$attribut;
}
return [
'unit' => 'CENTIMETER',
'amount' => \$attribut['amount'] / 10
];
})(
\$attribut
);
})()
PHP;
}

private function evaluate(array $context, array $attribut)
{
return (function () use ($input) {
$attribut = $attribut;
return (is_array($attribut)
&& array_key_exists('amount', $attribut)
&& array_key_exists('unit', $attribut)) ? null : (function ($attribut) {
if ($attribut['unit'] !== 'MILLIMETER') {
return $attribut;
}

return [
'unit' => 'CENTIMETER',
'amount' => $attribut['amount'] / 10
];
})(
$attribut
);
})();
}
}
16 changes: 10 additions & 6 deletions src/First.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ public function __construct($name)
);
}

private function compile()
private function compile($array)
{
return 'function(array $input) {return array_slice($input, 0, 1, true);}';
return <<<"PHP"
(function(\$array){
return reset(\$array);
})($array)
PHP;
}

private function evaluate(array $context)
private function evaluate(array $context, $array)
{
return function (array $input) {
return array_slice($input, 0, 1, true);
};
return (function ($array) {
return reset($array);
})($array);
}
}
Loading

0 comments on commit 232e546

Please sign in to comment.