Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| RoleMap | |
100.00% |
7 / 7 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| map | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| toDictionary | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| toPdf | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Pdf\Core\Document; |
| 6 | |
| 7 | use Phpdftk\Pdf\Core\PdfDictionary; |
| 8 | use Phpdftk\Pdf\Core\PdfName; |
| 9 | use Phpdftk\Pdf\Core\PdfVersion; |
| 10 | use Phpdftk\Pdf\Core\RequiresPdfVersion; |
| 11 | use Phpdftk\Pdf\Core\Serializable; |
| 12 | |
| 13 | /** |
| 14 | * RoleMap dictionary — ISO 32000-2 §14.7.3. |
| 15 | * |
| 16 | * Maps non-standard structure type names to standard structure types. |
| 17 | * Lives inline on `StructTreeRoot::$roleMap`. |
| 18 | * |
| 19 | * Example: |
| 20 | * $map = new RoleMap(); |
| 21 | * $map->map('Note', 'P'); |
| 22 | * $map->map('Heading1', 'H1'); |
| 23 | */ |
| 24 | #[RequiresPdfVersion(PdfVersion::V1_3)] |
| 25 | class RoleMap implements Serializable |
| 26 | { |
| 27 | /** @var array<string, string> */ |
| 28 | public array $entries = []; |
| 29 | |
| 30 | public function map(string $customType, string $standardType): self |
| 31 | { |
| 32 | $this->entries[$customType] = $standardType; |
| 33 | return $this; |
| 34 | } |
| 35 | |
| 36 | public function toDictionary(): PdfDictionary |
| 37 | { |
| 38 | $dict = new PdfDictionary(); |
| 39 | foreach ($this->entries as $from => $to) { |
| 40 | $dict->set($from, new PdfName($to)); |
| 41 | } |
| 42 | return $dict; |
| 43 | } |
| 44 | |
| 45 | public function toPdf(): string |
| 46 | { |
| 47 | return $this->toDictionary()->toPdf(); |
| 48 | } |
| 49 | } |