Skip to content

Commit

Permalink
Update CurrencyInput.php
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMichalica authored Jun 11, 2020
1 parent 26d209d commit 85280c6
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/Forms/Controls/CurrencyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ class CurrencyInput extends TextInput
self::CURRENCY_GBP => '£',
];

public static $codes = [
self::CURRENCY_CZK => 'CZK',
self::CURRENCY_EUR => 'EUR',
self::CURRENCY_USD => 'USD',
self::CURRENCY_GBP => 'GBP',
];

protected static $formats = [
self::LANGUAGE_CS => [
'digitGroupSeparator' => ' ',
'decimalCharacter' => ',',
'decimalCharacterAlternative' => '.',
'currencyExpression' => 'symbol',
'currencySymbolPlacement' => 's',
'currencySymbolSeparator' => ' ',
'decimalPlaces' => 0,
Expand All @@ -43,13 +51,15 @@ class CurrencyInput extends TextInput
self::LANGUAGE_EN => [
'digitGroupSeparator' => ',',
'decimalCharacter' => '.',
'currencyExpression' => 'symbol',
'currencySymbolPlacement' => 'p',
'currencySymbolSeparator' => '',
],
self::LANGUAGE_SK => [
'digitGroupSeparator' => ' ',
'decimalCharacter' => ',',
'decimalCharacterAlternative' => '.',
'currencyExpression' => 'symbol',
'currencySymbolPlacement' => 's',
'currencySymbolSeparator' => ' ',
],
Expand All @@ -74,7 +84,17 @@ public static function register()
public function getFormat()
{
$options = static::$formats[static::$defaultLanguage];
$options['currencySymbol'] = static::$symbols[$this->getOption('currency') ?? static::$defaultCurrency];

switch ($options['currencyExpression']) {
case 'symbol':
$options['currencySymbol'] = static::$symbols[$this->getOption('currency') ?? static::$defaultCurrency];
break;
case 'code':
$options['currencySymbol'] = static::$codes[$this->getOption('currency') ?? static::$defaultCurrency];
break;
default:
$options['currencySymbol'] = '';
}

if ($options['currencySymbol'] !== '') {
// suffix
Expand Down Expand Up @@ -135,12 +155,22 @@ public static function setFormat(string $language, array $options)
{
$options = array_merge(static::$formats[$language], $options);

if ($options['currencySymbol'] !== '') {
if ($options['currencySymbolPlacement'] !== 's' || $options['currencySymbolPlacement'] === 'p') {
throw new \InvalidArgumentException("Option 'currencySymbolPlacement' must be either 's' or 'p'.");
foreach ($options as $key => $value) {
if ($value === null) {
throw new \InvalidArgumentException("Option '$key' must not be null.");
}
}

bd ($options);

if ($options['currencyExpression'] !== false && $options['currencyExpression'] !== 'symbol' && $options['currencyExpression'] !== 'code') {
throw new \InvalidArgumentException("Option 'currencyExpression' must be either 'symbol', 'code' or false.");
}

if ($options['currencySymbolPlacement'] !== 's' && $options['currencySymbolPlacement'] !== 'p') {
throw new \InvalidArgumentException("Option 'currencySymbolPlacement' must be either 's' or 'p'.");
}

static::$formats[$language] = $options;
}
}

0 comments on commit 85280c6

Please sign in to comment.