1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Hydrogen package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace RDS\Hydrogen\Query; |
11
|
|
|
|
12
|
|
|
use RDS\Hydrogen\Criteria\HavingGroup; |
13
|
|
|
use RDS\Hydrogen\Criteria\WhereGroup; |
14
|
|
|
use RDS\Hydrogen\Criteria\GroupBy; |
15
|
|
|
use RDS\Hydrogen\Criteria\Having; |
16
|
|
|
use RDS\Hydrogen\Query; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait GroupByProvider |
20
|
|
|
* @mixin Query |
21
|
|
|
*/ |
22
|
|
|
trait GroupByProvider |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param string[] $fields |
26
|
|
|
* @return Query|$this|self |
27
|
|
|
*/ |
28
|
2 |
|
public function groupBy(string ...$fields): self |
29
|
|
|
{ |
30
|
2 |
|
foreach ($fields as $field) { |
31
|
2 |
|
$this->add(new GroupBy($this, $field)); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string|\Closure $field |
39
|
|
|
* @param $valueOrOperator |
40
|
|
|
* @param null $value |
41
|
|
|
* @return Query|$this|self |
42
|
|
|
*/ |
43
|
|
|
public function orHaving($field, $valueOrOperator = null, $value = null): self |
44
|
|
|
{ |
45
|
|
|
return $this->or->having($field, $valueOrOperator, $value); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string|\Closure $field |
50
|
|
|
* @param $valueOrOperator |
51
|
|
|
* @param null $value |
52
|
|
|
* @return Query|$this|self |
53
|
|
|
*/ |
54
|
|
|
public function having($field, $valueOrOperator = null, $value = null): self |
55
|
|
|
{ |
56
|
|
|
if (\is_string($field)) { |
57
|
|
|
[$operator, $value] = Having::completeMissingParameters($valueOrOperator, $value); |
|
|
|
|
58
|
|
|
|
59
|
|
|
return $this->add(new Having($this, $field, $operator, $value, $this->mode())); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($field instanceof \Closure) { |
63
|
|
|
return $this->add(new HavingGroup($this, $field, $this->mode())); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$error = \vsprintf('Selection set should be a type of string or Closure, but %s given', [ |
67
|
|
|
\studly_case(\gettype($field)), |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
throw new \InvalidArgumentException($error); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: