コード例 #1
0
 protected function outputNotNull($value)
 {
     if (is_numeric($value)) {
         return $value;
     }
     throw TypeConversionException::unexpectedValue($this, 'output', 'numeric value', $value);
 }
コード例 #2
0
 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);
 }
コード例 #3
0
 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) . ')';
 }
コード例 #4
0
 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) . '>';
 }
コード例 #5
0
 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) . '}';
 }
コード例 #6
0
 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) . ')';
 }
コード例 #7
0
 /**
  * Counts the number of points in input (based on number of commas)
  *
  * @param string $native
  * @return int Number of points
  * @throws TypeConversionException
  */
 protected function countPoints($native)
 {
     $commas = substr_count($native, ',');
     if ($commas % 2) {
         return intval(($commas + 1) / 2);
     } else {
         throw TypeConversionException::unexpectedValue($this, 'input', 'even number of numeric values', $native);
     }
 }
コード例 #8
0
 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);
 }
コード例 #9
0
 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;
 }
コード例 #10
0
 protected function outputNotNull($value)
 {
     if (is_object($value)) {
         $value = (array) $value;
     } elseif (!is_array($value)) {
         throw TypeConversionException::unexpectedValue($this, 'output', 'array or object', $value);
     }
     $parts = array();
     foreach ($value as $key => $item) {
         $parts[] = '"' . addcslashes($key, "\"\\") . '"' . '=>' . ($item === null ? 'NULL' : '"' . addcslashes($item, "\"\\") . '"');
     }
     return implode(', ', $parts);
 }
コード例 #11
0
 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 ? ']' : ')');
 }
コード例 #12
0
 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);
 }
コード例 #13
0
 protected function outputNotNull($value)
 {
     if (is_object($value)) {
         $value = (array) $value;
     } elseif (!is_array($value)) {
         throw TypeConversionException::unexpectedValue($this, 'output', 'array or object', $value);
     }
     $parts = array();
     foreach ($this->_items as $field => $type) {
         $v = $type->output(isset($value[$field]) ? $value[$field] : null);
         $parts[] = $v === null ? '' : '"' . strtr($v, $this->_escapes) . '"';
     }
     return '(' . join(',', $parts) . ')';
 }
コード例 #14
0
 /**
  * Calculates the number of array dimensions and required sizes for sub-arrays
  *
  * @param array $value
  * @return array
  * @throws TypeConversionException
  */
 private function _calculateRequiredSizes(array $value)
 {
     $sizes = array();
     while (is_array($value)) {
         $sizes[] = count($value);
         if (!count($value) || array_keys($value) !== range(0, count($value) - 1)) {
             if (0 === $this->_item->dimensions()) {
                 // scalar base type? "weird" sub-array is not allowed
                 throw TypeConversionException::unexpectedValue($this, 'output', 'non-empty array with 0-based numeric indexes', $value);
             }
             // assume that we reached an array representing base type
             array_pop($sizes);
             return $sizes;
         }
         if (null === ($value = $value[0])) {
             // null sub-arrays are not allowed, so that should be a base-type null
             return $sizes;
         }
     }
     if ($this->_item->dimensions() > 0) {
         // check whether we have an object representing base type
         if (is_object($value)) {
             try {
                 $this->_item->output($value);
                 return $sizes;
             } catch (PackageException $e) {
             }
         }
         array_splice($sizes, -$this->_item->dimensions());
     }
     return $sizes;
 }
コード例 #15
0
 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 ? ']' : ')');
 }
コード例 #16
0
 /**
  * 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);
 }
コード例 #17
0
 /**
  * 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);
 }