3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\Sql\Predicate
;
12 use Zend\Db\Sql\Exception
;
13 use Zend\Db\Sql\Select
;
15 class In
implements PredicateInterface
17 protected $identifier;
20 protected $selectSpecification = '%s IN %s';
21 protected $valueSpecSpecification = '%%s IN (%s)';
26 * @param null|string $identifier
27 * @param array $valueSet
29 public function __construct($identifier = null, $valueSet = null)
32 $this->setIdentifier($identifier);
35 $this->setValueSet($valueSet);
40 * Set identifier for comparison
42 * @param string $identifier
45 public function setIdentifier($identifier)
47 $this->identifier
= $identifier;
52 * Get identifier of comparison
56 public function getIdentifier()
58 return $this->identifier
;
62 * Set set of values for IN comparison
64 * @param array $valueSet
65 * @throws Exception\InvalidArgumentException
68 public function setValueSet($valueSet)
70 if (!is_array($valueSet) && !$valueSet instanceof Select
) {
71 throw new Exception\
InvalidArgumentException(
72 '$valueSet must be either an array or a Zend\Db\Sql\Select object, ' . gettype($valueSet) . ' given'
75 $this->valueSet
= $valueSet;
79 public function getValueSet()
81 return $this->valueSet
;
85 * Return array of parts for where statement
89 public function getExpressionData()
91 $values = $this->getValueSet();
92 if ($values instanceof Select
) {
93 $specification = $this->selectSpecification
;
94 $types = array(self
::TYPE_VALUE
);
95 $values = array($values);
97 $specification = sprintf($this->valueSpecSpecification
, implode(', ', array_fill(0, count($values), '%s')));
98 $types = array_fill(0, count($values), self
::TYPE_VALUE
);
101 $identifier = $this->getIdentifier();
102 array_unshift($values, $identifier);
103 array_unshift($types, self
::TYPE_IDENTIFIER
);