Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.77% |
79 / 88 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
InfoObject | |
89.77% |
79 / 88 |
|
40.00% |
2 / 5 |
17.31 | |
0.00% |
0 / 1 |
__construct | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
parse | |
88.52% |
54 / 61 |
|
0.00% |
0 / 1 |
8.10 | |||
setMetadata | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getMetadata | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
__toString | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
4.00 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Pdf\Build\PdfObject; |
15 | |
16 | use Pop\Pdf\Document\Metadata; |
17 | |
18 | /** |
19 | * Pdf info object class |
20 | * |
21 | * @category Pop |
22 | * @package Pop\Pdf |
23 | * @author Nick Sagona, III <dev@nolainteractive.com> |
24 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
25 | * @license http://www.popphp.org/license New BSD License |
26 | * @version 5.0.0 |
27 | */ |
28 | class InfoObject extends AbstractObject |
29 | { |
30 | |
31 | /** |
32 | * PDF info object index |
33 | * @var ?int |
34 | */ |
35 | protected ?int $index = 3; |
36 | |
37 | /** |
38 | * PDF metadata for the info object |
39 | * @var ?Metadata |
40 | */ |
41 | protected ?Metadata $metadata = null; |
42 | |
43 | /** |
44 | * Constructor |
45 | * |
46 | * Instantiate a PDF info object. |
47 | * |
48 | * @param int $index |
49 | * @param ?Metadata $metadata |
50 | */ |
51 | public function __construct(int $index = 3, \Pop\Pdf\Document\Metadata $metadata = null) |
52 | { |
53 | $this->setIndex($index); |
54 | $this->setData("[{info_index}] 0 obj\n<</Creator([{creator}])/CreationDate([{creation_date}])/ModDate" . |
55 | "([{mod_date}])/Author([{author}])/Title([{title}])/Subject([{subject}])/Producer([{producer}])>>\nendobj\n"); |
56 | |
57 | if ($metadata !== null) { |
58 | $this->setMetadata($metadata); |
59 | } |
60 | } |
61 | |
62 | /** |
63 | * Parse a info object from a string |
64 | * |
65 | * @param string $stream |
66 | * @return InfoObject |
67 | */ |
68 | public static function parse(string $stream): InfoObject |
69 | { |
70 | $info = new self(); |
71 | $info->setIndex(substr($stream, 0, strpos($stream, ' '))); |
72 | $stream = str_replace($info->getIndex() . ' 0 obj', '[{info_index}] 0 obj', $stream); |
73 | |
74 | // Determine the Creator |
75 | if (str_contains($stream, '/Creator')) { |
76 | $creator = substr($stream, strpos($stream, '/Creator')); |
77 | $creator = substr($creator, strpos($creator, '(')); |
78 | $creator = substr($creator, 0, strpos($creator, ')')); |
79 | $creator = str_replace('(', '', $creator); |
80 | $stream = str_replace('Creator(' . $creator . ')', 'Creator([{creator}])', $stream); |
81 | $info->getMetadata()->setCreator($creator); |
82 | } else { |
83 | $stream = str_replace('>>', '/Creator([{creator}])>>', $stream); |
84 | } |
85 | |
86 | // Determine the CreationDate |
87 | if (str_contains($stream, '/CreationDate')) { |
88 | $creationDate = substr($stream, strpos($stream, '/CreationDate')); |
89 | $creationDate = substr($creationDate, strpos($creationDate, '(')); |
90 | $creationDate = substr($creationDate, 0, strpos($creationDate, ')')); |
91 | $creationDate = str_replace('(', '', $creationDate); |
92 | $stream = str_replace('CreationDate(' . $creationDate . ')', 'CreationDate([{creation_date}])', $stream); |
93 | $info->getMetadata()->setCreationDate($creationDate); |
94 | } else { |
95 | $stream = str_replace('>>', '/CreationDate([{creation_date}])>>', $stream); |
96 | } |
97 | |
98 | // Determine the ModDate |
99 | if (str_contains($stream, '/ModDate')) { |
100 | $modDate = substr($stream, strpos($stream, '/ModDate')); |
101 | $modDate = substr($modDate, strpos($modDate, '(')); |
102 | $modDate = substr($modDate, 0, strpos($modDate, ')')); |
103 | $modDate = str_replace('(', '', $modDate); |
104 | $stream = str_replace('ModDate(' . $modDate . ')', 'ModDate([{mod_date}])', $stream); |
105 | $info->getMetadata()->setModDate($modDate); |
106 | } else { |
107 | $stream = str_replace('>>', '/ModDate([{mod_date}])>>', $stream); |
108 | } |
109 | |
110 | // Determine the Author |
111 | if (str_contains($stream, '/Author')) { |
112 | $author = substr($stream, strpos($stream, '/Author')); |
113 | $author = substr($author, strpos($author, '(')); |
114 | $author = substr($author, 0, strpos($author, ')')); |
115 | $author = str_replace('(', '', $author); |
116 | $stream = str_replace('Author(' . $author . ')', 'Author([{author}])', $stream); |
117 | $info->getMetadata()->setAuthor($author); |
118 | } else { |
119 | $stream = str_replace('>>', '/Author([{author}])>>', $stream); |
120 | } |
121 | |
122 | // Determine the Title |
123 | if (str_contains($stream, '/Title')) { |
124 | $title = substr($stream, strpos($stream, '/Title')); |
125 | $title = substr($title, strpos($title, '(')); |
126 | $title = substr($title, 0, strpos($title, ')')); |
127 | $title = str_replace('(', '', $title); |
128 | $stream = str_replace('Title(' . $title . ')', 'Title([{title}])', $stream); |
129 | $info->getMetadata()->setTitle($title); |
130 | } else { |
131 | $stream = str_replace('>>', '/Title([{title}])>>', $stream); |
132 | } |
133 | |
134 | // Determine the Subject |
135 | if (str_contains($stream, '/Subject')) { |
136 | $subject = substr($stream, strpos($stream, '/Subject')); |
137 | $subject = substr($subject, strpos($subject, '(')); |
138 | $subject = substr($subject, 0, strpos($subject, ')')); |
139 | $subject = str_replace('(', '', $subject); |
140 | $stream = str_replace('Subject(' . $subject . ')', 'Subject([{subject}])', $stream); |
141 | $info->getMetadata()->setSubject($subject); |
142 | } else { |
143 | $stream = str_replace('>>', '/Subject([{subject}])>>', $stream); |
144 | } |
145 | |
146 | // Determine the Producer |
147 | if (str_contains($stream, '/Producer')) { |
148 | $producer = substr($stream, strpos($stream, '/Producer')); |
149 | $producer = substr($producer, strpos($producer, '(')); |
150 | $producer = substr($producer, 0, strpos($producer, ')')); |
151 | $producer = str_replace('(', '', $producer); |
152 | $stream = str_replace('Producer(' . $producer . ')', 'Producer([{producer}])', $stream); |
153 | $info->getMetadata()->setProducer($producer); |
154 | } else { |
155 | $stream = str_replace('>>', '/Producer([{producer}])>>', $stream); |
156 | } |
157 | |
158 | $info->setData($stream); |
159 | return $info; |
160 | } |
161 | |
162 | /** |
163 | * Set the info object metadata |
164 | * |
165 | * @param Metadata $metadata |
166 | * @return InfoObject |
167 | */ |
168 | public function setMetadata(Metadata $metadata): InfoObject |
169 | { |
170 | $this->metadata = $metadata; |
171 | return $this; |
172 | } |
173 | |
174 | /** |
175 | * Get the info object metadata |
176 | * |
177 | * @return ?Metadata |
178 | */ |
179 | public function getMetadata(): ?Metadata |
180 | { |
181 | if ($this->metadata === null) { |
182 | $this->metadata = new Metadata(); |
183 | } |
184 | return $this->metadata; |
185 | } |
186 | |
187 | /** |
188 | * Method to print the PDF info object. |
189 | * |
190 | * @return string |
191 | */ |
192 | public function __toString(): string |
193 | { |
194 | if ($this->metadata === null) { |
195 | $this->metadata = new \Pop\Pdf\Document\Metadata(); |
196 | } |
197 | |
198 | // Set the CreationDate and the ModDate if they are null. |
199 | if ($this->metadata->getCreationDate() === null) { |
200 | $this->metadata->setCreationDate(date('D, M j, Y h:i A')); |
201 | } |
202 | if ($this->metadata->getModDate() === null) { |
203 | $this->metadata->setModDate(date('D, M j, Y h:i A')); |
204 | } |
205 | |
206 | return str_replace( |
207 | [ |
208 | '[{info_index}]', '[{title}]', '[{subject}]', '[{author}]', |
209 | '[{creator}]', '[{producer}]', '[{mod_date}]', '[{creation_date}]' |
210 | ], |
211 | [ |
212 | $this->index, $this->metadata->getTitle(), $this->metadata->getSubject(), $this->metadata->getAuthor(), |
213 | $this->metadata->getCreator(), $this->metadata->getProducer(), $this->metadata->getModDate(), $this->metadata->getCreationDate() |
214 | ], |
215 | $this->data |
216 | ); |
217 | } |
218 | |
219 | } |