diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:45:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:45:49 +0000 |
commit | 0ff39c83d38ce538a9f5dba53eca0fa9cb16d9e6 (patch) | |
tree | 84c735df2e97350a721273e9dd425729d43cc8a2 /vendor/iio/libmergepdf/src/Pages.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-pdfexport-0ff39c83d38ce538a9f5dba53eca0fa9cb16d9e6.tar.xz icingaweb2-module-pdfexport-0ff39c83d38ce538a9f5dba53eca0fa9cb16d9e6.zip |
Adding upstream version 0.10.2+dfsg1.upstream/0.10.2+dfsg1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/iio/libmergepdf/src/Pages.php')
-rw-r--r-- | vendor/iio/libmergepdf/src/Pages.php | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/vendor/iio/libmergepdf/src/Pages.php b/vendor/iio/libmergepdf/src/Pages.php new file mode 100644 index 0000000..7675315 --- /dev/null +++ b/vendor/iio/libmergepdf/src/Pages.php @@ -0,0 +1,67 @@ +<?php + +declare(strict_types = 1); + +namespace iio\libmergepdf; + +/** + * Parse page numbers from string + */ +final class Pages implements PagesInterface +{ + /** + * @var int[] Added integer page numbers + */ + private $pages = []; + + /** + * Parse page numbers from expression string + * + * Pages should be formatted as 1,3,6 or 12-16 or combined. Note that pages + * are merged in the order that you provide them. If you put pages 12-14 + * before 1-5 then 12-14 will be placed first. + */ + public function __construct(string $expressionString = '') + { + $expressions = explode( + ',', + str_replace(' ', '', $expressionString) + ); + + foreach ($expressions as $expr) { + if (empty($expr)) { + continue; + } + if (ctype_digit($expr)) { + $this->addPage((int)$expr); + continue; + } + if (preg_match("/^(\d+)-(\d+)/", $expr, $matches)) { + $this->addRange((int)$matches[1], (int)$matches[2]); + continue; + } + throw new Exception("Invalid page number(s) for expression '$expr'"); + } + } + + /** + * Add a single page + */ + public function addPage(int $page): void + { + $this->pages[] = $page; + } + + /** + * Add a range of pages + */ + public function addRange(int $start, int $end): void + { + $this->pages = array_merge($this->pages, range($start, $end)); + } + + public function getPageNumbers(): array + { + return $this->pages; + } +} |