protected function outputNotNull($value) { if (is_numeric($value)) { return $value; } throw TypeConversionException::unexpectedValue($this, 'output', 'numeric value', $value); }
/** * Parses given number of points from native database value * * @param string $native native database value * @param int $pos position * @param int $count number of points to parse * @param bool $allowSquare whether square brackets [] are allowed around points * @return array * @throws TypeConversionException */ protected function parsePoints($native, &$pos, $count, $allowSquare = false) { $hasDelimiters = $squareDelimiter = false; $char = $this->nextChar($native, $pos); if ('[' === $char) { if (!$allowSquare) { throw TypeConversionException::parsingFailed($this, "'(' or numeric value", $native, $pos); } $hasDelimiters = $squareDelimiter = true; $pos++; } elseif ('(' === $char) { $nextPos = $pos + 1; if ($pos === call_user_func(self::$strrpos, $native, '(') || '(' === $this->nextChar($native, $nextPos)) { $hasDelimiters = true; $pos++; } } $points = array(); for ($i = 0; $i < $count; $i++) { if ($i > 0) { $this->expectChar($native, $pos, ','); } $points[] = $this->point->parseInput($native, $pos); } if ($hasDelimiters) { $this->expectChar($native, $pos, $squareDelimiter ? ']' : ')'); } if ($allowSquare) { $points['open'] = $squareDelimiter; } return $points; }
protected function inputNotNull($native) { foreach ($this->getFormats(self::DEFAULT_STYLE) as $format) { if ($value = \DateTime::createFromFormat('!' . $format, $native)) { return $value; } } throw TypeConversionException::unexpectedValue($this, 'input', 'time string without time zone info', $native); }
protected function outputNotNull($value) { if (is_array($value)) { $value = Box::createFromArray($value); } elseif (!$value instanceof Box) { throw TypeConversionException::unexpectedValue($this, 'output', 'instance of Box or an array', $value); } return '(' . $this->point->output($value->start) . ',' . $this->point->output($value->end) . ')'; }
protected function outputNotNull($value) { if (is_array($value)) { $value = Circle::createFromArray($value); } elseif (!$value instanceof Circle) { throw TypeConversionException::unexpectedValue($this, 'output', 'instance of Circle or an array', $value); } return '<' . $this->point->output($value->center) . ',' . $this->_float->output($value->radius) . '>'; }
protected function outputNotNull($value) { if (is_array($value)) { $value = Point::createFromArray($value); } elseif (!$value instanceof Point) { throw TypeConversionException::unexpectedValue($this, 'output', 'instance of Point or an array', $value); } return '(' . $this->_float->output($value->x) . ',' . $this->_float->output($value->y) . ')'; }
protected function outputNotNull($value) { if (is_array($value)) { $value = Line::createFromArray($value); } elseif (!$value instanceof Line) { throw TypeConversionException::unexpectedValue($this, 'output', 'instance of Line or an array', $value); } return '{' . $this->_float->output($value->A) . ',' . $this->_float->output($value->B) . ',' . $this->_float->output($value->C) . '}'; }
protected function outputNotNull($value) { if (is_array($value)) { $value = Tid::createFromArray($value); } elseif (!$value instanceof Tid) { throw TypeConversionException::unexpectedValue($this, 'output', 'instance of Tid or an array', $value); } /* @var $value Tid */ return sprintf('(%d,%d)', $value->block, $value->tuple); }
protected function outputNotNull($value) { if (is_float($value) && is_nan($value)) { return 'NaN'; } $value = str_replace(',', '.', $value); if (!is_numeric($value)) { throw TypeConversionException::unexpectedValue($this, 'output', 'numeric value', $value); } return $value; }
protected function outputNotNull($value) { if (is_array($value)) { $value = Path::createFromArray($value); } elseif (!$value instanceof Path) { throw TypeConversionException::unexpectedValue($this, 'output', 'instance of Path or an array', $value); } $points = array(); foreach ($value as $point) { $points[] = $this->point->output($point); } return ($value->open ? '[' : '(') . implode(',', $points) . ($value->open ? ']' : ')'); }
protected function getFormats($style) { list($output, $order) = array_map('trim', explode(',', $style)); if (0 === strcasecmp('ISO', $output)) { return array('Y-m-d H:i:s.u', 'Y-m-d H:i:s'); } elseif (0 === strcasecmp('Postgres', $output)) { return 0 === strcasecmp('DMY', $order) ? array('* d M H:i:s.u Y', '* d M H:i:s Y') : array('* M d H:i:s.u Y', '* M d H:i:s Y'); } elseif (0 === strcasecmp('SQL', $output)) { return 0 === strcasecmp('DMY', $order) ? array('d/m/Y H:i:s.u', 'd/m/Y H:i:s') : array('m/d/Y H:i:s.u', 'm/d/Y H:i:s'); } elseif (0 === strcasecmp('German', $output)) { return array('d.m.Y H:i:s.u', 'd.m.Y H:i:s'); } throw TypeConversionException::unexpectedValue($this, 'input', 'valid DateStyle setting', $style); }
protected function parseInput($native, &$pos) { $result = array(); while (false !== ($char = $this->nextChar($native, $pos))) { $key = $this->_readString($native, $pos, '=', false); $this->expectChar($native, $pos, '='); // don't use expectChar as there can be no whitespace if ('>' !== $native[$pos]) { throw TypeConversionException::parsingFailed($this, "'=>'", $native, $pos - 1); } $pos++; // skip possible whitespace before value $this->nextChar($native, $pos); $result[$key] = $this->_readString($native, $pos, ',', true); // skip one comma after the pair if (',' === $this->nextChar($native, $pos)) { $pos++; } } return $result; }
/** * Converts PHP variable not identical to null into native format * * Note: a passed string will be returned as-is without any attempts to parse it. * PostgreSQL's interval parser accepts a lot more possible formats than this * class can handle. * * @param string|number|\DateInterval $value * @return string * @throws TypeConversionException */ protected function outputNotNull($value) { if (is_string($value)) { return $value; } elseif (is_int($value)) { return sprintf('%d seconds', $value); } elseif (is_float($value)) { return rtrim(sprintf('%.6f', $value), '.0') . ' seconds'; } elseif ($value instanceof \DateInterval) { return DateInterval::formatAsISO8601($value); } throw TypeConversionException::unexpectedValue($this, 'output', 'a string, a number or an instance of DateInterval', $value); }
/** * Throws an Exception if next non-whitespace character in input is not the given char * * @param string $string * @param int $pos * @param string $char * @throws TypeConversionException */ protected function expectChar($string, &$pos, $char) { if ($char !== $this->nextChar($string, $pos)) { throw TypeConversionException::parsingFailed($this, "'{$char}'", $string, $pos); } $pos++; }
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; }
protected function parseInput($native, &$pos) { reset($this->_items); $result = array(); $unescape = array_flip($this->_escapes); $this->expectChar($native, $pos, '('); // Leading "(" while (true) { /* @var $type TypeConverter */ if (!(list($field, $type) = each($this->_items))) { // Check if we have more fields left. throw TypeConversionException::parsingFailed($this, 'end of input: no more fields left', $native, $pos); } switch ($char = $this->nextChar($native, $pos)) { case ',': case ')': // Comma or end of row instead of value: treat as NULL. $result[$field] = null; break; case '"': // Quoted string. if (!preg_match('/"((?>[^"]+|"")*)"/As', $native, $m, 0, $pos)) { throw TypeConversionException::parsingFailed($this, 'quoted string', $native, $pos); } $result[$field] = $type->input(strtr($m[1], $unescape)); $pos += call_user_func(self::$strlen, $m[0]); $char = $this->nextChar($native, $pos); break; default: // Unquoted string. $len = strcspn($native, ',)', $pos); $result[$field] = $type->input(call_user_func(self::$substr, $native, $pos, $len)); $pos += $len; $char = $this->nextChar($native, $pos); break; } switch ($char) { // Expect delimiter after value case ')': $pos++; break 2; case ',': $pos++; break; default: throw TypeConversionException::parsingFailed($this, "',' or ')'", $native, $pos); } } if (list($field, ) = each($this->_items)) { // point error at preceding ')' throw TypeConversionException::parsingFailed($this, "value for '{$field}'", $native, $pos - 1); } return $result; }
/** * Converts PHP variable not identical to null into native format * * Note: a passed string will be returned as-is without any attempts to parse it. * PostgreSQL's interval parser accepts a lot more possible formats than this * class can handle. Integer will be handled using date() with an appropriate * format specification. * * @param string|integer|\DateTime $value * @return string * @throws TypeConversionException */ protected function outputNotNull($value) { if (is_string($value)) { return $value; } elseif (is_int($value)) { $formats = $this->getFormats(self::DEFAULT_STYLE); return date($formats[0], $value); } elseif ($value instanceof \DateTime) { $formats = $this->getFormats(self::DEFAULT_STYLE); return $value->format($formats[0]); } throw TypeConversionException::unexpectedValue($this, 'output', 'a string, an integer or an instance of DateTime', $value); }
protected function outputNotNull($value) { if (is_array($value)) { $value = call_user_func(array($this->resultClass, 'createFromArray'), $value); } elseif (!$value instanceof Range) { throw TypeConversionException::unexpectedValue($this, 'output', 'instance of Range or an array', $value); } /* @var $value Range */ if ($value->empty) { return 'empty'; } return ($value->lowerInclusive ? '[' : '(') . (null === $value->lower ? '' : '"' . addcslashes($this->_subtype->output($value->lower), "\"\\") . '"') . ',' . (null === $value->upper ? '' : '"' . addcslashes($this->_subtype->output($value->upper), "\"\\") . '"') . ($value->upperInclusive ? ']' : ')'); }