Completed
Push — master ( 4dc0e8...439184 )
by Hannes
22s queued 18s
created

Pages::getPageNumbers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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));
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->pages, range($start, $end)) of type array is incompatible with the declared type array<integer,integer> of property $pages.

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..

Loading history...
61
    }
62
63
    public function getPageNumbers(): array
64
    {
65
        return $this->pages;
66
    }
67
}
68