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