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