Lines
95.45%
21 / 22
Functions and Methods
83.33%
5 / 6
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Where | 95.45% | 21 / 22 | 83.33% | 5 / 6 | 14 | 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 | 66.66% | 2 / 3 | 0.00% | 0 / 1 | 2.15 | |||||
| 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\Zmsbackend\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\Zmsbackend\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 | * @psalm-api | |
| 67 | */ | |
| 68 | public function orWhere($field = null, $operator = null, $value = null) | |
| 69 | { | |
| 70 | if (!isset($this->whereBuilder)) { | |
| 71 | $this->whereBuilder = new ConditionBuilder(); | |
| 72 | } | |
| 73 | if ($field == null) { | |
| 74 | return $this->whereBuilder->conditions(); | |
| 75 | } | |
| 76 | $this->whereBuilder->orWith($field, $operator, $value); | |
| 77 | return $this; | |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * @psalm-api | |
| 82 | */ | |
| 83 | public function whereIn(string $field, array $values): static|array | |
| 84 | { | |
| 85 | if (empty($values)) { | |
| 86 | throw new \InvalidArgumentException('whereIn() requires a non-empty $values array.'); | |
| 87 | } | |
| 88 | return $this->where($field, 'IN', array_values($values)); | |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * Returns the SQL string for the WHERE portion of the query | |
| 93 | * | |
| 94 | * @param DialectInterface $dialect | |
| 95 | * @return string | |
| 96 | */ | |
| 97 | public function buildWhereSQL(DialectInterface $dialect) | |
| 98 | { | |
| 99 | if (!isset($this->whereBuilder) || !$this->whereBuilder->hasConditions()) { | |
| 100 | return ''; | |
| 101 | } | |
| 102 | ||
| 103 | return 'WHERE ' . $this->whereBuilder->buildConditionSQL($dialect); | |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Returns an array of all the parameter that have been passed to where() | |
| 108 | * ready to be thrown at PDO. | |
| 109 | * | |
| 110 | * @return array | |
| 111 | */ | |
| 112 | public function getWhereParams() | |
| 113 | { | |
| 114 | return (isset($this->whereBuilder)) ? $this->whereBuilder->getConditionParameters() : []; | |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Resets the WHERE portion of this query to empty. | |
| 119 | * | |
| 120 | * @return $this | |
| 121 | */ | |
| 122 | public function resetWhere() | |
| 123 | { | |
| 124 | unset($this->whereBuilder); | |
| 125 | $this->whereBuilder = new ConditionBuilder(); | |
| 126 | return $this; | |
| 127 | } | |
| 128 | } |