Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.36% |
19 / 22 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Where | |
86.36% |
19 / 22 |
|
83.33% |
5 / 6 |
14.50 | |
0.00% |
0 / 1 |
| where | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| orWhere | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| whereIn | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| buildWhereSQL | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| getWhereParams | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| resetWhere | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace 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 | */ |
| 30 | trait 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 | public function whereIn(string $field, array $values) |
| 80 | { |
| 81 | if (empty($values)) { |
| 82 | throw new \InvalidArgumentException('whereIn() requires a non-empty $values array.'); |
| 83 | } |
| 84 | return $this->where($field, 'IN', array_values($values)); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns the SQL string for the WHERE portion of the query |
| 89 | * |
| 90 | * @param DialectInterface $dialect |
| 91 | * @return string |
| 92 | */ |
| 93 | public function buildWhereSQL(DialectInterface $dialect) |
| 94 | { |
| 95 | if (!isset($this->whereBuilder) || !$this->whereBuilder->hasConditions()) { |
| 96 | return ''; |
| 97 | } |
| 98 | |
| 99 | return 'WHERE ' . $this->whereBuilder->buildConditionSQL($dialect); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns an array of all the parameter that have been passed to where() |
| 104 | * ready to be thrown at PDO. |
| 105 | * |
| 106 | * @return array |
| 107 | */ |
| 108 | public function getWhereParams() |
| 109 | { |
| 110 | return (isset($this->whereBuilder)) ? $this->whereBuilder->getConditionParameters() : []; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Resets the WHERE portion of this query to empty. |
| 115 | * |
| 116 | * @return $this |
| 117 | */ |
| 118 | public function resetWhere() |
| 119 | { |
| 120 | unset($this->whereBuilder); |
| 121 | $this->whereBuilder = new ConditionBuilder(); |
| 122 | return $this; |
| 123 | } |
| 124 | } |