PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.6.1
MxChat – AI Chatbot & Content Generation for WordPress v2.6.1
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
mxchat-basic / includes / pdf-parser / src / Smalot / PdfParser / Header.php

Header.php in MxChat – AI Chatbot & Content Generation for WordPress 2.6.1, at includes/pdf-parser/src/Smalot/PdfParser/Header.php

195 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @file
5 * This file is part of the PdfParser library.
6 *
7 * @author Sébastien MALOT <sebastien@malot.fr>
8 *
9 * @date 2017-01-03
10 *
11 * @license LGPLv3
12 *
13 * @url <https://github.com/smalot/pdfparser>
14 *
15 * PdfParser is a pdf library written in PHP, extraction oriented.
16 * Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
17 *
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU Lesser General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public License
29 * along with this program.
30 * If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
31 */
32
33 namespace Smalot\PdfParser;
34
35 use Smalot\PdfParser\Element\ElementArray;
36 use Smalot\PdfParser\Element\ElementMissing;
37 use Smalot\PdfParser\Element\ElementStruct;
38 use Smalot\PdfParser\Element\ElementXRef;
39
40 /**
41 * Class Header
42 */
43 class Header
44 {
45 /**
46 * @var Document|null
47 */
48 protected $document;
49
50 /**
51 * @var Element[]
52 */
53 protected $elements;
54
55 /**
56 * @param Element[] $elements list of elements
57 * @param Document $document document
58 */
59 public function __construct(array $elements = [], ?Document $document = null)
60 {
61 $this->elements = $elements;
62 $this->document = $document;
63 }
64
65 public function init()
66 {
67 foreach ($this->elements as $element) {
68 if ($element instanceof Element) {
69 $element->init();
70 }
71 }
72 }
73
74 /**
75 * Returns all elements.
76 */
77 public function getElements()
78 {
79 foreach ($this->elements as $name => $element) {
80 $this->resolveXRef($name);
81 }
82
83 return $this->elements;
84 }
85
86 /**
87 * Used only for debug.
88 */
89 public function getElementTypes(): array
90 {
91 $types = [];
92
93 foreach ($this->elements as $key => $element) {
94 $types[$key] = \get_class($element);
95 }
96
97 return $types;
98 }
99
100 public function getDetails(bool $deep = true): array
101 {
102 $values = [];
103 $elements = $this->getElements();
104
105 foreach ($elements as $key => $element) {
106 if ($element instanceof self && $deep) {
107 $values[$key] = $element->getDetails($deep);
108 } elseif ($element instanceof PDFObject && $deep) {
109 $values[$key] = $element->getDetails(false);
110 } elseif ($element instanceof ElementArray) {
111 if ($deep) {
112 $values[$key] = $element->getDetails();
113 }
114 } elseif ($element instanceof Element) {
115 $values[$key] = (string) $element;
116 }
117 }
118
119 return $values;
120 }
121
122 /**
123 * Indicate if an element name is available in header.
124 *
125 * @param string $name the name of the element
126 */
127 public function has(string $name): bool
128 {
129 return \array_key_exists($name, $this->elements);
130 }
131
132 /**
133 * @return Element|PDFObject
134 */
135 public function get(string $name)
136 {
137 if (\array_key_exists($name, $this->elements) && $element = $this->resolveXRef($name)) {
138 return $element;
139 }
140
141 return new ElementMissing();
142 }
143
144 /**
145 * Resolve XRef to object.
146 *
147 * @return Element|PDFObject
148 *
149 * @throws \Exception
150 */
151 protected function resolveXRef(string $name)
152 {
153 if (($obj = $this->elements[$name]) instanceof ElementXRef && null !== $this->document) {
154 /** @var ElementXRef $obj */
155 $object = $this->document->getObjectById($obj->getId());
156
157 if (null === $object) {
158 return new ElementMissing();
159 }
160
161 // Update elements list for future calls.
162 $this->elements[$name] = $object;
163 }
164
165 return $this->elements[$name];
166 }
167
168 /**
169 * @param string $content The content to parse
170 * @param Document $document The document
171 * @param int $position The new position of the cursor after parsing
172 */
173 public static function parse(string $content, Document $document, int &$position = 0): self
174 {
175 /* @var Header $header */
176 if ('<<' == substr(trim($content), 0, 2)) {
177 $header = ElementStruct::parse($content, $document, $position);
178 } else {
179 $elements = ElementArray::parse($content, $document, $position);
180 $header = new self([], $document);
181
182 if ($elements) {
183 $header = new self($elements->getRawContent(), null);
184 }
185 }
186
187 if ($header) {
188 return $header;
189 }
190
191 // Build an empty header.
192 return new self([], $document);
193 }
194 }
195