Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| PdfName | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
16 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toPdf | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
15 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Pdf\Core; |
| 6 | |
| 7 | /** |
| 8 | * Represents a PDF name literal, e.g. /MediaBox, /Type, /Font. |
| 9 | * Names beginning with / are the canonical PDF name syntax. |
| 10 | */ |
| 11 | class PdfName implements Serializable |
| 12 | { |
| 13 | public function __construct(public readonly string $value) {} |
| 14 | |
| 15 | /** |
| 16 | * Returns the PDF name token, escaping characters outside the printable |
| 17 | * ASCII range (except #) with #XX notation as required by the spec. |
| 18 | */ |
| 19 | public function toPdf(): string |
| 20 | { |
| 21 | $escaped = ''; |
| 22 | $len = strlen($this->value); |
| 23 | for ($i = 0; $i < $len; $i++) { |
| 24 | $c = $this->value[$i]; |
| 25 | $ord = ord($c); |
| 26 | // Must escape: delimiters, whitespace, #, and non-printable/high bytes |
| 27 | if ( |
| 28 | $ord < 0x21 |
| 29 | || $ord > 0x7E |
| 30 | || $c === '#' |
| 31 | || $c === '(' |
| 32 | || $c === ')' |
| 33 | || $c === '<' |
| 34 | || $c === '>' |
| 35 | || $c === '[' |
| 36 | || $c === ']' |
| 37 | || $c === '{' |
| 38 | || $c === '}' |
| 39 | || $c === '/' |
| 40 | || $c === '%' |
| 41 | ) { |
| 42 | $escaped .= sprintf('#%02X', $ord); |
| 43 | } else { |
| 44 | $escaped .= $c; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return '/' . $escaped; |
| 49 | } |
| 50 | } |