From b5fcbaf4e67175414449b9a0ea818c0d9fa7b7d2 Mon Sep 17 00:00:00 2001 From: julien Date: Mon, 5 Jul 2021 17:28:52 +0200 Subject: [PATCH 1/3] feature/format-metric add formatMetric function --- src/AkeneoFilterProvider.php | 1 + src/FormatMetric.php | 322 +++++++++++++++++++++++++++++++++++ 2 files changed, 323 insertions(+) create mode 100644 src/FormatMetric.php diff --git a/src/AkeneoFilterProvider.php b/src/AkeneoFilterProvider.php index 9372914..a96ae4e 100644 --- a/src/AkeneoFilterProvider.php +++ b/src/AkeneoFilterProvider.php @@ -26,6 +26,7 @@ public function getFunctions() new DateTimeZone('dateTimeZone'), new MetricAmount('metricAmount'), new MetricUnit('metricUnit'), + new FormatMetric('metricUnit'), ]; } } diff --git a/src/FormatMetric.php b/src/FormatMetric.php new file mode 100644 index 0000000..8b41544 --- /dev/null +++ b/src/FormatMetric.php @@ -0,0 +1,322 @@ +bindTo($this), + \Closure::fromCallable([$this, 'evaluate'])->bindTo($this) + ); + } + + private function compile($attribut, $locale) + { + return <<<"PATTERN" + is_null(${attribut}) || !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; + } + + private function evaluate(array $context, array $attribut, string $format) + { + return is_null($value) || !is_array($value) || + !array_key_exists('amount', $value) || !array_key_exists('unit', $value) ? 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 $mapping['unit']; + })( + $value['unit'], + $value['amount'], + $locale + ); + } +} + From 20b60955c4703115f2c789954177ee7a0d66286e Mon Sep 17 00:00:00 2001 From: julien Date: Tue, 6 Jul 2021 09:25:04 +0200 Subject: [PATCH 2/3] feature/format-metric fix phpstan level to 5 ( included ) --- src/FormatMetric.php | 16 ++++++++-------- src/MetricAmount.php | 4 ++-- src/MetricUnit.php | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/FormatMetric.php b/src/FormatMetric.php index 8b41544..d3c0e7a 100644 --- a/src/FormatMetric.php +++ b/src/FormatMetric.php @@ -20,7 +20,7 @@ public function __construct($name) private function compile($attribut, $locale) { return <<<"PATTERN" - is_null(${attribut}) || !is_array(${attribut}) || !array_key_exists('unit', ${attribut}) || + !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); @@ -167,11 +167,11 @@ private function compile($attribut, $locale) PATTERN; } - private function evaluate(array $context, array $attribut, string $format) + private function evaluate(array $context, array $attribut, string $locale) { - return is_null($value) || !is_array($value) || - !array_key_exists('amount', $value) || !array_key_exists('unit', $value) ? null : (function ($unit, $amount,$locale) { - $fmt = numfmt_create($locale, NumberFormatter::DECIMAL); + 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); $formattedAmount = numfmt_format($fmt, $amount); $mapping = [ 'SQUARE_MILLIMETER' => 'mm²', @@ -311,10 +311,10 @@ private function evaluate(array $context, array $attribut, string $format) throw new \RuntimeException(sprintf('Unknow Akeneo unit %s', $unit)); } - return $mapping['unit']; + return $mapping[$unit]; })( - $value['unit'], - $value['amount'], + $attribut['unit'], + $attribut['amount'], $locale ); } diff --git a/src/MetricAmount.php b/src/MetricAmount.php index c497b24..cec731e 100644 --- a/src/MetricAmount.php +++ b/src/MetricAmount.php @@ -18,12 +18,12 @@ public function __construct($name) private function compile(string $value, int $decimalRound = 4) { return << Date: Tue, 6 Jul 2021 09:56:20 +0200 Subject: [PATCH 3/3] feature/format-metric update formatMetric function --- src/AkeneoFilterProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AkeneoFilterProvider.php b/src/AkeneoFilterProvider.php index a96ae4e..b4d7b27 100644 --- a/src/AkeneoFilterProvider.php +++ b/src/AkeneoFilterProvider.php @@ -26,7 +26,7 @@ public function getFunctions() new DateTimeZone('dateTimeZone'), new MetricAmount('metricAmount'), new MetricUnit('metricUnit'), - new FormatMetric('metricUnit'), + new FormatMetric('formatMetric'), ]; } }