-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathQueryableBlock.php
More file actions
226 lines (197 loc) · 6.83 KB
/
QueryableBlock.php
File metadata and controls
226 lines (197 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
declare(strict_types=1);
namespace HiFolks\DataType\Traits;
use HiFolks\DataType\Block;
use HiFolks\DataType\Enums\Operator;
trait QueryableBlock
{
public static function like(mixed $value1, mixed $value2): bool
{
$strValue1 = strval($value1);
$strValue2 = strval($value2);
return str_contains($strValue1, $strValue2);
}
public function exists(): bool
{
return $this->count() > 0;
}
public function where(
string|int $field,
mixed $operator = Operator::EQUAL,
mixed $value = null,
bool $preseveKeys = true,
): self {
if (func_num_args() === 1) {
$value = true;
$operator = Operator::EQUAL;
}
if (func_num_args() === 2) {
$value = $operator;
$operator = Operator::EQUAL;
}
$returnData = [];
foreach ($this as $key => $element) {
$elementToCheck = $element;
if (is_array($element)) {
$elementToCheck = Block::make(
$element,
$this->iteratorReturnsBlock,
);
}
if (!$elementToCheck instanceof Block) {
return Block::make([], $this->iteratorReturnsBlock);
}
$found = match ($operator) {
Operator::EQUAL => $elementToCheck->get($field) == $value,
Operator::GREATER_THAN => $elementToCheck->get($field) > $value,
Operator::LESS_THAN => $elementToCheck->get($field) < $value,
Operator::GREATER_THAN_OR_EQUAL => $elementToCheck->get(
$field,
) >= $value,
Operator::LESS_THAN_OR_EQUAL => $elementToCheck->get($field)
<= $value,
Operator::NOT_EQUAL => $elementToCheck->get($field) != $value,
Operator::STRICT_NOT_EQUAL => $elementToCheck->get($field)
!== $value,
Operator::IN => in_array($elementToCheck->get($field), $value),
Operator::HAS => in_array($value, $elementToCheck->get($field)),
Operator::LIKE => str_contains(
$elementToCheck->get($field),
(string) $value,
),
default => $elementToCheck->get($field) === $value,
};
if ($found) {
if ($preseveKeys) {
$returnData[$key]
= $element instanceof Block
? $element->toArray()
: $element;
} else {
$returnData[]
= $element instanceof Block
? $element->toArray()
: $element;
}
}
}
return self::make($returnData, $this->iteratorReturnsBlock);
}
public function orderBy(string|int $field, string $order = "asc"): self
{
$map = [];
$array = $this->data;
foreach ($this as $key => $item) {
$map[$key] = $item->get($field);
}
if ($order === "desc") {
array_multisort($map, SORT_DESC, $array);
} else {
array_multisort($map, $array);
}
return self::make($array, $this->iteratorReturnsBlock);
}
public function select(int|string ...$columns): self
{
$table = self::make([], $this->iteratorReturnsBlock);
foreach ($this->data as $row) {
if (is_array($row)) {
/** @var Block $row */
$row = self::make($row, $this->iteratorReturnsBlock);
}
$newRow = [];
foreach ($columns as $column) {
/** @var Block $row */
$value = $row->get($column);
$newRow[$column] = $value;
}
$table->appendItem($newRow);
}
return $table;
}
/**
* Groups the elements of the Block by a specified field.
*
* This method takes a field name as an argument and groups the elements of the
* Block object based on the values of that field. Each element is grouped into
* an associative array where the keys are the values of the specified field
* and the values are arrays of elements that share that key.
*
* @param string|int $field The field name to group by.
* @return self A new Block instance with the grouped elements.
*
*/
public function groupBy(string|int $field): self
{
$result = [];
foreach ($this as $value) {
$property = $value->get($field);
$property = self::castForArrayKey($property);
if (!$property) {
continue;
}
if (!array_key_exists(strval($property), $result)) {
$result[$property] = [];
}
$result[$property][] = $value->toArray();
}
return self::make($result);
}
public function groupByFunction(callable $groupFunction): self
{
$result = [];
foreach ($this->data as $item) {
// Call the closure to determine the group key
$groupKey = $groupFunction($item);
// Group items under the same key
if (!array_key_exists($groupKey, $result)) {
$result[$groupKey] = [];
}
$result[$groupKey][] = $item;
}
return self::make($result);
}
public function extractWhere(string $property, mixed $value): self
{
$results = [];
$scan = function ($item) use (&$results, &$scan, $property, $value): void {
if (is_array($item)) {
// Match the property/value pair
if (
array_key_exists($property, $item)
&& $item[$property] === $value
) {
$results[] = $item;
}
// Scan deeper
foreach ($item as $subItem) {
$scan($subItem);
}
}
};
$scan($this->data);
return self::make($results);
}
private static function castVariableForStrval(
mixed $property,
): bool|float|int|string|null {
return match (gettype($property)) {
"boolean" => $property,
"double" => $property,
"integer" => $property,
"string" => $property,
default => null,
};
}
private static function castForArrayKey(
mixed $property,
): bool|int|string|null {
return match (gettype($property)) {
"boolean" => $property,
"double" => strval($property),
"integer" => $property,
"string" => $property,
default => null,
};
}
}