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