You are here

function IntegerFormatter::patternSymbols in Currency 7.2

Converts a number pattern to an array of NumberPatternSymbol objects.

Parameters

string $pattern:

Return value

array

Throws

\RuntimeException

2 calls to IntegerFormatter::patternSymbols()
DecimalFormatter::__construct in currency/vendor/bartfeenstra/cldr/src/BartFeenstra/CLDR/DecimalFormatter.php
Overrides parent::__construct().
IntegerFormatter::__construct in currency/vendor/bartfeenstra/cldr/src/BartFeenstra/CLDR/IntegerFormatter.php
Implements __construct().

File

currency/vendor/bartfeenstra/cldr/src/BartFeenstra/CLDR/IntegerFormatter.php, line 167
Contains class \BartFeenstra\CLDR\IntegerFormatter.

Class

IntegerFormatter
Formats an integer according CLDR number pattern guidelines.

Namespace

BartFeenstra\CLDR

Code

function patternSymbols($pattern) {

  // Convert the pattern to NumberPatternSymbol objects.
  $symbols = array();
  foreach ($this
    ->str_split($pattern) as $position => $symbol) {
    $symbols[] = new NumberPatternSymbol($symbol, $position);
  }

  // Loop through the NumberPatternSymbol objects and mark escaped symbols.
  foreach ($symbols as $i => $symbol) {

    // Check if the previous character is an unused escape symbol for this
    // symbol.
    if (isset($symbols[$i - 1]) && $symbols[$i - 1]->symbol == self::SYMBOL_ESCAPE && !$symbols[$i - 1]->escaped && !$symbols[$i - 1]->escapes_other_symbol && isset($symbols[$i + 1]) && $symbols[$i + 1]->symbol == self::SYMBOL_ESCAPE && !$symbols[$i + 1]->escaped && !$symbols[$i + 1]->escapes_other_symbol) {
      $symbol->escaped = TRUE;
      $symbols[$i - 1]->escapes_other_symbol = TRUE;
      $symbols[$i + 1]->escapes_other_symbol = TRUE;
    }
  }

  // Find illegal escape symbols, such as escape symbols that do not escape
  // other symbols and are not escaped themselves.
  foreach ($symbols as $symbol) {
    if ($symbol->symbol == self::SYMBOL_ESCAPE && !$symbol->escaped && !$symbol->escapes_other_symbol) {
      throw new \RunTimeException("Invalid escape symbol (') in pattern " . $pattern . "at position {$symbol->position}.");
    }
  }

  // Remove escape symbols from the array, because we have transferred their
  // meaning to other symbols' NumberPatternSymbol objects.
  foreach ($symbols as $i => $symbol) {
    if ($symbol->escapes_other_symbol) {
      unset($symbols[$i]);
    }
  }

  // Reset the array keys so they make sense again.
  return array_values($symbols);
}