/** * @dataProvider getValuesFrom */ public function testCastFrom($native, $value) { if ($value instanceof \Exception) { $this->setExpectedException(get_class($value)); $this->converter->input($native); } else { $this->assertEquals($value, $this->converter->input($native)); } }
/** * Parses a native value into PHP variable from given position * * @param string $native * @param int $pos * * @return Range * @throws TypeConversionException */ protected function parseInput($native, &$pos) { $char = $this->nextChar($native, $pos); if (('e' === $char || 'E' === $char) && preg_match('/empty/Ai', $native, $m, 0, $pos)) { $pos += 5; return call_user_func(array($this->resultClass, 'createEmpty')); } if ('(' === $char || '[' === $char) { $pos++; $lowerInclusive = '[' === $char; } else { throw TypeConversionException::parsingFailed($this, '[ or (', $native, $pos); } $lower = $this->_readRangeBound($native, $pos, ',)]'); $this->expectChar($native, $pos, ','); $upper = $this->_readRangeBound($native, $pos, ',])'); if (']' === $native[$pos]) { $upperInclusive = true; $pos++; } elseif (')' === $native[$pos]) { $upperInclusive = false; $pos++; } else { throw TypeConversionException::parsingFailed($this, '] or )', $native, $pos); } return new $this->resultClass($this->_subtype->input($lower), $this->_subtype->input($upper), $lowerInclusive, $upperInclusive); }
protected function parseInput($native, &$pos) { $result = array(); $this->expectChar($native, $pos, '{'); // Leading "{". while ('}' !== ($char = $this->nextChar($native, $pos))) { // require a comma delimiter between elements if (!empty($result)) { if (',' !== $char) { throw TypeConversionException::parsingFailed($this, "','", $native, $pos); } $pos++; $char = $this->nextChar($native, $pos); } if ('{' === $char) { // parse sub-array $result[] = $this->parseInput($native, $pos); } elseif ('"' === $char) { // quoted string if (!preg_match('/"((?>[^"\\\\]+|\\\\.)*)"/As', $native, $m, 0, $pos)) { throw TypeConversionException::parsingFailed($this, 'quoted string', $native, $pos); } $result[] = $this->_item->input(stripcslashes($m[1])); $pos += call_user_func(self::$strlen, $m[0]); } else { // zero-length string can appear only quoted if (0 === ($len = strcspn($native, ",} \t\r\n", $pos))) { throw TypeConversionException::parsingFailed($this, 'subarray, quoted or unquoted string', $native, $pos); } $v = call_user_func(self::$substr, $native, $pos, $len); $result[] = strcasecmp($v, "null") ? $this->_item->input(stripcslashes($v)) : null; $pos += $len; } } $pos++; // skip trailing "}" return $result; }