Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| PageSelector | |
100.00% |
18 / 18 |
|
100.00% |
8 / 8 |
17 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| all | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| pages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| range | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| even | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| odd | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| matches | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
8 | |||
| resolve | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Pdf\Toolkit; |
| 6 | |
| 7 | /** |
| 8 | * Selects which pages an operation applies to. |
| 9 | * |
| 10 | * Used by PdfStamper, AnnotationFlattener, PageTransformer, TextRedactor, etc. |
| 11 | * Page numbers are 1-based throughout the toolkit API. |
| 12 | */ |
| 13 | final class PageSelector |
| 14 | { |
| 15 | private function __construct( |
| 16 | private readonly string $mode, |
| 17 | /** @var list<int> */ |
| 18 | private readonly array $pages = [], |
| 19 | private readonly int $from = 0, |
| 20 | private readonly int $to = 0, |
| 21 | ) {} |
| 22 | |
| 23 | public static function all(): self |
| 24 | { |
| 25 | return new self('all'); |
| 26 | } |
| 27 | |
| 28 | public static function pages(int ...$pageNumbers): self |
| 29 | { |
| 30 | return new self('pages', array_values($pageNumbers)); |
| 31 | } |
| 32 | |
| 33 | public static function range(int $from, int $to): self |
| 34 | { |
| 35 | return new self('range', from: $from, to: $to); |
| 36 | } |
| 37 | |
| 38 | public static function even(): self |
| 39 | { |
| 40 | return new self('even'); |
| 41 | } |
| 42 | |
| 43 | public static function odd(): self |
| 44 | { |
| 45 | return new self('odd'); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Check if a page number matches this selector. |
| 50 | * |
| 51 | * @param int $pageNumber 1-based page number |
| 52 | * @param int $totalPages Total pages in the document |
| 53 | */ |
| 54 | public function matches(int $pageNumber, int $totalPages): bool |
| 55 | { |
| 56 | return match ($this->mode) { |
| 57 | 'all' => true, |
| 58 | 'pages' => in_array($pageNumber, $this->pages, true), |
| 59 | 'range' => $pageNumber >= $this->from && $pageNumber <= $this->to, |
| 60 | 'even' => $pageNumber % 2 === 0, |
| 61 | 'odd' => $pageNumber % 2 === 1, |
| 62 | default => false, |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Resolve matching page indices (0-based) for a document. |
| 68 | * |
| 69 | * @return list<int> 0-based page indices |
| 70 | */ |
| 71 | public function resolve(int $totalPages): array |
| 72 | { |
| 73 | $indices = []; |
| 74 | for ($i = 1; $i <= $totalPages; $i++) { |
| 75 | if ($this->matches($i, $totalPages)) { |
| 76 | $indices[] = $i - 1; |
| 77 | } |
| 78 | } |
| 79 | return $indices; |
| 80 | } |
| 81 | } |