Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.15% |
25 / 26 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| OutlineItem | |
96.15% |
25 / 26 |
|
50.00% |
1 / 2 |
14 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| toPdf | |
96.00% |
24 / 25 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Pdf\Core\Document; |
| 6 | |
| 7 | use Phpdftk\Pdf\Core\PdfArray; |
| 8 | use Phpdftk\Pdf\Core\PdfDictionary; |
| 9 | use Phpdftk\Pdf\Core\PdfName; |
| 10 | use Phpdftk\Pdf\Core\PdfNumber; |
| 11 | use Phpdftk\Pdf\Core\PdfObject; |
| 12 | use Phpdftk\Pdf\Core\PdfReference; |
| 13 | use Phpdftk\Pdf\Core\PdfString; |
| 14 | use Phpdftk\Pdf\Core\PdfVersion; |
| 15 | use Phpdftk\Pdf\Core\RequiresPdfVersion; |
| 16 | |
| 17 | /** |
| 18 | * PDF Outline Item (bookmark entry). |
| 19 | * |
| 20 | * Each item in the bookmarks tree. Items form a doubly-linked list |
| 21 | * at each level (Prev/Next) and a parent/child hierarchy (Parent/First/Last). |
| 22 | * |
| 23 | * /Dest or /A must be set for the bookmark to navigate somewhere. |
| 24 | */ |
| 25 | #[RequiresPdfVersion(PdfVersion::V1_1)] |
| 26 | class OutlineItem extends PdfObject |
| 27 | { |
| 28 | public PdfString $title; // /Title - required; displayed label |
| 29 | |
| 30 | public ?PdfReference $parent = null; // /Parent - required; parent outline item or Outline root |
| 31 | public ?PdfReference $prev = null; // /Prev - previous sibling |
| 32 | public ?PdfReference $next = null; // /Next - next sibling |
| 33 | public ?PdfReference $first = null; // /First - first child |
| 34 | public ?PdfReference $last = null; // /Last - last child |
| 35 | public int $count = 0; // /Count - negative = subtree closed |
| 36 | |
| 37 | /** /Dest - destination: PdfArray, PdfName (named dest), or string */ |
| 38 | public mixed $dest = null; |
| 39 | |
| 40 | public ?PdfReference $a = null; // /A - action (alternative to /Dest) |
| 41 | |
| 42 | /** @var PdfArray|null /C - RGB color [r g b], values 0.0–1.0 */ |
| 43 | public ?PdfArray $c = null; |
| 44 | |
| 45 | public int $f = 0; // /F - style flags: 1=italic, 2=bold |
| 46 | |
| 47 | public function __construct(string|PdfString $title) |
| 48 | { |
| 49 | $this->title = is_string($title) ? new PdfString($title) : $title; |
| 50 | } |
| 51 | |
| 52 | public function toPdf(): string |
| 53 | { |
| 54 | $dict = new PdfDictionary(); |
| 55 | $dict->set('Title', $this->title); |
| 56 | |
| 57 | if ($this->parent !== null) { |
| 58 | $dict->set('Parent', $this->parent); |
| 59 | } |
| 60 | if ($this->prev !== null) { |
| 61 | $dict->set('Prev', $this->prev); |
| 62 | } |
| 63 | if ($this->next !== null) { |
| 64 | $dict->set('Next', $this->next); |
| 65 | } |
| 66 | if ($this->first !== null) { |
| 67 | $dict->set('First', $this->first); |
| 68 | } |
| 69 | if ($this->last !== null) { |
| 70 | $dict->set('Last', $this->last); |
| 71 | } |
| 72 | if ($this->count !== 0) { |
| 73 | $dict->set('Count', new PdfNumber($this->count)); |
| 74 | } |
| 75 | if ($this->dest !== null) { |
| 76 | if ($this->dest instanceof \Phpdftk\Pdf\Core\Serializable) { |
| 77 | $dict->set('Dest', $this->dest); |
| 78 | } else { |
| 79 | $dict->set('Dest', new PdfString((string) $this->dest)); |
| 80 | } |
| 81 | } |
| 82 | if ($this->a !== null) { |
| 83 | $dict->set('A', $this->a); |
| 84 | } |
| 85 | if ($this->c !== null) { |
| 86 | $dict->set('C', $this->c); |
| 87 | } |
| 88 | if ($this->f !== 0) { |
| 89 | $dict->set('F', new PdfNumber($this->f)); |
| 90 | } |
| 91 | |
| 92 | return $dict->toPdf(); |
| 93 | } |
| 94 | } |