Skip to content

Commit

Permalink
Merge pull request #6 from php-etl/feature/format-metric
Browse files Browse the repository at this point in the history
feature/format-metric add formatMetric function
  • Loading branch information
gplanchat authored Jul 6, 2021
2 parents 408efcd + 92077a0 commit 47db1a7
Show file tree
Hide file tree
Showing 4 changed files with 327 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/AkeneoFilterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function getFunctions()
new DateTimeZone('dateTimeZone'),
new MetricAmount('metricAmount'),
new MetricUnit('metricUnit'),
new FormatMetric('formatMetric'),
];
}
}
322 changes: 322 additions & 0 deletions src/FormatMetric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Akeneo;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;

class FormatMetric 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, $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;
}

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);
$formattedAmount = numfmt_format($fmt, $amount);
$mapping = [
'SQUARE_MILLIMETER' => 'mm²',
'SQUARE_CENTIMETER' => 'cm²',
'SQUARE_DECIMETER' => 'dm²',
'SQUARE_METER' => '',
'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' => '',
'CENTIOHM' => '',
'DECIOHM' => '',
'OHM' => 'Ω',
'DEKAOHM' => 'daΩ',
'HECTOHM' => '',
'KILOHM' => '',
'MEGOHM' => '',
'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' => '',
'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];
})(
$attribut['unit'],
$attribut['amount'],
$locale
);
}
}

4 changes: 2 additions & 2 deletions src/MetricAmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public function __construct($name)
private function compile(string $value, int $decimalRound = 4)
{
return <<<PATTERN
is_null(${value}) || !is_array(${value}) || !array_key_exists('amount', ${value}) ? null : round(floatval(${value}['amount']), ${decimalRound})
!is_array(${value}) || !array_key_exists('amount', ${value}) ? null : round(floatval(${value}['amount']), ${decimalRound})
PATTERN;
}

private function evaluate(array $context, array $value, int $decimalRound)
{
return is_null($value) || !is_array($value) || !array_key_exists('amount', $value) ? null : round(floatval($value['amount']), $decimalRound);
return !is_array($value) || !array_key_exists('amount', $value) ? null : round(floatval($value['amount']), $decimalRound);
}
}
4 changes: 2 additions & 2 deletions src/MetricUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public function __construct($name)
private function compile(string $value)
{
return <<<"PATTERN"
is_null(${value}) || !is_array(${value}) || !array_key_exists('unit', ${value}) ? null : ${value}['unit']
!is_array(${value}) || !array_key_exists('unit', ${value}) ? null : ${value}['unit']
PATTERN;
}

private function evaluate(array $context, array $value)
{
return is_null($value) || !is_array($value) || !array_key_exists('unit', $value) ? null : $value['unit'];
return !is_array($value) || !array_key_exists('unit', $value) ? null : $value['unit'];
}
}

0 comments on commit 47db1a7

Please sign in to comment.