1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace iio\libmergepdf; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Parse page numbers from string |
9
|
|
|
*/ |
10
|
|
|
final class Pages implements PagesInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var int[] Added integer page numbers |
14
|
|
|
*/ |
15
|
|
|
private $pages = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Parse page numbers from expression string |
19
|
|
|
* |
20
|
|
|
* Pages should be formatted as 1,3,6 or 12-16 or combined. Note that pages |
21
|
|
|
* are merged in the order that you provide them. If you put pages 12-14 |
22
|
|
|
* before 1-5 then 12-14 will be placed first. |
23
|
|
|
*/ |
24
|
|
|
public function __construct(string $expressionString = '') |
25
|
|
|
{ |
26
|
|
|
$expressions = explode( |
27
|
|
|
',', |
28
|
|
|
str_replace(' ', '', $expressionString) |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
foreach ($expressions as $expr) { |
32
|
|
|
if (empty($expr)) { |
33
|
|
|
continue; |
34
|
|
|
} |
35
|
|
|
if (ctype_digit($expr)) { |
36
|
|
|
$this->addPage((int)$expr); |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
if (preg_match("/^(\d+)-(\d+)/", $expr, $matches)) { |
40
|
|
|
$this->addRange((int)$matches[1], (int)$matches[2]); |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
throw new Exception("Invalid page number(s) for expression '$expr'"); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add a single page |
49
|
|
|
*/ |
50
|
|
|
public function addPage(int $page): void |
51
|
|
|
{ |
52
|
|
|
$this->pages[] = $page; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Add a range of pages |
57
|
|
|
*/ |
58
|
|
|
public function addRange(int $start, int $end): void |
59
|
|
|
{ |
60
|
|
|
$this->pages = array_merge($this->pages, range($start, $end)); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get iterator of page numbers |
65
|
|
|
*/ |
66
|
|
|
public function getIterator(): iterable |
67
|
|
|
{ |
68
|
|
|
return new \ArrayIterator($this->pages); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check if pages has been specified |
73
|
|
|
*/ |
74
|
|
|
public function isEmpty(): bool |
75
|
|
|
{ |
76
|
|
|
return !empty($this->pages); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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..