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 Illuminate\Support\Arr; |
13
|
|
|
use RDS\Hydrogen\Query; |
14
|
|
|
use RDS\Hydrogen\Collection\Collection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ExecutionsProvider |
18
|
|
|
* @mixin Query |
19
|
|
|
*/ |
20
|
|
|
trait ExecutionsProvider |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param string ...$fields |
24
|
|
|
* @return object[]|iterable |
25
|
|
|
*/ |
26
|
20 |
|
public function get(string ...$fields): iterable |
27
|
|
|
{ |
28
|
20 |
|
$processor = $this->getRepository()->getProcessor(); |
|
|
|
|
29
|
|
|
|
30
|
20 |
|
return $processor->getResult($this, ...$fields); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the values of a given key. |
35
|
|
|
* |
36
|
|
|
* @param string|array $value |
37
|
|
|
* @param string|null $key |
38
|
|
|
* @return Collection|iterable |
39
|
|
|
*/ |
40
|
|
|
public function pluck($value, $key = null): array |
41
|
|
|
{ |
42
|
|
|
return $this |
|
|
|
|
43
|
|
|
->collect(...\array_filter([$value, $key])) |
44
|
|
|
->pluck($value, $key) |
45
|
|
|
->toArray(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $field |
50
|
|
|
* @param string|null $typeOf |
51
|
|
|
* @return mixed |
52
|
|
|
* @throws \LogicException |
53
|
|
|
*/ |
54
|
10 |
|
public function scalar(string $field, string $typeOf = null) |
55
|
|
|
{ |
56
|
10 |
|
$processor = $this->getRepository()->getProcessor(); |
|
|
|
|
57
|
|
|
|
58
|
10 |
|
$result = $processor->getScalarResult($this, $field); |
59
|
|
|
|
60
|
10 |
|
if ($typeOf !== null) { |
61
|
10 |
|
return $this->cast($result, $typeOf); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param mixed $result |
69
|
|
|
* @param string $typeOf |
70
|
|
|
* @return array|\Closure|object|mixed |
71
|
|
|
*/ |
72
|
10 |
|
private function cast($result, string $typeOf) |
73
|
|
|
{ |
74
|
10 |
|
$typeOf = \strtolower($typeOf); |
75
|
|
|
|
76
|
|
|
switch ($typeOf) { |
77
|
10 |
|
case 'callable': |
78
|
|
|
return function (callable $applicator = null) use ($result) { |
79
|
|
|
return ($applicator ?? '\\value')($result); |
80
|
|
|
}; |
81
|
|
|
|
82
|
10 |
|
case 'object': |
83
|
|
|
return (object)$result; |
84
|
|
|
|
85
|
10 |
|
case 'array': |
86
|
10 |
|
case 'iterable': |
87
|
|
|
return (array)$result; |
88
|
|
|
|
89
|
10 |
|
case 'string': |
90
|
|
|
return (string)$result; |
91
|
|
|
} |
92
|
|
|
|
93
|
10 |
|
$function = $typeOf . 'val'; |
94
|
|
|
|
95
|
10 |
|
if (! \function_exists($function)) { |
96
|
|
|
throw new \InvalidArgumentException('Could not cast to type ' . $typeOf); |
97
|
|
|
} |
98
|
|
|
|
99
|
10 |
|
return $function($result); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string|null $field |
104
|
|
|
* @return int |
105
|
|
|
* @throws \LogicException |
106
|
|
|
*/ |
107
|
2 |
|
public function count(string $field = null): int |
108
|
|
|
{ |
109
|
2 |
|
if ($field === null) { |
110
|
|
|
$field = \array_first($this->getMetadata()->identifier); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this |
|
|
|
|
114
|
2 |
|
->select('COUNT(' . $field . ') AS __count') |
115
|
2 |
|
->scalar('__count', 'int'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string|null $field |
120
|
|
|
* @return int |
121
|
|
|
* @throws \LogicException |
122
|
|
|
*/ |
123
|
2 |
|
public function sum(string $field = null): int |
124
|
|
|
{ |
125
|
|
|
return $this |
|
|
|
|
126
|
2 |
|
->select('SUM(' . $field . ') AS __sum') |
127
|
2 |
|
->scalar('__sum', 'int'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string|null $field |
132
|
|
|
* @return int |
133
|
|
|
* @throws \LogicException |
134
|
|
|
*/ |
135
|
2 |
|
public function avg(string $field = null): int |
136
|
|
|
{ |
137
|
|
|
return $this |
|
|
|
|
138
|
2 |
|
->select('AVG(' . $field . ') AS __avg') |
139
|
2 |
|
->scalar('__avg', 'int'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string|null $field |
144
|
|
|
* @return int |
145
|
|
|
* @throws \LogicException |
146
|
|
|
*/ |
147
|
2 |
|
public function max(string $field = null): int |
148
|
|
|
{ |
149
|
|
|
return $this |
|
|
|
|
150
|
2 |
|
->select('MAX(' . $field . ') AS __max') |
151
|
2 |
|
->scalar('__max', 'int'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string|null $field |
156
|
|
|
* @return int |
157
|
|
|
* @throws \LogicException |
158
|
|
|
*/ |
159
|
2 |
|
public function min(string $field = null): int |
160
|
|
|
{ |
161
|
|
|
return $this |
|
|
|
|
162
|
2 |
|
->select('MIN(' . $field . ') AS __min') |
163
|
2 |
|
->scalar('__min', 'int'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string ...$fields |
168
|
|
|
* @return Collection |
169
|
|
|
*/ |
170
|
2 |
|
public function collect(string ...$fields): Collection |
171
|
|
|
{ |
172
|
2 |
|
return Collection::wrap($this->get(...$fields)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string[] $fields |
177
|
|
|
* @return object|null |
178
|
|
|
* @throws \LogicException |
179
|
|
|
*/ |
180
|
|
|
public function first(string ...$fields) |
181
|
|
|
{ |
182
|
|
|
return \array_first($this->get(...$fields)); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.