src/Service/CurrencyService.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\model\DayPrice;
  4. use App\Entity\Price;
  5. use Swap\Swap;
  6. class CurrencyService {
  7.     private Swap $swap;
  8.     public function __construct(Swap $swap) {
  9.         $this->swap $swap;
  10.     }
  11.     /**
  12.      * @param Price|DayPrice $p
  13.      * @param string $toCurrency
  14.      */
  15.     public function updateCurrency($pstring $toCurrency) {
  16.         if($p->getCurrency() === $toCurrency) return;
  17.         $rate $this->getRate($p->getCurrency(), $toCurrency);
  18.         $p->setCurrency($toCurrency);
  19.         $p->setCents(intval($rate $p->getCents()));
  20.     }
  21.     public function updateCurrencyInfo($centsstring $fromCurrencystring $toCurrency) {
  22.         $rate $this->getRate($fromCurrency$toCurrency);
  23.         return intval($rate $cents);
  24.     }
  25.     /**
  26.      * Because of the APIs used, we must use the EUR or USD as a base for conversions
  27.      * TODO must be improved (for free usage) as it fetches a list of pair currencies but cache only one (the one asked)
  28.      */
  29.     public function getRate(string $fromstring $to): float {
  30.         $rate 1;
  31.         if($from === $to) {
  32.             $rate 1;
  33.         } else if ($from === 'EUR' || $from === 'USD') {
  34.             $rate $this->swap->latest("$from/$to")->getValue();
  35.         } else if ($to === 'EUR' || $to === 'USD') {
  36.             $rate $this->swap->latest("$to/$from")->getValue();
  37.         } else { //FROM -> EUR -> TO
  38.             $rateToEur $this->swap->latest("EUR/$from")->getValue();
  39.             $rateFromEur $this->swap->latest("EUR/$to")->getValue();
  40.             $rate $rateFromEur $rateToEur;
  41.         }
  42.         return $rate;
  43.     }
  44.     /**
  45.      * Returns the price to display
  46.      * TODO handle before/after ( $pad = $fmt->getAttribute(\NumberFormatter::PAD_AFTER_PREFIX);)
  47.      * in Google flight if currency of country use symbol, otherwise if has same symbol that other (eg "$") use currency iso code
  48.      */
  49.     public static function nicePrice(int $centsstring $currency, ?string $locale null): string {
  50.         if($cents 2000// if higher than 20, remove cents
  51.             $cents ceil($cents/100)*100;
  52.         $amount self::niceCents($cents); //1050 => "10.50" / 1000 => "10"
  53.         switch ($currency) {
  54.             case 'GBP' : return '£'.$amount;
  55.             case 'EUR' : return $amount.'€';
  56.             case 'MAD' : return $amount.' dh';
  57.             case 'CZK' : return $amount.' Kč';
  58.             case 'PLN' : return $amount.' zł';
  59.             case 'RUB' : return $amount.' ₽';
  60.             case 'UAH' : return $amount.' ₴';
  61.             case 'INR' : return '₹'.$amount;
  62.             case 'TRY' : return $amount.'₺';
  63.             case 'USD' : return '$'.$amount;
  64.             case 'BGN' : return 'Лв'.$amount;
  65.             case 'VND' : return $amount.'₫';
  66.             case 'THB' : return '฿'.$amount;
  67.             case 'HUF' : return $amount.'Ft';
  68.             default: return $locale self::nicePriceExt($cents$currency$locale) : $currency.' '.$amount;
  69.         }
  70.     }
  71.     /**
  72.      * Generates the amount part
  73.      * 550 => 5.50
  74.      * 500 => 5
  75.      */
  76.     public static function niceCents(int $cents): string {
  77.         if($cents 100 == 0)
  78.             return $cents/100;
  79.         else
  80.             return number_format($cents/1002);
  81.     }
  82.     /**
  83.      * Generates the price to display using the ext-intl
  84.      * //Intl::getCurrencyBundle()->getCurrencySymbol('INR');
  85.      */
  86.     public static function nicePriceExt(int $centsstring $currency, ?string $locale): string {
  87.         $amount $cents/100;
  88.         $fmt = new \NumberFormatter$locale, \NumberFormatter::CURRENCY );
  89.         $fmt->setTextAttribute(\NumberFormatter::CURRENCY_CODE$currency);
  90.         if($cents 100 == 0)
  91.             $fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS0); //do not display cents if ".00"
  92.         return $fmt->format($amount);
  93.     }
  94.     public static function countryCurrency(string $countryCode) : string {
  95.         // use swich case as number formatter method could be slow
  96.         switch ($countryCode) {
  97.             case 'GB': return 'GBP';
  98.             case 'US': return 'USD';
  99.             case 'FR': return 'EUR';
  100.             case 'ES': return 'EUR';
  101.             case 'IT': return 'EUR';
  102.         }
  103.         $fmt = new \NumberFormatter'_'.$countryCode, \NumberFormatter::CURRENCY );
  104.         return $fmt->getTextAttribute(\NumberFormatter::CURRENCY_CODE);
  105.     }
  106.     public static function countryLang(string $countryCode): string {
  107.         return "";
  108.     }
  109. }