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 |
|
throw $this->inputFactory ? new InvalidReturnValue : new InvalidArgument; |
41
|
|
|
} |
42
|
104 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Static alias of normal constructor. |
46
|
|
|
* |
47
|
|
|
* @param callable|array|Traversable $input |
48
|
|
|
* @return Collection |
49
|
|
|
*/ |
50
|
8 |
|
public static function from($input) |
51
|
|
|
{ |
52
|
8 |
|
return new self($input); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns lazy collection of values, where first value is $input and all subsequent values are computed by applying |
57
|
|
|
* $function to the last value in the collection. By default this produces an infinite collection. However you can |
58
|
|
|
* end the collection by throwing a NoMoreItems exception. |
59
|
|
|
* |
60
|
|
|
* @param mixed $input |
61
|
|
|
* @param callable $function |
62
|
|
|
* @return Collection |
63
|
|
|
*/ |
64
|
2 |
|
public static function iterate($input, callable $function) |
65
|
|
|
{ |
66
|
2 |
|
return iterate($input, $function); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns a lazy collection of $value repeated $times times. If $times is not provided the collection is infinite. |
71
|
|
|
* |
72
|
|
|
* @param mixed $value |
73
|
|
|
* @param int $times |
74
|
|
|
* @return Collection |
75
|
|
|
*/ |
76
|
3 |
|
public static function repeat($value, $times = -1) |
77
|
|
|
{ |
78
|
3 |
|
return repeat($value, $times); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns a lazy collection of numbers starting at $start, incremented by $step until $end is reached. |
83
|
|
|
* |
84
|
|
|
* @param int $start |
85
|
|
|
* @param int|null $end |
86
|
|
|
* @param int $step |
87
|
|
|
* @return Collection |
88
|
|
|
*/ |
89
|
2 |
|
public static function range($start = 0, $end = null, $step = 1) |
90
|
|
|
{ |
91
|
2 |
|
return \DusanKasan\Knapsack\range($start, $end, $step); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
* @throws InvalidReturnValue |
97
|
|
|
*/ |
98
|
103 |
|
public function getIterator() |
99
|
|
|
{ |
100
|
103 |
|
if ($this->inputFactory) { |
101
|
80 |
|
$input = call_user_func($this->inputFactory); |
102
|
|
|
|
103
|
80 |
|
if (is_array($input)) { |
104
|
1 |
|
$input = new RecursiveArrayIterator($input); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
80 |
|
if (!($input instanceof Traversable)) { |
108
|
|
|
throw new InvalidReturnValue; |
109
|
|
|
} |
110
|
|
|
|
111
|
80 |
|
$this->input = $input; |
112
|
80 |
|
} |
113
|
|
|
|
114
|
103 |
|
return $this->input; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
1 |
|
public function serialize() |
121
|
|
|
{ |
122
|
1 |
|
return serialize( |
123
|
1 |
|
toArray( |
124
|
1 |
|
map( |
125
|
1 |
|
$this->input, |
126
|
1 |
|
function ($value, $key) { |
127
|
1 |
|
return [$key, $value]; |
128
|
|
|
} |
129
|
1 |
|
) |
130
|
1 |
|
) |
131
|
1 |
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
1 |
|
public function unserialize($serialized) |
138
|
|
|
{ |
139
|
1 |
|
$this->input = dereferenceKeyValue(unserialize($serialized)); |
140
|
1 |
|
} |
141
|
|
|
} |
142
|
|
|
|