Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumerableDeferred.php
More file actions
Latest commit
112 lines (100 loc) · 2.47 KB
/
Copy pathEnumerableDeferred.php
File metadata and controls
112 lines (100 loc) · 2.47 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
<?php
/**
* @author: stev leibelt <artodeto@bazzline.net>
* @since: 2015-07-30
*/
namespaceNet\Bazzline\Component\Toolbox\Process;
class EnumerableDeferred
{
/** @var callable */
private$finisher;
/** @var callable */
private$initializer;
privateint$iterator;
privateint$iterationLimit;
/** callable */
private$processor;
/**
* @param callable $initializer
* @param callable $processor
* @param callable $finisher
* @param int $limit
*/
publicfunction__construct(
callable$initializer,
callable$processor,
callable$finisher,
int$limit = 10
) {
$this->iterationLimit = (int) $limit;
$this->initializer = $initializer;
$this->processor = $processor;
$this->finisher = $finisher;
$this->initialize();
}
publicfunction__destruct()
{
$this->finish();
}
/**
* @param mixed $data,... unlimited optional number of additional variables [...]
*/
publicfunction__invoke(mixed$data = null): void
{
call_user_func_array(
[
$this,
'increase'
],
func_get_args()
);
}
/**
* @param mixed $data,... unlimited optional number of additional variables [...]
*/
publicfunctionincrease(mixed$data = null): void
{
$arguments = func_get_args();
$this->call($this->processor, $arguments);
if ($this->limitReached($this->iterator, $this->iterationLimit)) {
//finish
$this->finish();
//reinitialize
$this->initialize();
} else {
++$this->iterator;
}
}
privatefunctionfinish(): void
{
$this->call($this->finisher);
}
privatefunctioninitialize(): void
{
$this->iterator = 0;
$this->call($this->initializer);
}
/**
* @param callable $callable
* @param null|array $arguments
*/
privatefunctioncall(
callable$callable,
array$arguments = null
): void {
if (!is_null($arguments)) {
call_user_func_array(
$callable,
$arguments
);
} else {
call_user_func($callable);
}
}
privatefunctionlimitReached(
int$iterator,
int$limit
): bool {
return ($iterator >= $limit);
}
}