Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.37% |
38 / 43 |
|
90.32% |
28 / 31 |
CRAP | |
0.00% |
0 / 1 |
AbstractDocument | |
88.37% |
38 / 43 |
|
90.32% |
28 / 31 |
38.04 | |
0.00% |
0 / 1 |
setVersion | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setMetadata | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setOrigin | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOrigin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMetadata | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
hasPages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFonts | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFont | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
hasFonts | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAvailableFonts | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isFontAvailable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasFont | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getStyles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getStyle | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
hasStyles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAvailableStyles | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isStyleAvailable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasStyle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCurrentPage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNumberOfPages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCurrentFont | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNumberOfFonts | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getForms | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getForm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
hasForms | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addForm | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setCompression | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isCompressed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
addPage | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
addPages | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
createPage | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
copyPage | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
orderPages | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
deletePage | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
addFont | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
addFonts | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
embedFont | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
embedFonts | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
createStyle | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
addStyle | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
addStyles | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
setCurrentPage | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
setCurrentFont | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
__toString | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
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\Document; |
15 | |
16 | /** |
17 | * Abstract Pdf document class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Pdf |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 5.0.0 |
25 | */ |
26 | abstract class AbstractDocument implements DocumentInterface |
27 | { |
28 | |
29 | /** |
30 | * Document origin constants |
31 | */ |
32 | const ORIGIN_TOP_LEFT = 'ORIGIN_TOP_LEFT'; |
33 | const ORIGIN_TOP_RIGHT = 'ORIGIN_TOP_RIGHT'; |
34 | const ORIGIN_BOTTOM_LEFT = 'ORIGIN_BOTTOM_LEFT'; |
35 | const ORIGIN_BOTTOM_RIGHT = 'ORIGIN_BOTTOM_RIGHT'; |
36 | const ORIGIN_CENTER = 'ORIGIN_CENTER'; |
37 | |
38 | /** |
39 | * PDF version |
40 | * @var float |
41 | */ |
42 | protected float $version = 1.7; |
43 | |
44 | /** |
45 | * PDF metadata for the info object |
46 | * @var ?Metadata |
47 | */ |
48 | protected ?Metadata $metadata = null; |
49 | |
50 | /** |
51 | * Pages array |
52 | * @var array |
53 | */ |
54 | protected array $pages = []; |
55 | |
56 | /** |
57 | * Fonts array |
58 | * @var array |
59 | */ |
60 | protected array $fonts = []; |
61 | |
62 | /** |
63 | * Styles array |
64 | * @var array |
65 | */ |
66 | protected array $styles = []; |
67 | |
68 | /** |
69 | * Forms array |
70 | * @var array |
71 | */ |
72 | protected array $forms = []; |
73 | |
74 | /** |
75 | * Current page |
76 | * @var ?int |
77 | */ |
78 | protected ?int $currentPage = null; |
79 | |
80 | /** |
81 | * Current font |
82 | * @var ?string |
83 | */ |
84 | protected ?string $currentFont = null; |
85 | |
86 | /** |
87 | * Compression property |
88 | * @var bool |
89 | */ |
90 | protected bool$compression = false; |
91 | |
92 | /** |
93 | * Document origin |
94 | * @var string |
95 | */ |
96 | protected string $origin = 'ORIGIN_BOTTOM_LEFT'; |
97 | |
98 | /** |
99 | * Set the document version |
100 | * |
101 | * @param float $version |
102 | * @return AbstractDocument |
103 | */ |
104 | public function setVersion(float $version): AbstractDocument |
105 | { |
106 | $this->version = $version; |
107 | return $this; |
108 | } |
109 | |
110 | /** |
111 | * Set the document metadata |
112 | * |
113 | * @param Metadata $metadata |
114 | * @return AbstractDocument |
115 | */ |
116 | public function setMetadata(Metadata $metadata): AbstractDocument |
117 | { |
118 | $this->metadata = $metadata; |
119 | return $this; |
120 | } |
121 | |
122 | /** |
123 | * Set the document origin |
124 | * |
125 | * @param string $origin |
126 | * @return AbstractDocument |
127 | */ |
128 | public function setOrigin(string $origin): AbstractDocument |
129 | { |
130 | if (defined('Pop\Pdf\Document\AbstractDocument::' . $origin)) { |
131 | $this->origin = $origin; |
132 | } |
133 | return $this; |
134 | } |
135 | |
136 | /** |
137 | * Get the document version |
138 | * |
139 | * @return float |
140 | */ |
141 | public function getVersion(): float |
142 | { |
143 | return $this->version; |
144 | } |
145 | |
146 | /** |
147 | * Get the document origin |
148 | * |
149 | * @return string |
150 | */ |
151 | public function getOrigin(): string |
152 | { |
153 | return $this->origin; |
154 | } |
155 | |
156 | /** |
157 | * Get the document metadata |
158 | * |
159 | * @return ?Metadata |
160 | */ |
161 | public function getMetadata(): ?Metadata |
162 | { |
163 | return $this->metadata; |
164 | } |
165 | |
166 | /** |
167 | * Get the PDF page objects array |
168 | * |
169 | * @return array |
170 | */ |
171 | public function getPages(): array |
172 | { |
173 | return $this->pages; |
174 | } |
175 | |
176 | /** |
177 | * Get a PDF page object |
178 | * |
179 | * @param int $p |
180 | * @throws \Pop\Pdf\Exception |
181 | * @return Page |
182 | */ |
183 | public function getPage(int $p): Page |
184 | { |
185 | if (!isset($this->pages[$p - 1])) { |
186 | throw new \Pop\Pdf\Exception('Error: That page (' . $p . ') does not exist.'); |
187 | } |
188 | return $this->pages[$p - 1]; |
189 | } |
190 | |
191 | /** |
192 | * Determine if the document has page objects |
193 | * |
194 | * @return bool |
195 | */ |
196 | public function hasPages(): bool |
197 | { |
198 | return (count($this->pages) > 0); |
199 | } |
200 | |
201 | /** |
202 | * Get the PDF font objects array |
203 | * |
204 | * @return array |
205 | */ |
206 | public function getFonts(): array |
207 | { |
208 | return $this->fonts; |
209 | } |
210 | |
211 | /** |
212 | * Get a PDF font object |
213 | * |
214 | * @param string $name |
215 | * @throws \Pop\Pdf\Exception |
216 | * @return Font |
217 | */ |
218 | public function getFont(string $name): Font |
219 | { |
220 | if (!isset($this->fonts[$name])) { |
221 | throw new \Pop\Pdf\Exception("Error: The font '" . $name . "' has not been added to the PDF document."); |
222 | } |
223 | return $this->fonts[$name]; |
224 | } |
225 | |
226 | /** |
227 | * Determine if the document has font objects |
228 | * |
229 | * @return bool |
230 | */ |
231 | public function hasFonts(): bool |
232 | { |
233 | return (count($this->fonts) > 0); |
234 | } |
235 | |
236 | /** |
237 | * Get available fonts that have been added to the PDF document |
238 | * |
239 | * @return array |
240 | */ |
241 | public function getAvailableFonts(): array |
242 | { |
243 | return array_keys($this->fonts); |
244 | } |
245 | |
246 | /** |
247 | * Determine if a font has been added to the PDF document |
248 | * |
249 | * @param string $font |
250 | * @return bool |
251 | */ |
252 | public function isFontAvailable(string $font): bool |
253 | { |
254 | return array_key_exists($font, $this->fonts); |
255 | } |
256 | |
257 | /** |
258 | * Determine if a font has been added to the PDF document (alias) |
259 | * |
260 | * @param string $font |
261 | * @return bool |
262 | */ |
263 | public function hasFont(string $font): bool |
264 | { |
265 | return array_key_exists($font, $this->fonts); |
266 | } |
267 | |
268 | /** |
269 | * Get the PDF style objects array |
270 | * |
271 | * @return array |
272 | */ |
273 | public function getStyles(): array |
274 | { |
275 | return $this->styles; |
276 | } |
277 | |
278 | /** |
279 | * Get a PDF style object |
280 | * |
281 | * @param string $name |
282 | * @throws \Pop\Pdf\Exception |
283 | * @return Style |
284 | */ |
285 | public function getStyle(string $name): Style |
286 | { |
287 | if (!isset($this->styles[$name])) { |
288 | throw new \Pop\Pdf\Exception("Error: The style '" . $name . "' has not been added to the PDF document."); |
289 | } |
290 | return $this->styles[$name]; |
291 | } |
292 | |
293 | /** |
294 | * Determine if the document has style objects |
295 | * |
296 | * @return bool |
297 | */ |
298 | public function hasStyles(): bool |
299 | { |
300 | return (count($this->styles) > 0); |
301 | } |
302 | |
303 | /** |
304 | * Get available styles that have been added to the PDF document |
305 | * |
306 | * @return array |
307 | */ |
308 | public function getAvailableStyles(): array |
309 | { |
310 | return array_keys($this->styles); |
311 | } |
312 | |
313 | /** |
314 | * Determine if a style has been added to the PDF document |
315 | * |
316 | * @param string $style |
317 | * @return bool |
318 | */ |
319 | public function isStyleAvailable(string $style): bool |
320 | { |
321 | return array_key_exists($style, $this->styles); |
322 | } |
323 | |
324 | /** |
325 | * Determine if a style has been added to the PDF document (alias) |
326 | * |
327 | * @param string $style |
328 | * @return bool |
329 | */ |
330 | public function hasStyle(string $style): bool |
331 | { |
332 | return array_key_exists($style, $this->styles); |
333 | } |
334 | |
335 | /** |
336 | * Get the current page number |
337 | * |
338 | * @return ?int |
339 | */ |
340 | public function getCurrentPage(): ?int |
341 | { |
342 | return $this->currentPage; |
343 | } |
344 | |
345 | /** |
346 | * Get the current number of pages |
347 | * |
348 | * @return int |
349 | */ |
350 | public function getNumberOfPages(): int |
351 | { |
352 | return count($this->pages); |
353 | } |
354 | |
355 | /** |
356 | * Get the current font |
357 | * |
358 | * @return ?string |
359 | */ |
360 | public function getCurrentFont(): ?string |
361 | { |
362 | return $this->currentFont; |
363 | } |
364 | |
365 | /** |
366 | * Get the current number of fonts |
367 | * |
368 | * @return int |
369 | */ |
370 | public function getNumberOfFonts(): int |
371 | { |
372 | return count($this->fonts); |
373 | } |
374 | |
375 | /** |
376 | * Get form objects |
377 | * |
378 | * @return array |
379 | */ |
380 | public function getForms(): array |
381 | { |
382 | return $this->forms; |
383 | } |
384 | |
385 | /** |
386 | * Get form objects |
387 | * |
388 | * @param string $name |
389 | * @return ?Form |
390 | */ |
391 | public function getForm(string $name): ?Form |
392 | { |
393 | return (isset($this->forms[$name])) ? $this->forms[$name] : null; |
394 | } |
395 | |
396 | /** |
397 | * Determine if the document has form objects |
398 | * |
399 | * @return bool |
400 | */ |
401 | public function hasForms(): bool |
402 | { |
403 | return (count($this->forms) > 0); |
404 | } |
405 | |
406 | /** |
407 | * Add form |
408 | * |
409 | * @param Form $form |
410 | * @return AbstractDocument |
411 | */ |
412 | public function addForm(Form $form): AbstractDocument |
413 | { |
414 | $this->forms[$form->getName()] = $form; |
415 | return $this; |
416 | } |
417 | |
418 | /** |
419 | * Set the compression |
420 | * |
421 | * @param bool $compression |
422 | * @return AbstractDocument |
423 | */ |
424 | public function setCompression(bool $compression): AbstractDocument |
425 | { |
426 | $this->compression = $compression; |
427 | return $this; |
428 | } |
429 | |
430 | /** |
431 | * Determine whether the PDF is compressed or not |
432 | * |
433 | * @return bool |
434 | */ |
435 | public function isCompressed(): bool |
436 | { |
437 | return $this->compression; |
438 | } |
439 | |
440 | /** |
441 | * Constructor |
442 | * |
443 | * Instantiate a PDF document |
444 | * |
445 | * @param ?Page $page |
446 | * @param ?Metadata $metadata |
447 | * @return AbstractDocument |
448 | */ |
449 | abstract public function __construct(?Page $page = null, ?Metadata $metadata = null); |
450 | |
451 | /** |
452 | * Add a page to the PDF document |
453 | * |
454 | * @param Page $page |
455 | * @return AbstractDocument |
456 | */ |
457 | abstract public function addPage(Page $page): AbstractDocument; |
458 | |
459 | /** |
460 | * Add pages to the PDF document |
461 | * |
462 | * @param array $pages |
463 | * @return AbstractDocument |
464 | */ |
465 | abstract public function addPages(array $pages): AbstractDocument; |
466 | |
467 | /** |
468 | * Create and return a new page object, adding it to the PDF document |
469 | * |
470 | * @param mixed $size |
471 | * @param ?int $height |
472 | * @return Page |
473 | */ |
474 | abstract public function createPage(mixed $size, ?int $height = null): Page; |
475 | |
476 | /** |
477 | * Copy and return a page of the PDF, adding it to the PDF document |
478 | * |
479 | * @param int $p |
480 | * @throws Exception |
481 | * @return Page |
482 | */ |
483 | abstract public function copyPage(int $p): Page; |
484 | |
485 | /** |
486 | * Order the pages |
487 | * |
488 | * @param array $pages |
489 | * @throws Exception |
490 | * @return AbstractDocument |
491 | */ |
492 | abstract public function orderPages(array $pages): AbstractDocument; |
493 | |
494 | /** |
495 | * Delete a page from the PDF document |
496 | * |
497 | * @param int $p |
498 | * @throws Exception |
499 | * @return AbstractDocument |
500 | */ |
501 | abstract public function deletePage(int $p): AbstractDocument; |
502 | |
503 | /** |
504 | * Add a font |
505 | * |
506 | * @param Font $font |
507 | * @throws Exception |
508 | * @return AbstractDocument |
509 | */ |
510 | abstract public function addFont(Font $font): AbstractDocument; |
511 | |
512 | /** |
513 | * Add fonts |
514 | * |
515 | * @param array $fonts |
516 | * @throws Exception |
517 | * @return AbstractDocument |
518 | */ |
519 | abstract public function addFonts(array $fonts): AbstractDocument; |
520 | |
521 | /** |
522 | * Embed a font |
523 | * |
524 | * @param Font $font |
525 | * @param bool $embedOverride |
526 | * @throws Exception |
527 | * @return AbstractDocument |
528 | */ |
529 | abstract public function embedFont(Font $font, bool $embedOverride = false): AbstractDocument; |
530 | |
531 | /** |
532 | * Embed fonts |
533 | * |
534 | * @param array $fonts |
535 | * @param bool $embedOverride |
536 | * @throws Exception |
537 | * @return AbstractDocument |
538 | */ |
539 | abstract public function embedFonts(array $fonts, bool $embedOverride = false): AbstractDocument; |
540 | |
541 | /** |
542 | * Create style |
543 | * |
544 | * @param Style|string $style |
545 | * @return AbstractDocument |
546 | */ |
547 | abstract public function createStyle(Style|string $style, ?string $font = null, int|float|null $size = null): AbstractDocument; |
548 | |
549 | /** |
550 | * Add a style |
551 | * |
552 | * @param Style|string $style |
553 | * @return AbstractDocument |
554 | */ |
555 | abstract public function addStyle(Style|string $style): AbstractDocument; |
556 | |
557 | /** |
558 | * Add styles |
559 | * |
560 | * @param array $styles |
561 | * @return AbstractDocument |
562 | */ |
563 | abstract public function addStyles(array $styles): AbstractDocument; |
564 | |
565 | /** |
566 | * Set the current page of the PDF document |
567 | * |
568 | * @param int $p |
569 | * @throws Exception |
570 | * @return AbstractDocument |
571 | */ |
572 | abstract public function setCurrentPage(int $p): AbstractDocument; |
573 | |
574 | /** |
575 | * Set the current font of the PDF document |
576 | * |
577 | * @param string $name |
578 | * @throws Exception |
579 | * @return AbstractDocument |
580 | */ |
581 | abstract public function setCurrentFont(string $name): AbstractDocument; |
582 | |
583 | /** |
584 | * Output the PDF document to string |
585 | * |
586 | * @return string |
587 | */ |
588 | abstract public function __toString(): string; |
589 | |
590 | } |