/** * Create where clause * * @param Where|\Closure|string|array|Predicate\PredicateInterface $predicate * @param string $combination One of the OP_* constants from Predicate\PredicateSet * @throws Exception\InvalidArgumentException * @return $this */ public function where($predicate, $combination = Predicate\PredicateSet::OP_AND) { if ($predicate instanceof Where) { $this->where = $predicate; } elseif ($predicate instanceof Predicate\PredicateInterface) { $this->where->addPredicate($predicate, $combination); } elseif ($predicate instanceof \Closure) { $predicate($this->where); } else { if (is_string($predicate)) { // String $predicate should be passed as an expression $predicate = strpos($predicate, Expression::PLACEHOLDER) !== false ? new Predicate\Expression($predicate) : new Predicate\Literal($predicate); $this->where->addPredicate($predicate, $combination); } elseif (is_array($predicate)) { foreach ($predicate as $pkey => $pvalue) { if (is_string($pkey)) { if (strpos($pkey, '?') !== false) { $predicate = new Predicate\Expression($pkey, $pvalue); } elseif ($pvalue === null) { // map PHP null to SQL IS NULL expression $predicate = new Predicate\IsNull($pkey, $pvalue); } elseif (is_array($pvalue) || $pvalue instanceof self) { // if the value is an array, assume IN() is desired $predicate = new Predicate\In($pkey, $pvalue); } elseif ($pvalue instanceof Predicate\PredicateInterface) { throw new Exception\InvalidArgumentException('Using Predicate must not use string keys'); } else { // otherwise assume that array('foo' => 'bar') means "foo" = 'bar' $predicate = new Predicate\Operator($pkey, $pvalue, Predicate\Operator::OP_EQ); } } elseif ($pvalue instanceof Predicate\PredicateInterface) { // Predicate type is ok $predicate = $pvalue; } else { // must be an array of expressions (with int-indexed array) $predicate = strpos($pvalue, Expression::PLACEHOLDER) !== false ? new Predicate\Expression($pvalue) : new Predicate\Literal($pvalue); } $this->where->addPredicate($predicate, $combination); } } } return $this; }
/** * Create where clause * * @param Where|\Closure|string|array $predicate * @param string $combination One of the OP_* constants from Predicate\PredicateSet * @return $this */ public function where($predicate, $combination = Predicate\PredicateSet::OP_AND) { if ($predicate instanceof Where) { $this->where = $predicate; } elseif ($predicate instanceof \Closure) { $predicate($this->where); } else { if (is_string($predicate)) { // String $predicate should be passed as an expression $predicate = new Predicate\Expression($predicate); $this->where->addPredicate($predicate, $combination); } elseif (is_array($predicate)) { foreach ($predicate as $pkey => $pvalue) { if (is_string($pkey)) { // Otherwise, if still a string, do something intelligent with the PHP type provided if (strpos($pkey, '?') !== false) { $predicate = new Predicate\Expression($pkey, $pvalue); } elseif ($pvalue === null) { // map PHP null to SQL IS NULL expression $predicate = new Predicate\IsNull($pkey, $pvalue); } elseif (is_array($pvalue)) { // if the value is an array, assume IN() is desired $predicate = new Predicate\In($pkey, $pvalue); } else { // otherwise assume that array('foo' => 'bar') means "foo" = 'bar' $predicate = new Predicate\Operator($pkey, $pvalue, Predicate\Operator::OP_EQ); } } elseif ($pvalue instanceof Predicate\PredicateInterface) { // Predicate type is ok $predicate = $pvalue; } else { // must be an array of expressions (with int-indexed array) $predicate = new Predicate\Expression($pvalue); } $this->where->addPredicate($predicate, $combination); } } } return $this; }