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\Query; |
13
|
|
|
use RDS\Hydrogen\Collection\Collection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ExecutionsProvider |
17
|
|
|
* @mixin Query |
18
|
|
|
*/ |
19
|
|
|
trait ExecutionsProvider |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param string ...$fields |
23
|
|
|
* @return object[]|iterable |
24
|
|
|
*/ |
25
|
|
|
public function get(string ...$fields): iterable |
26
|
|
|
{ |
27
|
|
|
$processor = $this->getRepository()->getProcessor(); |
|
|
|
|
28
|
|
|
|
29
|
|
|
if (\count($fields) === 0) { |
30
|
|
|
return $processor->getResult($this); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return Collection::wrap($processor->getArrayResult($this)) |
34
|
|
|
->map(function ($item) use ($fields) { |
35
|
|
|
$result = []; |
36
|
|
|
|
37
|
|
|
foreach ($fields as $field) { |
38
|
|
|
$result[$field] = \data_get($item, $field); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $result; |
42
|
|
|
}) |
43
|
|
|
->toArray(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $field |
48
|
|
|
* @param string|null $typeOf |
49
|
|
|
* @return mixed |
50
|
|
|
* @throws \LogicException |
51
|
|
|
*/ |
52
|
|
|
public function scalar(string $field, string $typeOf = null) |
53
|
|
|
{ |
54
|
|
|
$result = \data_get($this->first($field), $field); |
55
|
|
|
|
56
|
|
|
if ($typeOf !== null) { |
57
|
|
|
return $this->cast($result, $typeOf); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param mixed $result |
65
|
|
|
* @param string $typeOf |
66
|
|
|
* @return array|\Closure|object |
67
|
|
|
*/ |
68
|
|
|
private function cast($result, string $typeOf) |
69
|
|
|
{ |
70
|
|
|
$typeOf = \strtolower($typeOf); |
71
|
|
|
|
72
|
|
|
switch ($typeOf) { |
73
|
|
|
case 'callable': |
74
|
|
|
return function (callable $applicator = null) use ($result) { |
75
|
|
|
return ($applicator ?? '\\value')($result); |
76
|
|
|
}; |
77
|
|
|
|
78
|
|
|
case 'object': |
79
|
|
|
return (object)$result; |
80
|
|
|
|
81
|
|
|
case 'array': |
82
|
|
|
case 'iterable': |
83
|
|
|
return (array)$result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$function = $typeOf . 'val'; |
87
|
|
|
|
88
|
|
|
if (! \function_exists($function)) { |
89
|
|
|
throw new \InvalidArgumentException('Could not cast to type ' . $typeOf); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $function($result); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string|null $field |
97
|
|
|
* @return int |
98
|
|
|
* @throws \LogicException |
99
|
|
|
*/ |
100
|
|
|
public function count(?string $field = 'id'): int |
101
|
|
|
{ |
102
|
|
|
return $this->select('COUNT(' . $field . ') AS __count') |
|
|
|
|
103
|
|
|
->scalar('__count', 'int'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string|null $field |
108
|
|
|
* @return int |
109
|
|
|
* @throws \LogicException |
110
|
|
|
*/ |
111
|
|
|
public function sum(string $field = null): int |
112
|
|
|
{ |
113
|
|
|
return $this->select('SUM(' . $field . ') AS __sum') |
|
|
|
|
114
|
|
|
->scalar('__sum', 'int'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string|null $field |
119
|
|
|
* @return int |
120
|
|
|
* @throws \LogicException |
121
|
|
|
*/ |
122
|
|
|
public function avg(string $field = null): int |
123
|
|
|
{ |
124
|
|
|
return $this->select('AVG(' . $field . ') AS __avg') |
|
|
|
|
125
|
|
|
->scalar('__avg', 'int'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string|null $field |
130
|
|
|
* @return int |
131
|
|
|
* @throws \LogicException |
132
|
|
|
*/ |
133
|
|
|
public function max(string $field = null): int |
134
|
|
|
{ |
135
|
|
|
return $this->select('MAX(' . $field . ') AS __max') |
|
|
|
|
136
|
|
|
->scalar('__max', 'int'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string|null $field |
141
|
|
|
* @return int |
142
|
|
|
* @throws \LogicException |
143
|
|
|
*/ |
144
|
|
|
public function min(string $field = null): int |
145
|
|
|
{ |
146
|
|
|
return $this->select('MIN(' . $field . ') AS __min') |
|
|
|
|
147
|
|
|
->scalar('__min', 'int'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string ...$fields |
152
|
|
|
* @return Collection |
153
|
|
|
*/ |
154
|
|
|
public function collect(string ...$fields): Collection |
155
|
|
|
{ |
156
|
|
|
return Collection::wrap($this->get(...$fields)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string[] $fields |
161
|
|
|
* @return object|null |
162
|
|
|
* @throws \LogicException |
163
|
|
|
*/ |
164
|
|
|
public function first(string ...$fields) |
165
|
|
|
{ |
166
|
|
|
return \array_first($this->get(...$fields)); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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.