Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Where
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
12
100.00% covered (success)
100.00%
1 / 1
 where
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 orWhere
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 buildWhereSQL
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getWhereParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 resetWhere
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace BO\Zmsdb\Query\Builder;
4
5/**
6 * Where
7 *
8 * Adds in where(), orWhere() and groupings
9 * into the SQL builder.
10 *
11 *      $query->where(function(ConditionBuilder $conditions) {
12 *          $conditions
13 *              ->andWith('user', '=', 'Alex')
14 *              ->andWith('country', '=', 'GB');
15 *      })
16 *      ->orWhere(function(ConditionBuilder $conditions) {
17 *          $conditions->andWith('user', '=', 'Lucie');
18 *          $conditions->andWith('country', '=', 'CA');
19 *      });
20 *
21 * Would generate:
22 *
23 *      WHERE (name = 'Alex' AND country = 'GB')
24 *      OR (name = 'Lucie' AND country = 'CA')
25 *
26 * @package     BO\Zmsdb\Query\Builder
27 * @author      Alex Gisby<alex@solution10.com>
28 * @license     MIT
29 */
30trait Where
31{
32    /**
33     * @var     ConditionBuilder
34     */
35    protected $whereBuilder;
36
37    /**
38     * Get/Set an "AND WHERE" clause on the query. You can either pass a simple
39     * comparison ('name', '=', 'Alex') or a function to append multiple queries
40     * in a group.
41     *
42     * @param   string|\Closure|null    $field      Fieldname|callback for group|to return
43     * @param   string|null             $operator   Operator (=, !=, <>, <= etc)
44     * @param   mixed|null              $value      Value to test against
45     * @return  $this|array                         $this on set, array on get
46     */
47    public function where($field = null, $operator = null, $value = null)
48    {
49        if (!isset($this->whereBuilder)) {
50            $this->whereBuilder = new ConditionBuilder();
51        }
52        if ($field == null) {
53            return $this->whereBuilder->conditions();
54        }
55        $this->whereBuilder->andWith($field, $operator, $value);
56        return $this;
57    }
58
59    /**
60     * Adds a new 'OR ' predicate to the query.
61     *
62     * @param   string|\Closure|null    $field      Fieldname|callback for group|to return
63     * @param   string|null             $operator   Operator (=, !=, <>, <= etc)
64     * @param   mixed|null              $value      Value to test against
65     * @return  $this|array                         $this on set, array on get
66     */
67    public function orWhere($field = null, $operator = null, $value = null)
68    {
69        if (!isset($this->whereBuilder)) {
70            $this->whereBuilder = new ConditionBuilder();
71        }
72        if ($field == null) {
73            return $this->whereBuilder->conditions();
74        }
75        $this->whereBuilder->orWith($field, $operator, $value);
76        return $this;
77    }
78
79    /**
80     * Returns the SQL string for the WHERE portion of the query
81     *
82     * @param   DialectInterface    $dialect
83     * @return  string
84     */
85    public function buildWhereSQL(DialectInterface $dialect)
86    {
87        if (!isset($this->whereBuilder) || !$this->whereBuilder->hasConditions()) {
88            return '';
89        }
90
91        return 'WHERE ' . $this->whereBuilder->buildConditionSQL($dialect);
92    }
93
94    /**
95     * Returns an array of all the parameter that have been passed to where()
96     * ready to be thrown at PDO.
97     *
98     * @return  array
99     */
100    public function getWhereParams()
101    {
102        return (isset($this->whereBuilder)) ? $this->whereBuilder->getConditionParameters() : [];
103    }
104
105    /**
106     * Resets the WHERE portion of this query to empty.
107     *
108     * @return  $this
109     */
110    public function resetWhere()
111    {
112        unset($this->whereBuilder);
113        $this->whereBuilder = new ConditionBuilder();
114        return $this;
115    }
116}