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