Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.37% covered (success)
97.37%
37 / 38
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FormXObject
97.37% covered (success)
97.37%
37 / 38
50.00% covered (danger)
50.00%
1 / 2
18
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 toPdf
97.22% covered (success)
97.22%
35 / 36
0.00% covered (danger)
0.00%
0 / 1
17
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core\Graphics\XObject;
6
7use Phpdftk\Pdf\Core\Content\Resources;
8use Phpdftk\Pdf\Core\Document\GroupAttributes;
9use Phpdftk\Pdf\Core\PdfArray;
10use Phpdftk\Pdf\Core\PdfDictionary;
11use Phpdftk\Pdf\Core\PdfName;
12use Phpdftk\Pdf\Core\PdfNumber;
13use Phpdftk\Pdf\Core\PdfReference;
14use Phpdftk\Pdf\Core\PdfStream;
15use Phpdftk\Pdf\Core\PdfString;
16use Phpdftk\Pdf\Core\Serializable;
17use Phpdftk\Pdf\Core\PdfVersion;
18use Phpdftk\Pdf\Core\RequiresPdfVersion;
19
20/**
21 * Form XObject (/Subtype /Form) — ISO 32000-2 §8.10, Table 95.
22 *
23 * A self-contained content stream that can be placed on a page. Carries
24 * its own resource dictionary, optional transparency group, optional
25 * reference-XObject pointer, and optional metadata.
26 */
27class FormXObject extends PdfStream
28{
29    public const PDF_TYPE    = 'XObject';
30    public const PDF_SUBTYPE = 'Form';
31
32    public PdfArray $bBox;                                     // /BBox - required
33    public ?PdfArray $matrix = null;                           // /Matrix
34    public Resources|PdfDictionary|null $resources = null;     // /Resources
35    public ?PdfName $formType = null;                          // /FormType
36    public GroupAttributes|Serializable|null $group = null;    // /Group - transparency group
37    public ?PdfReference $ref = null;                          // /Ref - reference XObject
38    public ?PdfReference $metadata = null;                     // /Metadata - XMP stream
39    public ?PdfReference $pieceInfo = null;                    // /PieceInfo
40    public ?PdfString $lastModified = null;                    // /LastModified
41    public ?int $structParent = null;                          // /StructParent
42    public ?int $structParents = null;                         // /StructParents
43    public ?PdfReference $oc = null;                           // /OC - optional content
44    #[RequiresPdfVersion(PdfVersion::V2_0)]
45    public ?PdfArray $af = null;                               // /AF - associated files
46    public ?PdfDictionary $opi = null;                         // /OPI
47    public ?PdfReference $measure = null;                      // /Measure
48    public ?PdfReference $ptData = null;                       // /PtData
49    public ?PdfName $name = null;                              // /Name - deprecated but permitted
50
51    public function __construct(PdfArray $bBox, string $data = '')
52    {
53        parent::__construct(new PdfDictionary(), $data);
54        $this->bBox = $bBox;
55    }
56
57    public function toPdf(): string
58    {
59        $this->dictionary->set('Type', new PdfName(self::PDF_TYPE));
60        $this->dictionary->set('Subtype', new PdfName(self::PDF_SUBTYPE));
61        if ($this->formType !== null) {
62            $this->dictionary->set('FormType', $this->formType);
63        }
64        $this->dictionary->set('BBox', $this->bBox);
65
66        if ($this->matrix !== null) {
67            $this->dictionary->set('Matrix', $this->matrix);
68        }
69        if ($this->resources !== null) {
70            $this->dictionary->set('Resources', $this->resources);
71        }
72        if ($this->group !== null) {
73            $this->dictionary->set('Group', $this->group);
74        }
75        if ($this->ref !== null) {
76            $this->dictionary->set('Ref', $this->ref);
77        }
78        if ($this->metadata !== null) {
79            $this->dictionary->set('Metadata', $this->metadata);
80        }
81        if ($this->pieceInfo !== null) {
82            $this->dictionary->set('PieceInfo', $this->pieceInfo);
83        }
84        if ($this->lastModified !== null) {
85            $this->dictionary->set('LastModified', $this->lastModified);
86        }
87        if ($this->structParent !== null) {
88            $this->dictionary->set('StructParent', new PdfNumber($this->structParent));
89        }
90        if ($this->structParents !== null) {
91            $this->dictionary->set('StructParents', new PdfNumber($this->structParents));
92        }
93        if ($this->oc !== null) {
94            $this->dictionary->set('OC', $this->oc);
95        }
96        if ($this->af !== null) {
97            $this->dictionary->set('AF', $this->af);
98        }
99        if ($this->opi !== null) {
100            $this->dictionary->set('OPI', $this->opi);
101        }
102        if ($this->measure !== null) {
103            $this->dictionary->set('Measure', $this->measure);
104        }
105        if ($this->ptData !== null) {
106            $this->dictionary->set('PtData', $this->ptData);
107        }
108        if ($this->name !== null) {
109            $this->dictionary->set('Name', $this->name);
110        }
111
112        return parent::toPdf();
113    }
114}