Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| PdfArray | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toPdf | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Pdf\Core; |
| 6 | |
| 7 | /** |
| 8 | * Represents a PDF array object: [ item1 item2 ... ] |
| 9 | * |
| 10 | * Items may be any Serializable object, a raw string (emitted verbatim), |
| 11 | * or a scalar int/float (treated as PdfNumber). |
| 12 | */ |
| 13 | class PdfArray implements Serializable |
| 14 | { |
| 15 | /** @param array<int|string, mixed> $items */ |
| 16 | public function __construct(public readonly array $items = []) {} |
| 17 | |
| 18 | public function toPdf(): string |
| 19 | { |
| 20 | $parts = []; |
| 21 | foreach ($this->items as $item) { |
| 22 | $parts[] = match (true) { |
| 23 | $item instanceof Serializable => $item->toPdf(), |
| 24 | is_int($item), is_float($item) => (new PdfNumber($item))->toPdf(), |
| 25 | is_bool($item) => (new PdfBoolean($item))->toPdf(), |
| 26 | is_null($item) => 'null', |
| 27 | default => (string) $item, |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | return '[ ' . implode(' ', $parts) . ' ]'; |
| 32 | } |
| 33 | } |