Source of file Expression.php
Size: 3,944 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Db/Sql/Expression.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Db\Sql; class Expression implements ExpressionInterface { /** * @const */ const PLACEHOLDER = '?'; /** * @var string */ protected $expression = ''; /** * @var array */ protected $parameters = array(); /** * @var array */ protected $types = array(); /** * @param string $expression * @param string|array $parameters * @param array $types */ public function __construct($expression = '', $parameters = null, array $types = array()) { if ($expression) { $this->setExpression($expression); } if ($parameters) { $this->setParameters($parameters); } if ($types) { $this->setTypes($types); } } /** * @param $expression * @return Expression * @throws Exception\InvalidArgumentException */ public function setExpression($expression) { if (!is_string($expression) || $expression == '') { throw new Exception\InvalidArgumentException('Supplied expression must be a string.'); } $this->expression = $expression; return $this; } /** * @return string */ public function getExpression() { return $this->expression; } /** * @param $parameters * @return Expression * @throws Exception\InvalidArgumentException */ public function setParameters($parameters) { if (!is_scalar($parameters) && !is_array($parameters)) { throw new Exception\InvalidArgumentException('Expression parameters must be a scalar or array.'); } $this->parameters = $parameters; return $this; } /** * @return array */ public function getParameters() { return $this->parameters; } /** * @param array $types * @return Expression */ public function setTypes(array $types) { $this->types = $types; return $this; } /** * @return array */ public function getTypes() { return $this->types; } /** * @return array * @throws Exception\RuntimeException */ public function getExpressionData() { $parameters = (is_scalar($this->parameters)) ? array($this->parameters) : $this->parameters; $types = array(); $parametersCount = count($parameters); if ($parametersCount == 0 && strpos($this->expression, self::PLACEHOLDER) !== false) { // if there are no parameters, but there is a placeholder $parametersCount = substr_count($this->expression, self::PLACEHOLDER); $parameters = array_fill(0, $parametersCount, null); } for ($i = 0; $i < $parametersCount; $i++) { $types[$i] = (isset($this->types[$i]) && ($this->types[$i] == self::TYPE_IDENTIFIER || $this->types[$i] == self::TYPE_LITERAL)) ? $this->types[$i] : self::TYPE_VALUE; } // assign locally, escaping % signs $expression = str_replace('%', '%%', $this->expression); if ($parametersCount > 0) { $count = 0; $expression = str_replace(self::PLACEHOLDER, '%s', $expression, $count); if ($count !== $parametersCount) { throw new Exception\RuntimeException('The number of replacements in the expression does not match the number of parameters'); } } return array(array( $expression, $parameters, $types )); } } |