Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
StructElem
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
3 / 3
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMinimumPdfVersion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toPdf
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
13
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core\Document;
6
7use Phpdftk\Pdf\Core\PdfArray;
8use Phpdftk\Pdf\Core\PdfDictionary;
9use Phpdftk\Pdf\Core\PdfName;
10use Phpdftk\Pdf\Core\PdfNumber;
11use Phpdftk\Pdf\Core\PdfObject;
12use Phpdftk\Pdf\Core\PdfReference;
13use Phpdftk\Pdf\Core\PdfString;
14use Phpdftk\Pdf\Core\PdfVersion;
15use Phpdftk\Pdf\Core\PdfVersionAware;
16use Phpdftk\Pdf\Core\RequiresPdfVersion;
17
18/**
19 * PDF Structure Element (ISO 32000-2 Table 324).
20 *
21 * Represents a node in the structure tree for tagged PDF.
22 *
23 * Example:
24 *   $elem = new StructElem('P');
25 *   $elem->p = new PdfReference($parent->objectNumber);
26 *   $elem->t = new PdfString('Paragraph Title');
27 */
28#[RequiresPdfVersion(PdfVersion::V1_3)]
29class StructElem extends PdfObject implements PdfVersionAware
30{
31    public const PDF_TYPE = 'StructElem';
32
33    public PdfName $s;                              // /S - structure type (required)
34    public ?PdfReference $p = null;                 // /P - parent element
35    public ?PdfString $id = null;                   // /ID
36    public ?PdfReference $pg = null;                // /Pg - page
37    public PdfReference|PdfArray|null $k = null;    // /K - children
38    public ?PdfArray $a = null;                     // /A - attribute objects
39    public ?PdfArray $c = null;                     // /C - class names
40    public ?int $r = null;                          // /R - revision number
41    public ?PdfString $t = null;                    // /T - title
42    public ?PdfString $lang = null;                 // /Lang
43    public ?PdfString $alt = null;                  // /Alt - alternate description
44    public ?PdfString $e = null;                    // /E - expanded form
45    public ?PdfString $actualText = null;           // /ActualText
46
47    public function __construct(string $structureType)
48    {
49        $this->s = new PdfName($structureType);
50    }
51
52    public function getMinimumPdfVersion(): ?PdfVersion
53    {
54        return StandardStructureType::minimumVersion($this->s->value);
55    }
56
57    public function toPdf(): string
58    {
59        $dict = new PdfDictionary();
60        $dict->set('Type', new PdfName(self::PDF_TYPE));
61        $dict->set('S', $this->s);
62
63        if ($this->p !== null) {
64            $dict->set('P', $this->p);
65        }
66        if ($this->id !== null) {
67            $dict->set('ID', $this->id);
68        }
69        if ($this->pg !== null) {
70            $dict->set('Pg', $this->pg);
71        }
72        if ($this->k !== null) {
73            $dict->set('K', $this->k);
74        }
75        if ($this->a !== null) {
76            $dict->set('A', $this->a);
77        }
78        if ($this->c !== null) {
79            $dict->set('C', $this->c);
80        }
81        if ($this->r !== null) {
82            $dict->set('R', new PdfNumber($this->r));
83        }
84        if ($this->t !== null) {
85            $dict->set('T', $this->t);
86        }
87        if ($this->lang !== null) {
88            $dict->set('Lang', $this->lang);
89        }
90        if ($this->alt !== null) {
91            $dict->set('Alt', $this->alt);
92        }
93        if ($this->e !== null) {
94            $dict->set('E', $this->e);
95        }
96        if ($this->actualText !== null) {
97            $dict->set('ActualText', $this->actualText);
98        }
99
100        return $dict->toPdf();
101    }
102}