| Total Complexity | 130 |
| Total Lines | 856 |
| 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 |
||
| 38 | class Page extends PDFObject |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var Font[] |
||
| 42 | */ |
||
| 43 | protected $fonts = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var PDFObject[] |
||
| 47 | */ |
||
| 48 | protected $xobjects = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $dataTm = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return Font[] |
||
| 57 | */ |
||
| 58 | public function getFonts() |
||
| 95 | } |
||
| 96 | |||
| 97 | public function getFont(string $id): ?Font |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Support for XObject |
||
| 123 | * |
||
| 124 | * @return PDFObject[] |
||
| 125 | */ |
||
| 126 | public function getXObjects() |
||
| 127 | { |
||
| 128 | if (null !== $this->xobjects) { |
||
| 129 | return $this->xobjects; |
||
| 130 | } |
||
| 131 | |||
| 132 | $resources = $this->get('Resources'); |
||
| 133 | |||
| 134 | if (method_exists($resources, 'has') && $resources->has('XObject')) { |
||
| 135 | if ($resources->get('XObject') instanceof Header) { |
||
| 136 | $xobjects = $resources->get('XObject')->getElements(); |
||
| 137 | } else { |
||
| 138 | $xobjects = $resources->get('XObject')->getHeader()->getElements(); |
||
| 139 | } |
||
| 140 | |||
| 141 | $table = []; |
||
| 142 | |||
| 143 | foreach ($xobjects as $id => $xobject) { |
||
| 144 | $table[$id] = $xobject; |
||
| 145 | |||
| 146 | // Store too on cleaned id value (only numeric) |
||
| 147 | $id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
| 148 | if ('' != $id) { |
||
| 149 | $table[$id] = $xobject; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | return $this->xobjects = $table; |
||
| 154 | } |
||
| 155 | |||
| 156 | return []; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function getXObject(string $id): ?PDFObject |
||
| 160 | { |
||
| 161 | $xobjects = $this->getXObjects(); |
||
| 162 | |||
| 163 | if (isset($xobjects[$id])) { |
||
| 164 | return $xobjects[$id]; |
||
| 165 | } |
||
| 166 | |||
| 167 | return null; |
||
| 168 | /*$id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
| 169 | |||
| 170 | if (isset($xobjects[$id])) { |
||
| 171 | return $xobjects[$id]; |
||
| 172 | } else { |
||
| 173 | return null; |
||
| 174 | }*/ |
||
| 175 | } |
||
| 176 | |||
| 177 | public function getText(self $page = null): string |
||
| 178 | { |
||
| 179 | if ($contents = $this->get('Contents')) { |
||
| 180 | if ($contents instanceof ElementMissing) { |
||
| 181 | return ''; |
||
| 182 | } elseif ($contents instanceof ElementNull) { |
||
| 183 | return ''; |
||
| 184 | } elseif ($contents instanceof PDFObject) { |
||
| 185 | $elements = $contents->getHeader()->getElements(); |
||
| 186 | |||
| 187 | if (is_numeric(key($elements))) { |
||
| 188 | $new_content = ''; |
||
| 189 | |||
| 190 | foreach ($elements as $element) { |
||
| 191 | if ($element instanceof ElementXRef) { |
||
| 192 | $new_content .= $element->getObject()->getContent(); |
||
| 193 | } else { |
||
| 194 | $new_content .= $element->getContent(); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | $header = new Header([], $this->document); |
||
| 199 | $contents = new PDFObject($this->document, $header, $new_content, $this->config); |
||
| 200 | } |
||
| 201 | } elseif ($contents instanceof ElementArray) { |
||
| 202 | // Create a virtual global content. |
||
| 203 | $new_content = ''; |
||
| 204 | |||
| 205 | foreach ($contents->getContent() as $content) { |
||
| 206 | $new_content .= $content->getContent()."\n"; |
||
| 207 | } |
||
| 208 | |||
| 209 | $header = new Header([], $this->document); |
||
| 210 | $contents = new PDFObject($this->document, $header, $new_content, $this->config); |
||
| 211 | } |
||
| 212 | |||
| 213 | /* |
||
| 214 | * Elements referencing each other on the same page can cause endless loops during text parsing. |
||
| 215 | * To combat this we keep a recursionStack containing already parsed elements on the page. |
||
| 216 | * The stack is only emptied here after getting text from a page. |
||
| 217 | */ |
||
| 218 | $contentsText = $contents->getText($this); |
||
| 219 | PDFObject::$recursionStack = []; |
||
| 220 | |||
| 221 | return $contentsText; |
||
| 222 | } |
||
| 223 | |||
| 224 | return ''; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Return true if the current page is a (setasign\Fpdi\Fpdi) FPDI/FPDF document |
||
| 229 | * |
||
| 230 | * The metadata 'Producer' should have the value of "FPDF" . FPDF_VERSION if the |
||
| 231 | * pdf file was generated by FPDF/Fpfi. |
||
| 232 | * |
||
| 233 | * @return bool true is the current page is a FPDI/FPDF document |
||
| 234 | */ |
||
| 235 | public function isFpdf(): bool |
||
| 236 | { |
||
| 237 | if (\array_key_exists('Producer', $this->document->getDetails()) && |
||
| 238 | \is_string($this->document->getDetails()['Producer']) && |
||
| 239 | 0 === strncmp($this->document->getDetails()['Producer'], 'FPDF', \strlen('FPDF'))) { |
||
| 240 | return true; |
||
| 241 | } |
||
| 242 | |||
| 243 | return false; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Return the page number of the PDF document of the page object |
||
| 248 | * |
||
| 249 | * @return int the page number |
||
| 250 | */ |
||
| 251 | public function getPageNumber(): int |
||
| 252 | { |
||
| 253 | $pages = $this->document->getPages(); |
||
| 254 | $numOfPages = \count($pages); |
||
| 255 | for ($pageNum = 0; $pageNum < $numOfPages; ++$pageNum) { |
||
| 256 | if ($pages[$pageNum] === $this) { |
||
| 257 | break; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | return $pageNum; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Return the Object of the page if the document is a FPDF/FPDI document |
||
| 266 | * |
||
| 267 | * If the document was generated by FPDF/FPDI it returns the |
||
| 268 | * PDFObject of the given page |
||
| 269 | * |
||
| 270 | * @return PDFObject The PDFObject for the page |
||
| 271 | */ |
||
| 272 | public function getPDFObjectForFpdf(): PDFObject |
||
| 273 | { |
||
| 274 | $pageNum = $this->getPageNumber(); |
||
| 275 | $xObjects = $this->getXObjects(); |
||
| 276 | |||
| 277 | return $xObjects[$pageNum]; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Return a new PDFObject of the document created with FPDF/FPDI |
||
| 282 | * |
||
| 283 | * For a document generated by FPDF/FPDI, it generates a |
||
| 284 | * new PDFObject for that document |
||
| 285 | * |
||
| 286 | * @return PDFObject The PDFObject |
||
| 287 | */ |
||
| 288 | public function createPDFObjectForFpdf(): PDFObject |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Return page if document is a FPDF/FPDI document |
||
| 300 | * |
||
| 301 | * @return Page The page |
||
| 302 | */ |
||
| 303 | public function createPageForFpdf(): self |
||
| 304 | { |
||
| 305 | $pdfObject = $this->getPDFObjectForFpdf(); |
||
| 306 | $new_content = $pdfObject->getContent(); |
||
| 307 | $header = $pdfObject->getHeader(); |
||
| 308 | $config = $pdfObject->config; |
||
| 309 | |||
| 310 | return new self($pdfObject->document, $header, $new_content, $config); |
||
| 311 | } |
||
| 312 | |||
| 313 | public function getTextArray(self $page = null): array |
||
| 314 | { |
||
| 315 | if ($this->isFpdf()) { |
||
| 316 | $pdfObject = $this->getPDFObjectForFpdf(); |
||
| 317 | $newPdfObject = $this->createPDFObjectForFpdf(); |
||
| 318 | |||
| 319 | return $newPdfObject->getTextArray($pdfObject); |
||
| 320 | } else { |
||
| 321 | if ($contents = $this->get('Contents')) { |
||
| 322 | if ($contents instanceof ElementMissing) { |
||
| 323 | return []; |
||
| 324 | } elseif ($contents instanceof ElementNull) { |
||
| 325 | return []; |
||
| 326 | } elseif ($contents instanceof PDFObject) { |
||
| 327 | $elements = $contents->getHeader()->getElements(); |
||
| 328 | |||
| 329 | if (is_numeric(key($elements))) { |
||
| 330 | $new_content = ''; |
||
| 331 | |||
| 332 | /** @var PDFObject $element */ |
||
| 333 | foreach ($elements as $element) { |
||
| 334 | if ($element instanceof ElementXRef) { |
||
| 335 | $new_content .= $element->getObject()->getContent(); |
||
| 336 | } else { |
||
| 337 | $new_content .= $element->getContent(); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | $header = new Header([], $this->document); |
||
| 342 | $contents = new PDFObject($this->document, $header, $new_content, $this->config); |
||
| 343 | } else { |
||
| 344 | try { |
||
| 345 | $contents->getTextArray($this); |
||
| 346 | } catch (\Throwable $e) { |
||
| 347 | return $contents->getTextArray(); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | } elseif ($contents instanceof ElementArray) { |
||
| 351 | // Create a virtual global content. |
||
| 352 | $new_content = ''; |
||
| 353 | |||
| 354 | /** @var PDFObject $content */ |
||
| 355 | foreach ($contents->getContent() as $content) { |
||
| 356 | $new_content .= $content->getContent()."\n"; |
||
| 357 | } |
||
| 358 | |||
| 359 | $header = new Header([], $this->document); |
||
| 360 | $contents = new PDFObject($this->document, $header, $new_content, $this->config); |
||
| 361 | } |
||
| 362 | |||
| 363 | return $contents->getTextArray($this); |
||
| 364 | } |
||
| 365 | |||
| 366 | return []; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Gets all the text data with its internal representation of the page. |
||
| 372 | * |
||
| 373 | * Returns an array with the data and the internal representation |
||
| 374 | */ |
||
| 375 | public function extractRawData(): array |
||
| 376 | { |
||
| 377 | /* |
||
| 378 | * Now you can get the complete content of the object with the text on it |
||
| 379 | */ |
||
| 380 | $extractedData = []; |
||
| 381 | $content = $this->get('Contents'); |
||
| 382 | $values = $content->getContent(); |
||
| 383 | if (isset($values) && \is_array($values)) { |
||
| 384 | $text = ''; |
||
| 385 | foreach ($values as $section) { |
||
| 386 | $text .= $section->getContent(); |
||
| 387 | } |
||
| 388 | $sectionsText = $this->getSectionsText($text); |
||
| 389 | foreach ($sectionsText as $sectionText) { |
||
| 390 | $commandsText = $this->getCommandsText($sectionText); |
||
| 391 | foreach ($commandsText as $command) { |
||
| 392 | $extractedData[] = $command; |
||
| 393 | } |
||
| 394 | } |
||
| 395 | } else { |
||
| 396 | if ($this->isFpdf()) { |
||
| 397 | $content = $this->getPDFObjectForFpdf(); |
||
| 398 | } |
||
| 399 | $sectionsText = $content->getSectionsText($content->getContent()); |
||
| 400 | foreach ($sectionsText as $sectionText) { |
||
| 401 | $extractedData[] = ['t' => '', 'o' => 'BT', 'c' => '']; |
||
| 402 | |||
| 403 | $commandsText = $content->getCommandsText($sectionText); |
||
| 404 | foreach ($commandsText as $command) { |
||
| 405 | $extractedData[] = $command; |
||
| 406 | } |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | return $extractedData; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Gets all the decoded text data with it internal representation from a page. |
||
| 415 | * |
||
| 416 | * @param array $extractedRawData the extracted data return by extractRawData or |
||
| 417 | * null if extractRawData should be called |
||
| 418 | * |
||
| 419 | * @return array An array with the data and the internal representation |
||
| 420 | */ |
||
| 421 | public function extractDecodedRawData(array $extractedRawData = null): array |
||
| 422 | { |
||
| 423 | if (!isset($extractedRawData) || !$extractedRawData) { |
||
| 424 | $extractedRawData = $this->extractRawData(); |
||
| 425 | } |
||
| 426 | $currentFont = null; /** @var Font $currentFont */ |
||
| 427 | $clippedFont = null; |
||
| 428 | $fpdfPage = null; |
||
| 429 | if ($this->isFpdf()) { |
||
| 430 | $fpdfPage = $this->createPageForFpdf(); |
||
| 431 | } |
||
| 432 | foreach ($extractedRawData as &$command) { |
||
| 433 | if ('Tj' == $command['o'] || 'TJ' == $command['o']) { |
||
| 434 | $data = $command['c']; |
||
| 435 | if (!\is_array($data)) { |
||
| 436 | $tmpText = ''; |
||
| 437 | if (isset($currentFont)) { |
||
| 438 | $tmpText = $currentFont->decodeOctal($data); |
||
| 439 | //$tmpText = $currentFont->decodeHexadecimal($tmpText, false); |
||
| 440 | } |
||
| 441 | $tmpText = str_replace( |
||
| 442 | ['\\\\', '\(', '\)', '\n', '\r', '\t', '\ '], |
||
| 443 | ['\\', '(', ')', "\n", "\r", "\t", ' '], |
||
| 444 | $tmpText |
||
| 445 | ); |
||
| 446 | $tmpText = utf8_encode($tmpText); |
||
| 447 | if (isset($currentFont)) { |
||
| 448 | $tmpText = $currentFont->decodeContent($tmpText); |
||
| 449 | } |
||
| 450 | $command['c'] = $tmpText; |
||
| 451 | continue; |
||
| 452 | } |
||
| 453 | $numText = \count($data); |
||
| 454 | for ($i = 0; $i < $numText; ++$i) { |
||
| 455 | if (0 != ($i % 2)) { |
||
| 456 | continue; |
||
| 457 | } |
||
| 458 | $tmpText = $data[$i]['c']; |
||
| 459 | $decodedText = isset($currentFont) ? $currentFont->decodeOctal($tmpText) : $tmpText; |
||
| 460 | $decodedText = str_replace( |
||
| 461 | ['\\\\', '\(', '\)', '\n', '\r', '\t', '\ '], |
||
| 462 | ['\\', '(', ')', "\n", "\r", "\t", ' '], |
||
| 463 | $decodedText |
||
| 464 | ); |
||
| 465 | $decodedText = utf8_encode($decodedText); |
||
| 466 | if (isset($currentFont)) { |
||
| 467 | $decodedText = $currentFont->decodeContent($decodedText); |
||
| 468 | } |
||
| 469 | $command['c'][$i]['c'] = $decodedText; |
||
| 470 | continue; |
||
| 471 | } |
||
| 472 | } elseif ('Tf' == $command['o'] || 'TF' == $command['o']) { |
||
| 473 | $fontId = explode(' ', $command['c'])[0]; |
||
| 474 | // If document is a FPDI/FPDF the $page has the correct font |
||
| 475 | $currentFont = isset($fpdfPage) ? $fpdfPage->getFont($fontId) : $this->getFont($fontId); |
||
| 476 | continue; |
||
| 477 | } elseif ('Q' == $command['o']) { |
||
| 478 | $currentFont = $clippedFont; |
||
| 479 | } elseif ('q' == $command['o']) { |
||
| 480 | $clippedFont = $currentFont; |
||
| 481 | } |
||
| 482 | } |
||
| 483 | |||
| 484 | return $extractedRawData; |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Gets just the Text commands that are involved in text positions and |
||
| 489 | * Text Matrix (Tm) |
||
| 490 | * |
||
| 491 | * It extract just the PDF commands that are involved with text positions, and |
||
| 492 | * the Text Matrix (Tm). These are: BT, ET, TL, Td, TD, Tm, T*, Tj, ', ", and TJ |
||
| 493 | * |
||
| 494 | * @param array $extractedDecodedRawData The data extracted by extractDecodeRawData. |
||
| 495 | * If it is null, the method extractDecodeRawData is called. |
||
| 496 | * |
||
| 497 | * @return array An array with the text command of the page |
||
| 498 | */ |
||
| 499 | public function getDataCommands(array $extractedDecodedRawData = null): array |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Gets the Text Matrix of the text in the page |
||
| 633 | * |
||
| 634 | * Return an array where every item is an array where the first item is the |
||
| 635 | * Text Matrix (Tm) and the second is a string with the text data. The Text matrix |
||
| 636 | * is an array of 6 numbers. The last 2 numbers are the coordinates X and Y of the |
||
| 637 | * text. The first 4 numbers has to be with Scalation, Rotation and Skew of the text. |
||
| 638 | * |
||
| 639 | * @param array $dataCommands the data extracted by getDataCommands |
||
| 640 | * if null getDataCommands is called |
||
| 641 | * |
||
| 642 | * @return array an array with the data of the page including the Tm information |
||
| 643 | * of any text in the page |
||
| 644 | */ |
||
| 645 | public function getDataTm(array $dataCommands = null): array |
||
| 823 | } |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Gets text data that are around the given coordinates (X,Y) |
||
| 827 | * |
||
| 828 | * If the text is in near the given coordinates (X,Y) (or the TM info), |
||
| 829 | * the text is returned. The extractedData return by getDataTm, could be use to see |
||
| 830 | * where is the coordinates of a given text, using the TM info for it. |
||
| 831 | * |
||
| 832 | * @param float $x The X value of the coordinate to search for. if null |
||
| 833 | * just the Y value is considered (same Row) |
||
| 834 | * @param float $y The Y value of the coordinate to search for |
||
| 835 | * just the X value is considered (same column) |
||
| 836 | * @param float $xError The value less or more to consider an X to be "near" |
||
| 837 | * @param float $yError The value less or more to consider an Y to be "near" |
||
| 838 | * |
||
| 839 | * @return array An array of text that are near the given coordinates. If no text |
||
| 840 | * "near" the x,y coordinate, an empty array is returned. If Both, x |
||
| 841 | * and y coordinates are null, null is returned. |
||
| 842 | */ |
||
| 843 | public function getTextXY(float $x = null, float $y = null, float $xError = 0, float $yError = 0): array |
||
| 896 |
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.