1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Novactive Collection. |
4
|
|
|
* |
5
|
|
|
* @author Luke Visinoni <[email protected], [email protected]> |
6
|
|
|
* @author Sébastien Morel <[email protected], [email protected]> |
7
|
|
|
* @copyright 2017 Novactive |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Novactive\Collection\Selector; |
12
|
|
|
|
13
|
|
|
use Novactive\Collection\Collection; |
14
|
|
|
use Novactive\Collection\Factory; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Range. |
18
|
|
|
*/ |
19
|
|
|
class Range |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Explode the selector to get ranges. |
23
|
|
|
* |
24
|
|
|
* @param $selector |
25
|
|
|
* |
26
|
|
|
* @return Collection |
27
|
|
|
*/ |
28
|
23 |
|
protected function getRanges($selector) |
29
|
|
|
{ |
30
|
23 |
|
return Factory::create(explode(';', $selector))->map( |
31
|
|
|
function ($range) { |
32
|
23 |
|
if ($separator = $this->dotDotSeparator($range) != false) { |
33
|
5 |
|
return Factory::create([substr($range, 0, $separator), substr($range, $separator + 2)]); |
34
|
|
|
} |
35
|
20 |
|
if (($separator = $this->columnSeparator($range) != false) || |
36
|
14 |
|
($separator = $this->dashSeparator($range) != false) |
37
|
20 |
|
) { |
38
|
13 |
|
return Factory::create([substr($range, 0, $separator), substr($range, $separator + 1)]); |
39
|
|
|
} |
40
|
11 |
|
if ($separator = $this->commaSeparator($range) != false) { |
41
|
3 |
|
return Factory::create( |
42
|
|
|
[ |
43
|
3 |
|
substr($range, 0, $separator), |
44
|
3 |
|
intval(substr($range, 0, $separator)) + intval(substr($range, $separator + 1) - 1), |
45
|
|
|
] |
46
|
3 |
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// just a number here |
50
|
10 |
|
return Factory::create([$range, $range]); |
51
|
|
|
} |
52
|
23 |
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Collection $parameters |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
25 |
|
public function supports(Collection $parameters) |
61
|
|
|
{ |
62
|
25 |
|
return $parameters->assert( |
63
|
|
|
function ($param) { |
64
|
25 |
|
if (is_array($param) && count($param) == 2) { |
65
|
5 |
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
22 |
|
return preg_match('/^([0-9])*([,-:;\\.]*)([0-9])*$/uis', $param) == 1; |
69
|
25 |
|
}, |
70
|
|
|
true |
71
|
25 |
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Collection $parameters |
76
|
|
|
* @param Collection $collection |
77
|
|
|
*/ |
78
|
23 |
|
public function convert(Collection $parameters, Collection $collection) |
79
|
|
|
{ |
80
|
23 |
|
$newCollection = Factory::create(); |
81
|
23 |
|
$selector = $parameters->map( |
82
|
|
|
function ($param) { |
83
|
23 |
|
if ((is_array($param) && count($param) == 2)) { |
84
|
5 |
|
return implode(':', $param); |
85
|
|
|
} |
86
|
|
|
|
87
|
20 |
|
return $param; |
88
|
|
|
} |
89
|
23 |
|
)->implode(';'); |
90
|
|
|
|
91
|
23 |
|
return $this->getRanges($selector)->reduce( |
92
|
23 |
|
function (Collection $accumulator, Collection $range) use ($collection) { |
93
|
23 |
|
$from = $range->first(); |
94
|
23 |
|
$to = $range->last(); |
95
|
23 |
|
if ($to >= $from) { |
96
|
19 |
|
return $accumulator->append($collection->slice($from, ($to - $from) + 1)); |
97
|
|
|
} |
98
|
|
|
|
99
|
7 |
|
return $accumulator->append($collection->slice($to, ($from - $to) + 1)->inverse()); |
100
|
23 |
|
}, |
101
|
|
|
$newCollection |
102
|
23 |
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $string |
107
|
|
|
* |
108
|
|
|
* @return bool|int |
109
|
|
|
*/ |
110
|
23 |
|
protected function dotDotSeparator($string) |
111
|
|
|
{ |
112
|
23 |
|
return strpos($string, '..'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $string |
117
|
|
|
* |
118
|
|
|
* @return bool|int |
119
|
|
|
*/ |
120
|
20 |
|
protected function columnSeparator($string) |
121
|
|
|
{ |
122
|
20 |
|
return strpos($string, ':'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param $string |
127
|
|
|
* |
128
|
|
|
* @return bool|int |
129
|
|
|
*/ |
130
|
14 |
|
protected function dashSeparator($string) |
131
|
|
|
{ |
132
|
14 |
|
return strpos($string, '-'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $string |
137
|
|
|
* |
138
|
|
|
* @return bool|int |
139
|
|
|
*/ |
140
|
11 |
|
protected function commaSeparator($string) |
141
|
|
|
{ |
142
|
11 |
|
return strpos($string, ','); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|