1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DusanKasan\Knapsack; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use DusanKasan\Knapsack\Exceptions\InvalidArgument; |
7
|
|
|
use Iterator; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
use RecursiveArrayIterator; |
10
|
|
|
use Traversable; |
11
|
|
|
|
12
|
|
|
class Collection implements Iterator, \Serializable |
13
|
|
|
{ |
14
|
|
|
use CollectionTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Iterator |
18
|
|
|
*/ |
19
|
|
|
protected $input; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var callable |
23
|
|
|
*/ |
24
|
|
|
private $generatorFactory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param callable|Closure|array|Traversable $input If callable is passed, it must be a generator factory function |
28
|
|
|
*/ |
29
|
81 |
|
public function __construct($input) |
30
|
|
|
{ |
31
|
81 |
|
if (is_callable($input)) { |
32
|
68 |
|
$this->generatorFactory = $input; |
33
|
68 |
|
$this->input = $input(); |
34
|
81 |
|
} elseif (is_array($input)) { |
35
|
70 |
|
$input = new RecursiveArrayIterator($input); |
36
|
70 |
|
$this->input = $input; |
37
|
73 |
|
} elseif ($input instanceof IteratorAggregate) { |
38
|
1 |
|
$input = $input->getIterator(); |
39
|
1 |
|
$this->input = $input; |
40
|
6 |
|
} elseif ($input instanceof Iterator) { |
41
|
4 |
|
$this->input = $input; |
42
|
4 |
|
} else { |
43
|
1 |
|
throw new InvalidArgument; |
44
|
|
|
} |
45
|
80 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Static alias of normal constructor. |
49
|
|
|
* |
50
|
|
|
* @param array|Traversable $input |
51
|
|
|
* @return Collection |
52
|
|
|
*/ |
53
|
6 |
|
public static function from($input) |
54
|
|
|
{ |
55
|
6 |
|
return new self($input); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns lazy collection of values, where first value is $input and all subsequent values are computed by applying |
60
|
|
|
* $function to the last value in the collection. By default this produces an infinite collection. However you can |
61
|
|
|
* end the collection by throwing a NoMoreItems exception. |
62
|
|
|
* |
63
|
|
|
* @param mixed $input |
64
|
|
|
* @param callable $function |
65
|
|
|
* @return Collection |
66
|
|
|
*/ |
67
|
2 |
|
public static function iterate($input, callable $function) |
68
|
|
|
{ |
69
|
2 |
|
return iterate($input, $function); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns a lazy collection of $value repeated $times times. If $times is not provided the collection is infinite. |
74
|
|
|
* |
75
|
|
|
* @param mixed $value |
76
|
|
|
* @param int $times |
77
|
|
|
* @return Collection |
78
|
|
|
*/ |
79
|
3 |
|
public static function repeat($value, $times = -1) |
80
|
|
|
{ |
81
|
3 |
|
return repeat($value, $times); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns a lazy collection of numbers starting at $start, incremented by $step until $end is reached. |
86
|
|
|
* |
87
|
|
|
* @param int $start |
88
|
|
|
* @param int|null $end |
89
|
|
|
* @param int $step |
90
|
|
|
* @return Collection |
91
|
|
|
*/ |
92
|
2 |
|
public static function range($start = 0, $end = null, $step = 1) |
93
|
|
|
{ |
94
|
2 |
|
return \DusanKasan\Knapsack\range($start, $end, $step); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
76 |
|
public function current() |
101
|
|
|
{ |
102
|
76 |
|
return $this->input->current(); |
103
|
28 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
75 |
|
public function next() |
109
|
|
|
{ |
110
|
75 |
|
$this->input->next(); |
111
|
75 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
74 |
|
public function key() |
117
|
|
|
{ |
118
|
74 |
|
return $this->input->key(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
79 |
|
public function valid() |
125
|
|
|
{ |
126
|
79 |
|
return $this->input->valid(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
79 |
|
public function rewind() |
133
|
|
|
{ |
134
|
79 |
|
if ($this->generatorFactory) { |
135
|
68 |
|
$this->input = call_user_func($this->generatorFactory); |
136
|
68 |
|
} |
137
|
|
|
|
138
|
79 |
|
$this->input->rewind(); |
139
|
79 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
1 |
|
public function serialize() |
145
|
|
|
{ |
146
|
1 |
|
return serialize( |
147
|
1 |
|
toArray( |
148
|
1 |
|
map( |
149
|
1 |
|
$this->input, |
150
|
1 |
|
function ($value, $key) { |
151
|
1 |
|
return [$key, $value]; |
152
|
|
|
} |
153
|
1 |
|
) |
154
|
1 |
|
) |
155
|
1 |
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
1 |
|
public function unserialize($serialized) |
162
|
|
|
{ |
163
|
1 |
|
$this->input = dereferenceKeyValue(unserialize($serialized)); |
164
|
1 |
|
} |
165
|
|
|
} |
166
|
|
|
|