/** * @param string[] $typeSpecifiers * @param array $values * @return array * @throws InvalidTypeSpecifierException */ private function swapArguments(array $typeSpecifiers, array $values) : array { $swappedValues = []; $typeSpecifiers = array_values($typeSpecifiers); foreach ($typeSpecifiers as $key => $typeSpecifier) { $matches = []; $index = $key; if (1 === preg_match('/^%([0-9]+)\\$/', $typeSpecifier, $matches)) { if ('0' === $matches[1]) { throw InvalidTypeSpecifierException::invalidTypeSpecifier($typeSpecifier); } $index = $matches[1] - 1; } if (!array_key_exists($index, $values)) { throw InvalidTypeSpecifierException::unmatchedTypeSpecifier($typeSpecifier); } $swappedValues[] = $values[$index]; } return $swappedValues; }
/** * Formats the given message. * * Formats the message by the given formatters. * * @param string $message Message string containing type specifier for the values * @param mixed $values multiple values used for the message's type specifier * @throws InvalidTypeSpecifierException * @return string */ public function format(string $message, ...$values) : string { $messageMetaData = $this->messageParser->parseMessage($message, $values); $typeSpecifiers = $messageMetaData->typeSpecifiers; $values = $messageMetaData->values; $parsedMessage = $messageMetaData->parsedMessage; if (0 === count($values)) { throw InvalidTypeSpecifierException::noTypeSpecifier(); } if (count($typeSpecifiers) !== count($values)) { throw InvalidTypeSpecifierException::invalidTypeSpecifierCount(count($values), count($typeSpecifiers)); } foreach ($typeSpecifiers as $key => $typeSpecifier) { $value = array_shift($values); if (null !== ($formatter = $this->findFormatter($typeSpecifier))) { $parsedMessage[$key] = $formatter->formatValue($typeSpecifier, $value); } } return implode('', $parsedMessage); }
/** * @expectedException \Budgegeria\IntlFormat\Exception\InvalidTypeSpecifierException * @expectedExceptionMessage Value count of "1" doesn't match type specifier count of "2" */ public function testInvalidTypeSpecifierCount() { throw InvalidTypeSpecifierException::invalidTypeSpecifierCount(1, 2); }