From fb35b8bc57c6204ea5d909a7669e37ad1444f159 Mon Sep 17 00:00:00 2001 From: julien Date: Thu, 8 Jul 2021 09:45:01 +0200 Subject: [PATCH 1/4] feature/format-metric : fix FormatMetric evaluate function --- src/FormatMetric.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FormatMetric.php b/src/FormatMetric.php index d3c0e7a..07e23fb 100644 --- a/src/FormatMetric.php +++ b/src/FormatMetric.php @@ -311,7 +311,7 @@ private function evaluate(array $context, array $attribut, string $locale) throw new \RuntimeException(sprintf('Unknow Akeneo unit %s', $unit)); } - return $mapping[$unit]; + return sprintf('%s %s', $formattedAmount, $mapping[$unit]); })( $attribut['unit'], $attribut['amount'], From d0213ea7f65c5505dae31f3f93d1965e8802874e Mon Sep 17 00:00:00 2001 From: julien Date: Fri, 9 Jul 2021 12:19:37 +0200 Subject: [PATCH 2/4] feature/format-metric : change FormatMetric and ConvertMetric behaviour --- src/AkeneoFilterProvider.php | 1 + src/ConvertMetric.php | 63 ++++++++ src/FormatMetric.php | 300 ++++++++++++++++++----------------- 3 files changed, 216 insertions(+), 148 deletions(-) create mode 100644 src/ConvertMetric.php diff --git a/src/AkeneoFilterProvider.php b/src/AkeneoFilterProvider.php index b4d7b27..abf51be 100644 --- a/src/AkeneoFilterProvider.php +++ b/src/AkeneoFilterProvider.php @@ -27,6 +27,7 @@ public function getFunctions() new MetricAmount('metricAmount'), new MetricUnit('metricUnit'), new FormatMetric('formatMetric'), + new ConvertMetric('formatMetric'), ]; } } diff --git a/src/ConvertMetric.php b/src/ConvertMetric.php new file mode 100644 index 0000000..0c3c765 --- /dev/null +++ b/src/ConvertMetric.php @@ -0,0 +1,63 @@ +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 + ); + })(); + } +} diff --git a/src/FormatMetric.php b/src/FormatMetric.php index 07e23fb..325c48a 100644 --- a/src/FormatMetric.php +++ b/src/FormatMetric.php @@ -17,161 +17,165 @@ public function __construct($name) ); } - private function compile($attribut, $locale) + private function compile($attribute, $locale) { - return <<<"PATTERN" - !is_array(${attribut}) || !array_key_exists('unit', ${attribut}) || - !array_key_exists('unit', {$attribut}) ? null : (function (\$unit, \$amount,\$locale) { - \$fmt = numfmt_create(\$locale, \NumberFormatter::DECIMAL); - \$formattedAmount = numfmt_format(\$fmt, \$amount); - \$mapping = [ - 'SQUARE_MILLIMETER' => 'mm²', - 'SQUARE_CENTIMETER' => 'cm²', - 'SQUARE_DECIMETER' => 'dm²', - 'SQUARE_METER' => 'm²', - 'CENTIARE' => 'ca', - 'SQUARE_DEKAMETER' => 'dam²', - 'ARE' => 'a', - 'SQUARE_HECTOMETER' => 'hm²', - 'HECTARE' => 'ha', - 'SQUARE_KILOMETER' => 'km²', - 'SQUARE_MIL' => 'sq mil', - 'SQUARE_INCH' => 'in²', - 'SQUARE_FOOT' => 'ft²', - 'SQUARE_YARD' => 'yd²', - 'ARPENT' => 'arpent', - 'ACRE' => 'A', - 'SQUARE_FURLONG' => 'fur²', - 'SQUARE_MILE' => 'mi²', - 'BIT' => 'b', - 'BYTE' => 'B', - 'KILOBYTE' => 'kB', - 'MEGABYTE' => 'MB', - 'GIGABYTE' => 'GB', - 'TERABYTE' => 'TB', - 'DECIBEL' => 'dB', - 'HERTZ' => 'Hz', - 'KILOHERTZ' => 'kHz', - 'MEGAHERTZ' => 'MHz', - 'GIGAHERTZ' => 'GHz', - 'TERAHERTZ' => 'THz', - 'MILLIMETER' => 'mm', - 'CENTIMETER' => 'cm', - 'DECIMETER' => 'dm', - 'METER' => 'm', - 'DEKAMETER' => 'dam', - 'HECTOMETER' => 'hm', - 'KILOMETER' => 'km', - 'MIL' => 'mil', - 'INCH' => 'in', - 'FEET' => 'ft', - 'YARD' => 'yd', - 'CHAIN' => 'ch', - 'FURLONG' => 'fur', - 'MILE' => 'mi', - 'WATT' => 'W', - 'KILOWATT' => 'kW', - 'MEGAWATT' => 'MW', - 'GIGAWATT' => 'GW', - 'TERAWATT' => 'TW', - 'MILLIVOLT' => 'mV', - 'CENTIVOLT' => 'cV', - 'DECIVOLT' => 'dV', - 'VOLT' => 'V', - 'DEKAVOLT' => 'daV', - 'HECTOVOLT' => 'hV', - 'KILOVOLT' => 'kV', - 'MILLIAMPERE' => 'mA', - 'CENTIAMPERE' => 'cA', - 'DECIAMPERE' => 'dA', - 'AMPERE' => 'A', - 'DEKAMPERE' => 'daA', - 'HECTOAMPERE' => 'hA', - 'KILOAMPERE' => 'kA', - 'MILLIOHM' => 'mΩ', - 'CENTIOHM' => 'cΩ', - 'DECIOHM' => 'dΩ', - 'OHM' => 'Ω', - 'DEKAOHM' => 'daΩ', - 'HECTOHM' => 'hΩ', - 'KILOHM' => 'kΩ', - 'MEGOHM' => 'MΩ', - 'METER_PER_SECOND' => 'mdivs', - 'METER_PER_MINUTE' => 'mdivm', - 'METER_PER_HOUR' => 'mdivh', - 'KILOMETER_PER_HOUR' => 'kmdivh', - 'FOOT_PER_SECOND' => 'ftdivs', - 'FOOT_PER_HOUR' => 'ftdivh', - 'YARD_PER_HOUR' => 'yddivh', - 'MILE_PER_HOUR' => 'midivh', - 'MILLIAMPEREHOUR' => 'mAh', - 'AMPEREHOUR' => 'Ah', - 'MILLICOULOMB' => 'mC', - 'CENTIOULOMB' => 'cC', - 'DECICOULOMB' => 'dC', - 'COULOMB' => 'C', - 'DEKACOULOMB' => 'daC', - 'HECTOCOULOMB' => 'hC', - 'KILOCOULOMB' => 'kC', - 'MILLISECOND' => 'ms', - 'SECOND' => 's', - 'MINUTE' => 'm', - 'HOUR' => 'h', - 'DAY' => 'd', - 'CELSIUS' => '°C', - 'FAHRENHEIT' => '°F', - 'KELVIN' => '°K', - 'RANKINE' => '°R', - 'REAUMUR' => '°r', - 'CUBIC_MILLIMETER' => 'mm³', - 'CUBIC_CENTIMETER' => 'cm³', - 'MILLILITER' => 'ml', - 'CENTILITER' => 'cl', - 'DECILITER' => 'dl', - 'CUBIC_DECIMETER' => 'dm³', - 'LITER' => 'l', - 'CUBIC_METER' => 'm³', - 'OUNCE' => 'oz', - 'PINT' => 'pt', - 'BARREL' => 'bbl', - 'GALLON' => 'gal', - 'CUBIC_FOOT' => 'ft³', - 'CUBIC_INCH' => 'in³', - 'CUBIC_YARD' => 'yd³', - 'MILLIGRAM' => 'mg', - 'GRAM' => 'g', - 'KILOGRAM' => 'kg', - 'TON' => 't', - 'GRAIN' => 'gr', - 'DENIER' => 'denier', - 'ONCE' => 'once', - 'MARC' => 'marc', - 'LIVRE' => 'livre', - 'POUND' => 'lb', - 'BAR' => 'Bar', - 'PASCAL' => 'Pa', - 'HECTOPASCAL' => 'hPa', - 'MILLIBAR' => 'mBar', - 'ATM' => 'atm', - 'PSI' => 'PSI', - 'TORR' => 'Torr', - 'MMHG' => 'mmHg', - ]; - - if (!array_key_exists(\$unit, \$mapping)) { - throw new \RuntimeException(sprintf('Unknow Akeneo unit %s', \$unit)); - } - - return sprintf('%s %s',\$formattedAmount, \$mapping[\$unit]); - })(${attribut}['unit'],${attribut}['amount'],${locale}) -PATTERN; + return <<<"PHP" + (function() use (\$input){ + \$attribute = $attribute; + return !is_array(\$attribute) + || !array_key_exists('unit', \$attribute) + || !array_key_exists('amount', \$attribute) ? null : (function (\$unit, \$amount,\$locale) { + \$fmt = numfmt_create(\$locale, \NumberFormatter::PATTERN_DECIMAL); + \$formattedAmount = numfmt_format(\$fmt, \$amount); + \$mapping = [ + 'SQUARE_MILLIMETER' => 'mm²', + 'SQUARE_CENTIMETER' => 'cm²', + 'SQUARE_DECIMETER' => 'dm²', + 'SQUARE_METER' => 'm²', + 'CENTIARE' => 'ca', + 'SQUARE_DEKAMETER' => 'dam²', + 'ARE' => 'a', + 'SQUARE_HECTOMETER' => 'hm²', + 'HECTARE' => 'ha', + 'SQUARE_KILOMETER' => 'km²', + 'SQUARE_MIL' => 'sq mil', + 'SQUARE_INCH' => 'in²', + 'SQUARE_FOOT' => 'ft²', + 'SQUARE_YARD' => 'yd²', + 'ARPENT' => 'arpent', + 'ACRE' => 'A', + 'SQUARE_FURLONG' => 'fur²', + 'SQUARE_MILE' => 'mi²', + 'BIT' => 'b', + 'BYTE' => 'B', + 'KILOBYTE' => 'kB', + 'MEGABYTE' => 'MB', + 'GIGABYTE' => 'GB', + 'TERABYTE' => 'TB', + 'DECIBEL' => 'dB', + 'HERTZ' => 'Hz', + 'KILOHERTZ' => 'kHz', + 'MEGAHERTZ' => 'MHz', + 'GIGAHERTZ' => 'GHz', + 'TERAHERTZ' => 'THz', + 'MILLIMETER' => 'mm', + 'CENTIMETER' => 'cm', + 'DECIMETER' => 'dm', + 'METER' => 'm', + 'DEKAMETER' => 'dam', + 'HECTOMETER' => 'hm', + 'KILOMETER' => 'km', + 'MIL' => 'mil', + 'INCH' => 'in', + 'FEET' => 'ft', + 'YARD' => 'yd', + 'CHAIN' => 'ch', + 'FURLONG' => 'fur', + 'MILE' => 'mi', + 'WATT' => 'W', + 'KILOWATT' => 'kW', + 'MEGAWATT' => 'MW', + 'GIGAWATT' => 'GW', + 'TERAWATT' => 'TW', + 'MILLIVOLT' => 'mV', + 'CENTIVOLT' => 'cV', + 'DECIVOLT' => 'dV', + 'VOLT' => 'V', + 'DEKAVOLT' => 'daV', + 'HECTOVOLT' => 'hV', + 'KILOVOLT' => 'kV', + 'MILLIAMPERE' => 'mA', + 'CENTIAMPERE' => 'cA', + 'DECIAMPERE' => 'dA', + 'AMPERE' => 'A', + 'DEKAMPERE' => 'daA', + 'HECTOAMPERE' => 'hA', + 'KILOAMPERE' => 'kA', + 'MILLIOHM' => 'mΩ', + 'CENTIOHM' => 'cΩ', + 'DECIOHM' => 'dΩ', + 'OHM' => 'Ω', + 'DEKAOHM' => 'daΩ', + 'HECTOHM' => 'hΩ', + 'KILOHM' => 'kΩ', + 'MEGOHM' => 'MΩ', + 'METER_PER_SECOND' => 'mdivs', + 'METER_PER_MINUTE' => 'mdivm', + 'METER_PER_HOUR' => 'mdivh', + 'KILOMETER_PER_HOUR' => 'kmdivh', + 'FOOT_PER_SECOND' => 'ftdivs', + 'FOOT_PER_HOUR' => 'ftdivh', + 'YARD_PER_HOUR' => 'yddivh', + 'MILE_PER_HOUR' => 'midivh', + 'MILLIAMPEREHOUR' => 'mAh', + 'AMPEREHOUR' => 'Ah', + 'MILLICOULOMB' => 'mC', + 'CENTIOULOMB' => 'cC', + 'DECICOULOMB' => 'dC', + 'COULOMB' => 'C', + 'DEKACOULOMB' => 'daC', + 'HECTOCOULOMB' => 'hC', + 'KILOCOULOMB' => 'kC', + 'MILLISECOND' => 'ms', + 'SECOND' => 's', + 'MINUTE' => 'm', + 'HOUR' => 'h', + 'DAY' => 'd', + 'CELSIUS' => '°C', + 'FAHRENHEIT' => '°F', + 'KELVIN' => '°K', + 'RANKINE' => '°R', + 'REAUMUR' => '°r', + 'CUBIC_MILLIMETER' => 'mm³', + 'CUBIC_CENTIMETER' => 'cm³', + 'MILLILITER' => 'ml', + 'CENTILITER' => 'cl', + 'DECILITER' => 'dl', + 'CUBIC_DECIMETER' => 'dm³', + 'LITER' => 'l', + 'CUBIC_METER' => 'm³', + 'OUNCE' => 'oz', + 'PINT' => 'pt', + 'BARREL' => 'bbl', + 'GALLON' => 'gal', + 'CUBIC_FOOT' => 'ft³', + 'CUBIC_INCH' => 'in³', + 'CUBIC_YARD' => 'yd³', + 'MILLIGRAM' => 'mg', + 'GRAM' => 'g', + 'KILOGRAM' => 'kg', + 'TON' => 't', + 'GRAIN' => 'gr', + 'DENIER' => 'denier', + 'ONCE' => 'once', + 'MARC' => 'marc', + 'LIVRE' => 'livre', + 'POUND' => 'lb', + 'BAR' => 'Bar', + 'PASCAL' => 'Pa', + 'HECTOPASCAL' => 'hPa', + 'MILLIBAR' => 'mBar', + 'ATM' => 'atm', + 'PSI' => 'PSI', + 'TORR' => 'Torr', + 'MMHG' => 'mmHg', + ]; + + if (!array_key_exists(\$unit, \$mapping)) { + throw new \RuntimeException(sprintf('Unknow Akeneo unit %s', \$unit)); + } + + return sprintf('%s %s',\$formattedAmount, \$mapping[\$unit]); + })(${attribute}['unit'],${attribute}['amount'],$locale); + })() +PHP; } private function evaluate(array $context, array $attribut, string $locale) { return !is_array($attribut) || !array_key_exists('amount', $attribut) || !array_key_exists('unit', $attribut) ? null : (function ($unit, $amount,$locale) { - $fmt = numfmt_create($locale, \NumberFormatter::DECIMAL); + $fmt = numfmt_create($locale, \NumberFormatter::PATTERN_DECIMAL); $formattedAmount = numfmt_format($fmt, $amount); $mapping = [ 'SQUARE_MILLIMETER' => 'mm²', From f3cfb52d5b4121e3b56381697c5a90231259ee9e Mon Sep 17 00:00:00 2001 From: julien Date: Fri, 9 Jul 2021 14:44:51 +0200 Subject: [PATCH 3/4] feature/format-metric : refacto first function --- src/First.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/First.php b/src/First.php index c2b054e..54146a5 100644 --- a/src/First.php +++ b/src/First.php @@ -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); } } From cfb123d61a5401a034a6274abf5ea3e09f90a180 Mon Sep 17 00:00:00 2001 From: julien Date: Fri, 16 Jul 2021 09:06:20 +0200 Subject: [PATCH 4/4] feature/format-metric : change EL unction for convertMetric class --- src/AkeneoFilterProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AkeneoFilterProvider.php b/src/AkeneoFilterProvider.php index abf51be..b3d7716 100644 --- a/src/AkeneoFilterProvider.php +++ b/src/AkeneoFilterProvider.php @@ -27,7 +27,7 @@ public function getFunctions() new MetricAmount('metricAmount'), new MetricUnit('metricUnit'), new FormatMetric('formatMetric'), - new ConvertMetric('formatMetric'), + new ConvertMetric('convertMetric'), ]; } }