/** * Splits a quantity string into its syntactic parts. * * @see newQuantityFromParts * * @param string $value * * @throws InvalidArgumentException If $value is not a string * @throws ParseException If $value does not match the expected pattern * @return array list( $amount, $exactness, $margin, $unit ). * Parts not present in $value will be null */ private function splitQuantityString($value) { if (!is_string($value)) { throw new InvalidArgumentException('$value must be a string'); } //TODO: allow explicitly specifying the number of significant figures //TODO: allow explicitly specifying the uncertainty interval $numberPattern = $this->unlocalizer->getNumberRegex('@'); $unitPattern = $this->unlocalizer->getUnitRegex('@'); $pattern = '@^' . '\\s*(' . $numberPattern . ')' . '\\s*(?:' . '([~!])' . '|(?:\\+/?-|±)\\s*(' . $numberPattern . ')' . '|' . ')' . '\\s*(' . $unitPattern . ')?' . '\\s*$@u'; if (!preg_match($pattern, $value, $groups)) { throw new ParseException('Malformed quantity', $value, self::FORMAT_NAME); } // Remove $0. array_shift($groups); array_walk($groups, function (&$element) { if ($element === '') { $element = null; } }); return array_pad($groups, 4, null); }
/** * Splits a quantity string into its syntactic parts. * * @see newQuantityFromParts * * @param string $value * * @throws InvalidArgumentException If $value is not a string * @throws ParseException If $value does not match the expected pattern * @return array list( $amount, $exactness, $margin, $unit ). * Parts not present in $value will be null */ private function splitQuantityString($value) { if (!is_string($value)) { throw new InvalidArgumentException('$value must be a string'); } //TODO: allow explicitly specifying the number of significant figures //TODO: allow explicitly specifying the uncertainty interval $numberPattern = $this->unlocalizer->getNumberRegex('@'); $unitPattern = $this->unlocalizer->getUnitRegex('@'); $pattern = '@^' . '\\s*(' . $numberPattern . ')' . '\\s*(?:' . '([~!])' . '|(?:\\+/?-|±)\\s*(' . $numberPattern . ')' . '|' . ')' . '\\s*(' . $unitPattern . ')?' . '\\s*$@u'; if (!preg_match($pattern, $value, $groups)) { throw new ParseException('Malformed quantity', $value, self::FORMAT_NAME); } for ($i = 1; $i <= 4; $i++) { if (!isset($groups[$i])) { $groups[$i] = null; } elseif ($groups[$i] === '') { $groups[$i] = null; } } array_shift($groups); // remove $groups[0] return $groups; }