Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.46% |
23 / 26 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
| RootObject | |
88.46% |
23 / 26 |
|
87.50% |
7 / 8 |
9.12 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| parse | |
76.92% |
10 / 13 |
|
0.00% |
0 / 1 |
2.05 | |||
| setVersion | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setParentIndex | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setFormReferences | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getParentIndex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Pdf\Build\PdfObject; |
| 15 | |
| 16 | /** |
| 17 | * Pdf root object class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\Pdf |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 23 | * @license https://www.popphp.org/license New BSD License |
| 24 | * @version 5.2.7 |
| 25 | */ |
| 26 | class RootObject extends AbstractObject |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * PDF version |
| 31 | * @var float |
| 32 | */ |
| 33 | protected float $version = 1.7; |
| 34 | |
| 35 | /** |
| 36 | * PDF root object index |
| 37 | * @var ?int |
| 38 | */ |
| 39 | protected ?int $index = 1; |
| 40 | |
| 41 | /** |
| 42 | * PDF root parent index |
| 43 | * @var int |
| 44 | */ |
| 45 | protected int $parent = 2; |
| 46 | |
| 47 | /** |
| 48 | * Constructor |
| 49 | * |
| 50 | * Instantiate a PDF root object. |
| 51 | * |
| 52 | * @param int $index |
| 53 | */ |
| 54 | public function __construct(int $index = 1) |
| 55 | { |
| 56 | $this->setIndex($index); |
| 57 | $this->setData("[{root_index}] 0 obj\n<<[{form_index}]/Pages [{parent_index}] 0 R/Type/Catalog>>\nendobj\n"); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Parse a root object from a string |
| 62 | * |
| 63 | * @param string $stream |
| 64 | * @return RootObject |
| 65 | */ |
| 66 | public static function parse(string $stream): RootObject |
| 67 | { |
| 68 | $root = new self(); |
| 69 | |
| 70 | // Else, parse out any metadata and determine the root and parent object indices. |
| 71 | $root->setIndex(substr($stream, 0, strpos($stream, ' '))); |
| 72 | $stream = str_replace($root->getIndex() . ' 0 obj', '[{root_index}] 0 obj', $stream); |
| 73 | |
| 74 | // Strip away any metadata |
| 75 | if (str_contains($stream, '/Metadata')) { |
| 76 | $metadata = substr($stream, strpos($stream, 'Metadata')); |
| 77 | $metadata = '/' . substr($metadata, 0, strpos($metadata, '/')); |
| 78 | $stream = str_replace($metadata, '', $stream); |
| 79 | } |
| 80 | |
| 81 | // Determine the parent index. |
| 82 | $parent = substr($stream, (strpos($stream, '/Pages') + 6)); |
| 83 | $parent = trim(substr($parent, 0, strpos($parent, '0 R'))); |
| 84 | $root->setParentIndex($parent); |
| 85 | $stream = str_replace('Pages ' . $root->getParentIndex() . ' 0 R', 'Pages [{parent_index}] 0 R', $stream); |
| 86 | |
| 87 | // Set the root object parent index and the data. |
| 88 | $root->setData($stream . "\n"); |
| 89 | |
| 90 | return $root; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Set the root object version |
| 95 | * |
| 96 | * @param float $version |
| 97 | * @return RootObject |
| 98 | */ |
| 99 | public function setVersion(float $version): RootObject |
| 100 | { |
| 101 | $this->version = $version; |
| 102 | return $this; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Set the root object parent index |
| 107 | * |
| 108 | * @param int $p |
| 109 | * @return RootObject |
| 110 | */ |
| 111 | public function setParentIndex(int $p): RootObject |
| 112 | { |
| 113 | $this->parent = $p; |
| 114 | return $this; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Set the root object form index |
| 119 | * |
| 120 | * @param string $forms |
| 121 | * @return RootObject |
| 122 | */ |
| 123 | public function setFormReferences(string $forms): RootObject |
| 124 | { |
| 125 | $data = str_replace('[{form_index}]', '/AcroForm [' . $forms . ']', $this->data); |
| 126 | $this->setData($data); |
| 127 | return $this; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the root object version |
| 132 | * |
| 133 | * @return float |
| 134 | */ |
| 135 | public function getVersion(): float |
| 136 | { |
| 137 | return $this->version; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get the root object parent index |
| 142 | * |
| 143 | * @return int |
| 144 | */ |
| 145 | public function getParentIndex(): int |
| 146 | { |
| 147 | return $this->parent; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Method to print the root object. |
| 152 | * |
| 153 | * @return string |
| 154 | */ |
| 155 | public function __toString(): string |
| 156 | { |
| 157 | return '%PDF-' . $this->version . "\n" . |
| 158 | str_replace(['[{root_index}]', '[{parent_index}]', '[{form_index}]'], [$this->index, $this->parent, ''], $this->data); |
| 159 | } |
| 160 | |
| 161 | } |