Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
FileSpec
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
3 / 3
17
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 attachEmbeddedFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toPdf
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
14
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core\FileSpec;
6
7use Phpdftk\Pdf\Core\PdfArray;
8use Phpdftk\Pdf\Core\PdfBoolean;
9use Phpdftk\Pdf\Core\PdfDictionary;
10use Phpdftk\Pdf\Core\PdfName;
11use Phpdftk\Pdf\Core\PdfObject;
12use Phpdftk\Pdf\Core\PdfReference;
13use Phpdftk\Pdf\Core\PdfString;
14use Phpdftk\Pdf\Core\PdfVersion;
15use Phpdftk\Pdf\Core\RequiresPdfVersion;
16
17/**
18 * File specification dictionary (/Type /Filespec) — ISO 32000-2 §7.11.3.
19 *
20 * Identifies an external file or an embedded file stream. Used by
21 * FileAttachment annotations, embedded-file name trees, and the Launch,
22 * ImportData, SubmitForm, and GoToE actions.
23 */
24#[RequiresPdfVersion(PdfVersion::V1_3)]
25class FileSpec extends PdfObject
26{
27    public const PDF_TYPE = 'Filespec';
28
29    public ?PdfName $fs = null;              // /FS  file system (URL, etc.)
30    public ?PdfString $f = null;             // /F   file spec (PDFDocEncoded)
31    public ?PdfString $uf = null;            // /UF  Unicode file spec
32    public ?PdfString $dos = null;           // /DOS legacy
33    public ?PdfString $mac = null;           // /Mac legacy
34    public ?PdfString $unix = null;          // /Unix legacy
35    public ?PdfArray $id = null;             // /ID  file identifier
36    public ?bool $volatile = null;           // /V
37    public ?PdfDictionary $ef = null;        // /EF  embedded files dict
38    public ?PdfDictionary $rf = null;        // /RF  related files dict
39    public ?PdfString $desc = null;          // /Desc description
40    public ?PdfReference $ci = null;         // /CI  collection item dict
41    #[RequiresPdfVersion(PdfVersion::V2_0)]
42    public ?PdfName $afRelationship = null;  // /AFRelationship PDF 2.0
43
44    public function __construct(?string $fileName = null)
45    {
46        if ($fileName !== null) {
47            $this->f = new PdfString($fileName);
48            $this->uf = new PdfString($fileName);
49        }
50    }
51
52    /**
53     * Attach an embedded file stream under the /F key of /EF.
54     */
55    public function attachEmbeddedFile(PdfReference $stream): void
56    {
57        $this->ef = new PdfDictionary(['F' => $stream, 'UF' => $stream]);
58    }
59
60    public function toPdf(): string
61    {
62        $dict = new PdfDictionary();
63        $dict->set('Type', new PdfName(self::PDF_TYPE));
64
65        if ($this->fs !== null) {
66            $dict->set('FS', $this->fs);
67        }
68        if ($this->f !== null) {
69            $dict->set('F', $this->f);
70        }
71        if ($this->uf !== null) {
72            $dict->set('UF', $this->uf);
73        }
74        if ($this->dos !== null) {
75            $dict->set('DOS', $this->dos);
76        }
77        if ($this->mac !== null) {
78            $dict->set('Mac', $this->mac);
79        }
80        if ($this->unix !== null) {
81            $dict->set('Unix', $this->unix);
82        }
83        if ($this->id !== null) {
84            $dict->set('ID', $this->id);
85        }
86        if ($this->volatile !== null) {
87            $dict->set('V', new PdfBoolean($this->volatile));
88        }
89        if ($this->ef !== null) {
90            $dict->set('EF', $this->ef);
91        }
92        if ($this->rf !== null) {
93            $dict->set('RF', $this->rf);
94        }
95        if ($this->desc !== null) {
96            $dict->set('Desc', $this->desc);
97        }
98        if ($this->ci !== null) {
99            $dict->set('CI', $this->ci);
100        }
101        if ($this->afRelationship !== null) {
102            $dict->set('AFRelationship', $this->afRelationship);
103        }
104
105        return $dict->toPdf();
106    }
107}