|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace iio\libmergepdf; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Parse page numbers from string |
|
7
|
|
|
*/ |
|
8
|
|
|
class Pages implements \IteratorAggregate |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var integer[] Added integer page numbers |
|
12
|
|
|
*/ |
|
13
|
|
|
private $pages = []; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Constructor |
|
17
|
|
|
* |
|
18
|
|
|
* Pages should be formatted as 1,3,6 or 12-16 or combined. Note that pages |
|
19
|
|
|
* are merged in the order that you provide them. If you put pages 12-14 |
|
20
|
|
|
* before 1-5 then 12-14 will be placed first. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $expressionString |
|
23
|
|
|
* @throws Exception If unable to parse page numbers |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($expressionString = '') |
|
26
|
|
|
{ |
|
27
|
|
|
$expressions = explode( |
|
28
|
|
|
',', |
|
29
|
|
|
str_replace(' ', '', $expressionString) |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
foreach ($expressions as $expr) { |
|
33
|
|
|
if (empty($expr)) { |
|
34
|
|
|
continue; |
|
35
|
|
|
} |
|
36
|
|
|
if (ctype_digit($expr)) { |
|
37
|
|
|
$this->addPage($expr); |
|
38
|
|
|
continue; |
|
39
|
|
|
} |
|
40
|
|
|
if (preg_match("/^(\d+)-(\d+)/", $expr, $matches)) { |
|
41
|
|
|
$this->addRange($matches[1], $matches[2]); |
|
42
|
|
|
continue; |
|
43
|
|
|
} |
|
44
|
|
|
throw new Exception("Invalid page number(s) for expression '$expr'"); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add page to collection |
|
50
|
|
|
* |
|
51
|
|
|
* @param int|string $page |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function addPage($page) |
|
55
|
|
|
{ |
|
56
|
|
|
assert('is_numeric($page)'); |
|
57
|
|
|
$this->pages[] = intval($page); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Add range of pages |
|
62
|
|
|
* |
|
63
|
|
|
* @param int|string $start |
|
64
|
|
|
* @param int|string $end |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
public function addRange($start, $end) |
|
68
|
|
|
{ |
|
69
|
|
|
assert('is_numeric($start)'); |
|
70
|
|
|
assert('is_numeric($end)'); |
|
71
|
|
|
$this->pages = array_merge($this->pages, range($start, $end)); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get array of integer page numbers |
|
76
|
|
|
* |
|
77
|
|
|
* @return integer[] |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getPages() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->pages; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get iterator of page numbers |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getIterator() |
|
88
|
|
|
{ |
|
89
|
|
|
return new \ArrayIterator($this->getPages()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Check if pages has been sprcified |
|
94
|
|
|
* |
|
95
|
|
|
* @return boolean |
|
96
|
|
|
*/ |
|
97
|
|
|
public function hasPages() |
|
98
|
|
|
{ |
|
99
|
|
|
return !empty($this->getPages()); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..