Repository navigation
Expand file tree
/
Copy pathTaskList.php
More file actions
267 lines (226 loc) · 6.76 KB
/
Copy pathTaskList.php
File metadata and controls
267 lines (226 loc) · 6.76 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
declare(strict_types=1);
namespace SchedulerBundle\Task;
use ArrayIterator;
use Closure;
use SchedulerBundle\Exception\InvalidArgumentException;
use SchedulerBundle\Exception\RuntimeException;
use Throwable;
use Traversable;
use function array_chunk;
use function array_filter;
use function array_key_exists;
use function array_key_last;
use function array_values;
use function array_walk;
use function array_map;
use function uasort;
use function count;
use function gettype;
use function in_array;
use function is_string;
use function sprintf;
use const ARRAY_FILTER_USE_BOTH;
/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class TaskList implements TaskListInterface
{
/**
* @var TaskInterface[]
*/
private array $tasks = [];
/**
* @param TaskInterface[] $tasks
*/
public function __construct(array $tasks = [])
{
foreach ($tasks as $task) {
$this->add(task: $task);
}
}
/**
* {@inheritdoc}
*/
public function add(TaskInterface ...$task): void
{
if ([] === $task) {
return;
}
array_walk(array: $task, callback: function (TaskInterface $task): void {
$this->tasks[$task->getName()] = $task;
});
}
/**
* {@inheritdoc}
*/
public function has(string $taskName): bool
{
return array_key_exists(key: $taskName, array: $this->tasks);
}
/**
* {@inheritdoc}
*/
public function get(string $taskName, bool $lazy = false): TaskInterface|LazyTask
{
if ($lazy) {
return new LazyTask(name: $taskName, sourceTaskClosure: Closure::bind(closure: fn (): TaskInterface => $this->get(taskName: $taskName), newThis: $this));
}
$task = $this->tasks[$taskName] ?? null;
if (!$task instanceof TaskInterface) {
throw new InvalidArgumentException(message: sprintf('The task "%s" does not exist or is invalid', $taskName));
}
return $task;
}
/**
* {@inheritdoc}
*/
public function findByName(array $names): TaskListInterface|LazyTaskList
{
$filteredTasks = $this->filter(filter: static fn (TaskInterface $task): bool => in_array(needle: $task->getName(), haystack: $names, strict: true));
return new TaskList(tasks: $filteredTasks->toArray());
}
/**
* {@inheritdoc}
*/
public function filter(Closure $filter): TaskListInterface|LazyTaskList
{
return new TaskList(tasks: array_filter(array: $this->tasks, callback: $filter, mode: ARRAY_FILTER_USE_BOTH));
}
/**
* {@inheritdoc}
*/
public function remove(string $taskName): void
{
if (!$this->has(taskName: $taskName)) {
return;
}
unset($this->tasks[$taskName]);
}
/**
* {@inheritdoc}
*/
public function walk(Closure $func): TaskListInterface|LazyTaskList
{
array_walk(array: $this->tasks, callback: $func);
return $this;
}
/**
* {@inheritdoc}
*/
public function map(Closure $func, bool $keepKeys = true): array
{
$results = array_map(callback: $func, array: $this->tasks);
return $keepKeys ? $results : array_values(array: $results);
}
/**
* {@inheritdoc}
*/
public function last(): TaskInterface
{
$lastIndex = array_key_last(array: $this->tasks);
if (null === $lastIndex) {
throw new RuntimeException(message: 'The current list is empty');
}
return $this->tasks[$lastIndex];
}
/**
* {@inheritdoc}
*/
public function uasort(Closure $func): TaskListInterface|LazyTaskList
{
uasort(array: $this->tasks, callback: $func);
return $this;
}
/**
* {@inheritdoc}
*/
public function chunk(int $size, bool $preserveKeys = false): array
{
try {
$chunks = array_chunk(array: $this->tasks, length: $size, preserve_keys: $preserveKeys);
} catch (Throwable) {
throw new InvalidArgumentException(message: sprintf('The given size "%d" cannot be used to split the list', $size));
}
return $chunks;
}
/**
* {@inheritdoc}
*/
public function slice(string ...$tasks): TaskListInterface|LazyTaskList
{
$toRetrieveTasks = $this->findByName(names: $tasks);
if (0 === $toRetrieveTasks->count()) {
throw new RuntimeException(message:'The tasks cannot be found');
}
return $toRetrieveTasks->walk(func: function (TaskInterface $task): void {
$this->remove(taskName: $task->getName());
});
}
/**
* {@inheritdoc}
*/
public function toArray(bool $keepKeys = true): array
{
return $keepKeys ? $this->tasks : array_values(array: $this->tasks);
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset): bool
{
if (!is_string(value: $offset)) {
throw new InvalidArgumentException(message: sprintf('The offset must be a string, received "%s"', gettype($offset)));
}
return $this->has(taskName: $offset);
}
/**
* {@inheritdoc}
*/
public function offsetGet($offset): ?TaskInterface
{
if (!is_string(value: $offset)) {
throw new InvalidArgumentException(message: sprintf('The offset must be a string, received "%s"', gettype($offset)));
}
return $this->get(taskName: $offset);
}
/**
* {@inheritdoc}
*
* @param string|null|mixed $offset The name of the task, if null is passed and $value isn't, {@see TaskListInterface::add()} is called
* @param TaskInterface|mixed $value A TaskInstance instance or mixed (will trigger an exception)
*
* @throws Throwable If the $value is not a {@see TaskInterface}
*/
public function offsetSet($offset, $value): void
{
if (!$value instanceof TaskInterface) {
throw new InvalidArgumentException(message: sprintf('A task must be given, received "%s"', gettype($value)));
}
null === $offset ? $this->add(task: $value) : $this->tasks[$offset] = $value;
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset): void
{
if (!is_string(value: $offset)) {
throw new InvalidArgumentException(message: sprintf('The offset must be a string, received "%s"', gettype($offset)));
}
$this->remove(taskName: $offset);
}
/**
* {@inheritdoc}
*/
public function count(): int
{
return count(value: $this->tasks);
}
/**
* @return ArrayIterator<int|string, TaskInterface>
*/
public function getIterator(): Traversable
{
return new ArrayIterator(array: $this->tasks);
}
}