|
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
|
|
|
*/ |
|
28
|
106 |
|
public function __construct($input) |
|
29
|
|
|
{ |
|
30
|
106 |
|
if (is_callable($input)) { |
|
31
|
81 |
|
$this->inputFactory = $input; |
|
32
|
81 |
|
$input = $input(); |
|
33
|
81 |
|
} |
|
34
|
|
|
|
|
35
|
106 |
|
if (is_array($input)) { |
|
36
|
91 |
|
$this->input = new RecursiveArrayIterator($input); |
|
37
|
106 |
|
} elseif ($input instanceof Traversable) { |
|
38
|
81 |
|
$this->input = $input; |
|
39
|
81 |
|
} else { |
|
40
|
2 |
|
if ($this->inputFactory) { |
|
41
|
1 |
|
throw new InvalidReturnValue; |
|
42
|
|
|
} else { |
|
43
|
1 |
|
throw new InvalidArgument; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
104 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Static alias of normal constructor. |
|
50
|
|
|
* |
|
51
|
|
|
* @param callable|array|Traversable $input |
|
52
|
|
|
* @return Collection |
|
53
|
|
|
*/ |
|
54
|
8 |
|
public static function from($input) |
|
55
|
|
|
{ |
|
56
|
8 |
|
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
|
|
|
* $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
|
|
|
* |
|
64
|
|
|
* @param mixed $input |
|
65
|
|
|
* @param callable $function |
|
66
|
|
|
* @return Collection |
|
67
|
|
|
*/ |
|
68
|
4 |
|
public static function iterate($input, callable $function) |
|
69
|
|
|
{ |
|
70
|
2 |
|
return iterate($input, $function); |
|
71
|
4 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns a lazy collection of $value repeated $times times. If $times is not provided the collection is infinite. |
|
75
|
|
|
* |
|
76
|
|
|
* @param mixed $value |
|
77
|
|
|
* @param int $times |
|
78
|
|
|
* @return Collection |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public static function repeat($value, $times = -1) |
|
81
|
|
|
{ |
|
82
|
3 |
|
return repeat($value, $times); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns a lazy collection of numbers starting at $start, incremented by $step until $end is reached. |
|
87
|
|
|
* |
|
88
|
|
|
* @param int $start |
|
89
|
|
|
* @param int|null $end |
|
90
|
|
|
* @param int $step |
|
91
|
|
|
* @return Collection |
|
92
|
|
|
*/ |
|
93
|
2 |
|
public static function range($start = 0, $end = null, $step = 1) |
|
94
|
|
|
{ |
|
95
|
2 |
|
return \DusanKasan\Knapsack\range($start, $end, $step); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
* @throws InvalidReturnValue |
|
101
|
|
|
*/ |
|
102
|
103 |
|
public function getIterator() |
|
103
|
|
|
{ |
|
104
|
103 |
|
if ($this->inputFactory) { |
|
105
|
80 |
|
$input = call_user_func($this->inputFactory); |
|
106
|
|
|
|
|
107
|
80 |
|
if (is_array($input)) { |
|
108
|
1 |
|
$input = new RecursiveArrayIterator($input); |
|
109
|
1 |
|
} |
|
110
|
|
|
|
|
111
|
80 |
|
if (!($input instanceof Traversable)) { |
|
112
|
|
|
throw new InvalidReturnValue; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
80 |
|
$this->input = $input; |
|
116
|
80 |
|
} |
|
117
|
|
|
|
|
118
|
103 |
|
return $this->input; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function serialize() |
|
125
|
|
|
{ |
|
126
|
1 |
|
return serialize( |
|
127
|
1 |
|
toArray( |
|
128
|
1 |
|
map( |
|
129
|
1 |
|
$this->input, |
|
130
|
1 |
|
function ($value, $key) { |
|
131
|
1 |
|
return [$key, $value]; |
|
132
|
|
|
} |
|
133
|
1 |
|
) |
|
134
|
1 |
|
) |
|
135
|
1 |
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function unserialize($serialized) |
|
142
|
|
|
{ |
|
143
|
1 |
|
$this->input = dereferenceKeyValue(unserialize($serialized)); |
|
144
|
1 |
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|