Total Complexity | 144 |
Total Lines | 973 |
Duplicated Lines | 0 % |
Changes | 14 | ||
Bugs | 3 | Features | 2 |
Complex classes like Page often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Page, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Page extends PDFObject |
||
41 | { |
||
42 | /** |
||
43 | * @var Font[] |
||
44 | */ |
||
45 | protected $fonts; |
||
46 | |||
47 | /** |
||
48 | * @var PDFObject[] |
||
49 | */ |
||
50 | protected $xobjects; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $dataTm; |
||
56 | |||
57 | /** |
||
58 | * @param array<\Smalot\PdfParser\Font> $fonts |
||
59 | * |
||
60 | * @internal |
||
61 | */ |
||
62 | public function setFonts($fonts) |
||
63 | { |
||
64 | if (empty($this->fonts)) { |
||
65 | $this->fonts = $fonts; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @return Font[] |
||
71 | */ |
||
72 | public function getFonts() |
||
73 | { |
||
74 | if (null !== $this->fonts) { |
||
75 | return $this->fonts; |
||
76 | } |
||
77 | |||
78 | $resources = $this->get('Resources'); |
||
79 | |||
80 | if (method_exists($resources, 'has') && $resources->has('Font')) { |
||
81 | if ($resources->get('Font') instanceof ElementMissing) { |
||
|
|||
82 | return []; |
||
83 | } |
||
84 | |||
85 | if ($resources->get('Font') instanceof Header) { |
||
86 | $fonts = $resources->get('Font')->getElements(); |
||
87 | } else { |
||
88 | $fonts = $resources->get('Font')->getHeader()->getElements(); |
||
89 | } |
||
90 | |||
91 | $table = []; |
||
92 | |||
93 | foreach ($fonts as $id => $font) { |
||
94 | if ($font instanceof Font) { |
||
95 | $table[$id] = $font; |
||
96 | |||
97 | // Store too on cleaned id value (only numeric) |
||
98 | $id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
99 | if ('' != $id) { |
||
100 | $table[$id] = $font; |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | return $this->fonts = $table; |
||
106 | } |
||
107 | |||
108 | return []; |
||
109 | } |
||
110 | |||
111 | public function getFont(string $id): ?Font |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Support for XObject |
||
137 | * |
||
138 | * @return PDFObject[] |
||
139 | */ |
||
140 | public function getXObjects() |
||
141 | { |
||
142 | if (null !== $this->xobjects) { |
||
143 | return $this->xobjects; |
||
144 | } |
||
145 | |||
146 | $resources = $this->get('Resources'); |
||
147 | |||
148 | if (method_exists($resources, 'has') && $resources->has('XObject')) { |
||
149 | if ($resources->get('XObject') instanceof Header) { |
||
150 | $xobjects = $resources->get('XObject')->getElements(); |
||
151 | } else { |
||
152 | $xobjects = $resources->get('XObject')->getHeader()->getElements(); |
||
153 | } |
||
154 | |||
155 | $table = []; |
||
156 | |||
157 | foreach ($xobjects as $id => $xobject) { |
||
158 | $table[$id] = $xobject; |
||
159 | |||
160 | // Store too on cleaned id value (only numeric) |
||
161 | $id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
162 | if ('' != $id) { |
||
163 | $table[$id] = $xobject; |
||
164 | } |
||
165 | } |
||
166 | |||
167 | return $this->xobjects = $table; |
||
168 | } |
||
169 | |||
170 | return []; |
||
171 | } |
||
172 | |||
173 | public function getXObject(string $id): ?PDFObject |
||
174 | { |
||
175 | $xobjects = $this->getXObjects(); |
||
176 | |||
177 | if (isset($xobjects[$id])) { |
||
178 | return $xobjects[$id]; |
||
179 | } |
||
180 | |||
181 | return null; |
||
182 | /*$id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
183 | |||
184 | if (isset($xobjects[$id])) { |
||
185 | return $xobjects[$id]; |
||
186 | } else { |
||
187 | return null; |
||
188 | }*/ |
||
189 | } |
||
190 | |||
191 | public function getText(?self $page = null): string |
||
192 | { |
||
193 | if ($contents = $this->get('Contents')) { |
||
194 | if ($contents instanceof ElementMissing) { |
||
195 | return ''; |
||
196 | } elseif ($contents instanceof ElementNull) { |
||
197 | return ''; |
||
198 | } elseif ($contents instanceof PDFObject) { |
||
199 | $elements = $contents->getHeader()->getElements(); |
||
200 | |||
201 | if (is_numeric(key($elements))) { |
||
202 | $new_content = ''; |
||
203 | |||
204 | foreach ($elements as $element) { |
||
205 | if ($element instanceof ElementXRef) { |
||
206 | $new_content .= $element->getObject()->getContent(); |
||
207 | } else { |
||
208 | $new_content .= $element->getContent(); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | $header = new Header([], $this->document); |
||
213 | $contents = new PDFObject($this->document, $header, $new_content, $this->config); |
||
214 | } |
||
215 | } elseif ($contents instanceof ElementArray) { |
||
216 | // Create a virtual global content. |
||
217 | $new_content = ''; |
||
218 | |||
219 | foreach ($contents->getContent() as $content) { |
||
220 | $new_content .= $content->getContent()."\n"; |
||
221 | } |
||
222 | |||
223 | $header = new Header([], $this->document); |
||
224 | $contents = new PDFObject($this->document, $header, $new_content, $this->config); |
||
225 | } |
||
226 | |||
227 | /* |
||
228 | * Elements referencing each other on the same page can cause endless loops during text parsing. |
||
229 | * To combat this we keep a recursionStack containing already parsed elements on the page. |
||
230 | * The stack is only emptied here after getting text from a page. |
||
231 | */ |
||
232 | $contentsText = $contents->getText($this); |
||
233 | PDFObject::$recursionStack = []; |
||
234 | |||
235 | return $contentsText; |
||
236 | } |
||
237 | |||
238 | return ''; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Return true if the current page is a (setasign\Fpdi\Fpdi) FPDI/FPDF document |
||
243 | * |
||
244 | * The metadata 'Producer' should have the value of "FPDF" . FPDF_VERSION if the |
||
245 | * pdf file was generated by FPDF/Fpfi. |
||
246 | * |
||
247 | * @return bool true is the current page is a FPDI/FPDF document |
||
248 | */ |
||
249 | public function isFpdf(): bool |
||
250 | { |
||
251 | if (\array_key_exists('Producer', $this->document->getDetails()) |
||
252 | && \is_string($this->document->getDetails()['Producer']) |
||
253 | && 0 === strncmp($this->document->getDetails()['Producer'], 'FPDF', 4)) { |
||
254 | return true; |
||
255 | } |
||
256 | |||
257 | return false; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Return the page number of the PDF document of the page object |
||
262 | * |
||
263 | * @return int the page number |
||
264 | */ |
||
265 | public function getPageNumber(): int |
||
266 | { |
||
267 | $pages = $this->document->getPages(); |
||
268 | $numOfPages = \count($pages); |
||
269 | for ($pageNum = 0; $pageNum < $numOfPages; ++$pageNum) { |
||
270 | if ($pages[$pageNum] === $this) { |
||
271 | break; |
||
272 | } |
||
273 | } |
||
274 | |||
275 | return $pageNum; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Return the Object of the page if the document is a FPDF/FPDI document |
||
280 | * |
||
281 | * If the document was generated by FPDF/FPDI it returns the |
||
282 | * PDFObject of the given page |
||
283 | * |
||
284 | * @return PDFObject The PDFObject for the page |
||
285 | */ |
||
286 | public function getPDFObjectForFpdf(): PDFObject |
||
287 | { |
||
288 | $pageNum = $this->getPageNumber(); |
||
289 | $xObjects = $this->getXObjects(); |
||
290 | |||
291 | return $xObjects[$pageNum]; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Return a new PDFObject of the document created with FPDF/FPDI |
||
296 | * |
||
297 | * For a document generated by FPDF/FPDI, it generates a |
||
298 | * new PDFObject for that document |
||
299 | * |
||
300 | * @return PDFObject The PDFObject |
||
301 | */ |
||
302 | public function createPDFObjectForFpdf(): PDFObject |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Return page if document is a FPDF/FPDI document |
||
314 | * |
||
315 | * @return Page The page |
||
316 | */ |
||
317 | public function createPageForFpdf(): self |
||
318 | { |
||
319 | $pdfObject = $this->getPDFObjectForFpdf(); |
||
320 | $new_content = $pdfObject->getContent(); |
||
321 | $header = $pdfObject->getHeader(); |
||
322 | $config = $pdfObject->config; |
||
323 | |||
324 | return new self($pdfObject->document, $header, $new_content, $config); |
||
325 | } |
||
326 | |||
327 | public function getTextArray(?self $page = null): array |
||
328 | { |
||
329 | if ($this->isFpdf()) { |
||
330 | $pdfObject = $this->getPDFObjectForFpdf(); |
||
331 | $newPdfObject = $this->createPDFObjectForFpdf(); |
||
332 | |||
333 | return $newPdfObject->getTextArray($pdfObject); |
||
334 | } else { |
||
335 | if ($contents = $this->get('Contents')) { |
||
336 | if ($contents instanceof ElementMissing) { |
||
337 | return []; |
||
338 | } elseif ($contents instanceof ElementNull) { |
||
339 | return []; |
||
340 | } elseif ($contents instanceof PDFObject) { |
||
341 | $elements = $contents->getHeader()->getElements(); |
||
342 | |||
343 | if (is_numeric(key($elements))) { |
||
344 | $new_content = ''; |
||
345 | |||
346 | /** @var PDFObject $element */ |
||
347 | foreach ($elements as $element) { |
||
348 | if ($element instanceof ElementXRef) { |
||
349 | $new_content .= $element->getObject()->getContent(); |
||
350 | } else { |
||
351 | $new_content .= $element->getContent(); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | $header = new Header([], $this->document); |
||
356 | $contents = new PDFObject($this->document, $header, $new_content, $this->config); |
||
357 | } else { |
||
358 | try { |
||
359 | $contents->getTextArray($this); |
||
360 | } catch (\Throwable $e) { |
||
361 | return $contents->getTextArray(); |
||
362 | } |
||
363 | } |
||
364 | } elseif ($contents instanceof ElementArray) { |
||
365 | // Create a virtual global content. |
||
366 | $new_content = ''; |
||
367 | |||
368 | /** @var PDFObject $content */ |
||
369 | foreach ($contents->getContent() as $content) { |
||
370 | $new_content .= $content->getContent()."\n"; |
||
371 | } |
||
372 | |||
373 | $header = new Header([], $this->document); |
||
374 | $contents = new PDFObject($this->document, $header, $new_content, $this->config); |
||
375 | } |
||
376 | |||
377 | return $contents->getTextArray($this); |
||
378 | } |
||
379 | |||
380 | return []; |
||
381 | } |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Gets all the text data with its internal representation of the page. |
||
386 | * |
||
387 | * Returns an array with the data and the internal representation |
||
388 | */ |
||
389 | public function extractRawData(): array |
||
390 | { |
||
391 | /* |
||
392 | * Now you can get the complete content of the object with the text on it |
||
393 | */ |
||
394 | $extractedData = []; |
||
395 | $content = $this->get('Contents'); |
||
396 | $values = $content->getContent(); |
||
397 | if (isset($values) && \is_array($values)) { |
||
398 | $text = ''; |
||
399 | foreach ($values as $section) { |
||
400 | $text .= $section->getContent(); |
||
401 | } |
||
402 | $sectionsText = $this->getSectionsText($text); |
||
403 | foreach ($sectionsText as $sectionText) { |
||
404 | $commandsText = $this->getCommandsText($sectionText); |
||
405 | foreach ($commandsText as $command) { |
||
406 | $extractedData[] = $command; |
||
407 | } |
||
408 | } |
||
409 | } else { |
||
410 | if ($this->isFpdf()) { |
||
411 | $content = $this->getPDFObjectForFpdf(); |
||
412 | } |
||
413 | $sectionsText = $content->getSectionsText($content->getContent()); |
||
414 | foreach ($sectionsText as $sectionText) { |
||
415 | $commandsText = $content->getCommandsText($sectionText); |
||
416 | foreach ($commandsText as $command) { |
||
417 | $extractedData[] = $command; |
||
418 | } |
||
419 | } |
||
420 | } |
||
421 | |||
422 | return $extractedData; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Gets all the decoded text data with it internal representation from a page. |
||
427 | * |
||
428 | * @param array $extractedRawData the extracted data return by extractRawData or |
||
429 | * null if extractRawData should be called |
||
430 | * |
||
431 | * @return array An array with the data and the internal representation |
||
432 | */ |
||
433 | public function extractDecodedRawData(?array $extractedRawData = null): array |
||
434 | { |
||
435 | if (!isset($extractedRawData) || !$extractedRawData) { |
||
436 | $extractedRawData = $this->extractRawData(); |
||
437 | } |
||
438 | $currentFont = null; /** @var Font $currentFont */ |
||
439 | $clippedFont = null; |
||
440 | $fpdfPage = null; |
||
441 | if ($this->isFpdf()) { |
||
442 | $fpdfPage = $this->createPageForFpdf(); |
||
443 | } |
||
444 | foreach ($extractedRawData as &$command) { |
||
445 | if ('Tj' == $command['o'] || 'TJ' == $command['o']) { |
||
446 | $data = $command['c']; |
||
447 | if (!\is_array($data)) { |
||
448 | $tmpText = ''; |
||
449 | if (isset($currentFont)) { |
||
450 | $tmpText = $currentFont->decodeOctal($data); |
||
451 | // $tmpText = $currentFont->decodeHexadecimal($tmpText, false); |
||
452 | } |
||
453 | $tmpText = str_replace( |
||
454 | ['\\\\', '\(', '\)', '\n', '\r', '\t', '\ '], |
||
455 | ['\\', '(', ')', "\n", "\r", "\t", ' '], |
||
456 | $tmpText |
||
457 | ); |
||
458 | $tmpText = mb_convert_encoding($tmpText, 'UTF-8', 'ISO-8859-1'); |
||
459 | if (isset($currentFont)) { |
||
460 | $tmpText = $currentFont->decodeContent($tmpText); |
||
461 | } |
||
462 | $command['c'] = $tmpText; |
||
463 | continue; |
||
464 | } |
||
465 | $numText = \count($data); |
||
466 | for ($i = 0; $i < $numText; ++$i) { |
||
467 | if (0 != ($i % 2)) { |
||
468 | continue; |
||
469 | } |
||
470 | $tmpText = $data[$i]['c']; |
||
471 | $decodedText = isset($currentFont) ? $currentFont->decodeOctal($tmpText) : $tmpText; |
||
472 | $decodedText = str_replace( |
||
473 | ['\\\\', '\(', '\)', '\n', '\r', '\t', '\ '], |
||
474 | ['\\', '(', ')', "\n", "\r", "\t", ' '], |
||
475 | $decodedText |
||
476 | ); |
||
477 | |||
478 | $decodedText = mb_convert_encoding($decodedText, 'UTF-8', 'ISO-8859-1'); |
||
479 | |||
480 | if (isset($currentFont)) { |
||
481 | $decodedText = $currentFont->decodeContent($decodedText); |
||
482 | } |
||
483 | $command['c'][$i]['c'] = $decodedText; |
||
484 | continue; |
||
485 | } |
||
486 | } elseif ('Tf' == $command['o'] || 'TF' == $command['o']) { |
||
487 | $fontId = explode(' ', $command['c'])[0]; |
||
488 | // If document is a FPDI/FPDF the $page has the correct font |
||
489 | $currentFont = isset($fpdfPage) ? $fpdfPage->getFont($fontId) : $this->getFont($fontId); |
||
490 | continue; |
||
491 | } elseif ('Q' == $command['o']) { |
||
492 | $currentFont = $clippedFont; |
||
493 | } elseif ('q' == $command['o']) { |
||
494 | $clippedFont = $currentFont; |
||
495 | } |
||
496 | } |
||
497 | |||
498 | return $extractedRawData; |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * Gets just the Text commands that are involved in text positions and |
||
503 | * Text Matrix (Tm) |
||
504 | * |
||
505 | * It extract just the PDF commands that are involved with text positions, and |
||
506 | * the Text Matrix (Tm). These are: BT, ET, TL, Td, TD, Tm, T*, Tj, ', ", and TJ |
||
507 | * |
||
508 | * @param array $extractedDecodedRawData The data extracted by extractDecodeRawData. |
||
509 | * If it is null, the method extractDecodeRawData is called. |
||
510 | * |
||
511 | * @return array An array with the text command of the page |
||
512 | */ |
||
513 | public function getDataCommands(?array $extractedDecodedRawData = null): array |
||
666 | } |
||
667 | |||
668 | /** |
||
669 | * Gets the Text Matrix of the text in the page |
||
670 | * |
||
671 | * Return an array where every item is an array where the first item is the |
||
672 | * Text Matrix (Tm) and the second is a string with the text data. The Text matrix |
||
673 | * is an array of 6 numbers. The last 2 numbers are the coordinates X and Y of the |
||
674 | * text. The first 4 numbers has to be with Scalation, Rotation and Skew of the text. |
||
675 | * |
||
676 | * @param array $dataCommands the data extracted by getDataCommands |
||
677 | * if null getDataCommands is called |
||
678 | * |
||
679 | * @return array an array with the data of the page including the Tm information |
||
680 | * of any text in the page |
||
681 | */ |
||
682 | public function getDataTm(?array $dataCommands = null): array |
||
942 | } |
||
943 | |||
944 | /** |
||
945 | * Gets text data that are around the given coordinates (X,Y) |
||
946 | * |
||
947 | * If the text is in near the given coordinates (X,Y) (or the TM info), |
||
948 | * the text is returned. The extractedData return by getDataTm, could be use to see |
||
949 | * where is the coordinates of a given text, using the TM info for it. |
||
950 | * |
||
951 | * @param float $x The X value of the coordinate to search for. if null |
||
952 | * just the Y value is considered (same Row) |
||
953 | * @param float $y The Y value of the coordinate to search for |
||
954 | * just the X value is considered (same column) |
||
955 | * @param float $xError The value less or more to consider an X to be "near" |
||
956 | * @param float $yError The value less or more to consider an Y to be "near" |
||
957 | * |
||
958 | * @return array An array of text that are near the given coordinates. If no text |
||
959 | * "near" the x,y coordinate, an empty array is returned. If Both, x |
||
960 | * and y coordinates are null, null is returned. |
||
961 | */ |
||
962 | public function getTextXY(?float $x = null, ?float $y = null, float $xError = 0, float $yError = 0): array |
||
1015 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.