diff --git a/.php_cs b/.php_cs index cde339a6..2853a08b 100644 --- a/.php_cs +++ b/.php_cs @@ -15,10 +15,9 @@ return PhpCsFixer\Config::create() ->setRiskyAllowed(true) ->setFinder( PhpCsFixer\Finder::create() - ->files() - ->in(__DIR__ . '/samples') - ->in(__DIR__ . '/src') - ->in(__DIR__ . '/test') - ->in(__DIR__ . '/tests') - ->name('*.php') + ->exclude([ + 'vendor', + ]) + ->in(__DIR__) + ->name('*.php') ); diff --git a/.travis.yml b/.travis.yml index 400c3814..d030fbc9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,6 @@ php: - 7.1 - 7.2 - 7.3 - - 7.4 - nightly jobs: @@ -16,13 +15,14 @@ jobs: allow_failures: - php: nightly include: - - php: 7.2 - env: COMPOSER_FLAGS="--prefer-lowest" - php: 7.4 - env: COVERAGE_FLAGS="--coverage-clover coverage/clover.xml" + env: COMPOSER_FLAGS="--prefer-lowest" LINTER_RUN=run COVERAGE_FLAGS="--coverage-clover coverage/clover.xml" install: - - composer self-update - composer update --prefer-dist --no-progress --no-suggest --optimize-autoloader $COMPOSER_FLAGS -script: vendor/bin/phpunit $COVERAGE_FLAGS +script: + - if [ "$LINTER_RUN" = "run" ]; then php vendor/bin/php-cs-fixer fix --verbose --dry-run ; fi; + - if [ "$LINTER_RUN" = "run" ]; then composer require phpstan/phpstan --no-progress --no-suggest ; fi; + - if [ "$LINTER_RUN" = "run" ]; then php vendor/bin/phpstan analyse src test tests --no-progress --level 3 ; fi; + - php vendor/bin/phpunit $COVERAGE_FLAGS diff --git a/src/Smalot/PdfParser/Document.php b/src/Smalot/PdfParser/Document.php index d2436c07..1de0805c 100644 --- a/src/Smalot/PdfParser/Document.php +++ b/src/Smalot/PdfParser/Document.php @@ -157,22 +157,22 @@ public function getObjects() /** * @param string $id * - * @return PDFObject + * @return PDFObject|Font|Page|Element|null */ public function getObjectById($id) { if (isset($this->objects[$id])) { return $this->objects[$id]; - } else { - return null; } + + return null; } /** * @param string $type * @param string $subtype * - * @return PDFObject[] + * @return array */ public function getObjectsByType($type, $subtype = null) { @@ -211,9 +211,7 @@ public function getPages() /** @var Pages $object */ $object = $this->objects[$id]->get('Pages'); if (method_exists($object, 'getPages')) { - $pages = $object->getPages(true); - - return $pages; + return $object->getPages(true); } } diff --git a/src/Smalot/PdfParser/Element.php b/src/Smalot/PdfParser/Element.php index 805a0c51..02f8c5f2 100644 --- a/src/Smalot/PdfParser/Element.php +++ b/src/Smalot/PdfParser/Element.php @@ -88,9 +88,9 @@ public function contains($value) } return false; - } else { - return $this->equals($value); } + + return $this->equals($value); } public function getContent() diff --git a/src/Smalot/PdfParser/Element/ElementBoolean.php b/src/Smalot/PdfParser/Element/ElementBoolean.php index 9b348dfe..91f3be21 100644 --- a/src/Smalot/PdfParser/Element/ElementBoolean.php +++ b/src/Smalot/PdfParser/Element/ElementBoolean.php @@ -39,10 +39,9 @@ class ElementBoolean extends Element { /** - * @param string $value - * @param Document $document + * @param string|bool $value */ - public function __construct($value, Document $document = null) + public function __construct($value) { parent::__construct(('true' == strtolower($value) || true === $value), null); } @@ -76,7 +75,7 @@ public static function parse($content, Document $document = null, &$offset = 0) $value = $match['value']; $offset += strpos($content, $value) + \strlen($value); - return new self($value, $document); + return new self($value); } return false; diff --git a/src/Smalot/PdfParser/Element/ElementDate.php b/src/Smalot/PdfParser/Element/ElementDate.php index 4fad3d0d..2637e71f 100644 --- a/src/Smalot/PdfParser/Element/ElementDate.php +++ b/src/Smalot/PdfParser/Element/ElementDate.php @@ -60,15 +60,14 @@ class ElementDate extends ElementString /** * @param \DateTime $value - * @param Document $document */ - public function __construct($value, Document $document = null) + public function __construct($value) { if (!($value instanceof \DateTime)) { throw new \Exception('DateTime required.'); } - parent::__construct($value, null); + parent::__construct($value); } /** @@ -140,9 +139,8 @@ public static function parse($content, Document $document = null, &$offset = 0) } $offset += strpos($content, '(D:') + \strlen($match['name']) + 4; // 1 for '(D:' and ')' - $element = new self($date, $document); - return $element; + return new self($date); } return false; diff --git a/src/Smalot/PdfParser/Element/ElementHexa.php b/src/Smalot/PdfParser/Element/ElementHexa.php index 6dbc48d7..821c42b1 100644 --- a/src/Smalot/PdfParser/Element/ElementHexa.php +++ b/src/Smalot/PdfParser/Element/ElementHexa.php @@ -42,7 +42,7 @@ class ElementHexa extends ElementString * @param Document $document * @param int $offset * - * @return bool|ElementHexa + * @return bool|ElementHexa|ElementDate */ public static function parse($content, Document $document = null, &$offset = 0) { diff --git a/src/Smalot/PdfParser/Element/ElementMissing.php b/src/Smalot/PdfParser/Element/ElementMissing.php index 6c116f97..bdfd681e 100644 --- a/src/Smalot/PdfParser/Element/ElementMissing.php +++ b/src/Smalot/PdfParser/Element/ElementMissing.php @@ -30,7 +30,6 @@ namespace Smalot\PdfParser\Element; -use Smalot\PdfParser\Document; use Smalot\PdfParser\Element; /** @@ -38,11 +37,7 @@ */ class ElementMissing extends Element { - /** - * @param string $value - * @param Document $document - */ - public function __construct($value, Document $document = null) + public function __construct() { parent::__construct(null, null); } diff --git a/src/Smalot/PdfParser/Element/ElementName.php b/src/Smalot/PdfParser/Element/ElementName.php index e2e88dae..a73c5698 100644 --- a/src/Smalot/PdfParser/Element/ElementName.php +++ b/src/Smalot/PdfParser/Element/ElementName.php @@ -40,10 +40,9 @@ class ElementName extends Element { /** - * @param string $value - * @param Document $document + * @param string $value */ - public function __construct($value, Document $document = null) + public function __construct($value) { parent::__construct($value, null); } @@ -70,7 +69,7 @@ public static function parse($content, Document $document = null, &$offset = 0) $offset += strpos($content, $name) + \strlen($name); $name = Font::decodeEntities($name); - return new self($name, $document); + return new self($name); } return false; diff --git a/src/Smalot/PdfParser/Element/ElementNull.php b/src/Smalot/PdfParser/Element/ElementNull.php index ca448552..0ea67af3 100644 --- a/src/Smalot/PdfParser/Element/ElementNull.php +++ b/src/Smalot/PdfParser/Element/ElementNull.php @@ -38,11 +38,7 @@ */ class ElementNull extends Element { - /** - * @param string $value - * @param Document $document - */ - public function __construct($value, Document $document = null) + public function __construct() { parent::__construct(null, null); } @@ -75,7 +71,7 @@ public static function parse($content, Document $document = null, &$offset = 0) if (preg_match('/^\s*(null)/s', $content, $match)) { $offset += strpos($content, 'null') + \strlen('null'); - return new self(null, $document); + return new self(); } return false; diff --git a/src/Smalot/PdfParser/Element/ElementNumeric.php b/src/Smalot/PdfParser/Element/ElementNumeric.php index 733d3eeb..012b798f 100644 --- a/src/Smalot/PdfParser/Element/ElementNumeric.php +++ b/src/Smalot/PdfParser/Element/ElementNumeric.php @@ -39,10 +39,9 @@ class ElementNumeric extends Element { /** - * @param string $value - * @param Document $document + * @param string $value */ - public function __construct($value, Document $document = null) + public function __construct($value) { parent::__construct((float) $value, null); } @@ -60,7 +59,7 @@ public static function parse($content, Document $document = null, &$offset = 0) $value = $match['value']; $offset += strpos($content, $value) + \strlen($value); - return new self($value, $document); + return new self($value); } return false; diff --git a/src/Smalot/PdfParser/Element/ElementString.php b/src/Smalot/PdfParser/Element/ElementString.php index 2aa0861e..1086737a 100644 --- a/src/Smalot/PdfParser/Element/ElementString.php +++ b/src/Smalot/PdfParser/Element/ElementString.php @@ -40,10 +40,9 @@ class ElementString extends Element { /** - * @param string $value - * @param Document $document + * @param string $value */ - public function __construct($value, Document $document = null) + public function __construct($value) { parent::__construct($value, null); } @@ -80,7 +79,7 @@ public static function parse($content, Document $document = null, &$offset = 0) } // Extract string. - $name = substr($name, 0, $cur_start_pos); + $name = substr($name, 0, (int) $cur_start_pos); $offset += strpos($content, '(') + $cur_start_pos + 2; // 2 for '(' and ')' $name = str_replace( ['\\\\', '\\ ', '\\/', '\(', '\)', '\n', '\r', '\t'], @@ -94,7 +93,7 @@ public static function parse($content, Document $document = null, &$offset = 0) $name = Font::decodeHexadecimal($name, false); $name = Font::decodeUnicode($name); - return new self($name, $document); + return new self($name); } return false; diff --git a/src/Smalot/PdfParser/Element/ElementStruct.php b/src/Smalot/PdfParser/Element/ElementStruct.php index 5402d179..dafa0336 100644 --- a/src/Smalot/PdfParser/Element/ElementStruct.php +++ b/src/Smalot/PdfParser/Element/ElementStruct.php @@ -44,7 +44,7 @@ class ElementStruct extends Element * @param Document $document * @param int $offset * - * @return bool|ElementStruct + * @return false|Header */ public static function parse($content, Document $document = null, &$offset = 0) { @@ -64,13 +64,12 @@ public static function parse($content, Document $document = null, &$offset = 0) $offset += strpos($content, '<<') + \strlen(rtrim($sub)); // Removes '<<' and '>>'. - $sub = trim(preg_replace('/^\s*<<(.*)>>\s*$/s', '\\1', $sub)); + $sub = trim((string) preg_replace('/^\s*<<(.*)>>\s*$/s', '\\1', $sub)); $position = 0; $elements = Element::parse($sub, $document, $position); - $header = new Header($elements, $document); - return $header; + return new Header($elements, $document); } return false; diff --git a/src/Smalot/PdfParser/Encoding.php b/src/Smalot/PdfParser/Encoding.php index 04c22ff9..4e381ee5 100644 --- a/src/Smalot/PdfParser/Encoding.php +++ b/src/Smalot/PdfParser/Encoding.php @@ -56,20 +56,20 @@ public function init() { $this->mapping = []; $this->differences = []; - $this->encoding = null; + $this->encoding = []; if ($this->has('BaseEncoding')) { // Load reference table charset. $baseEncoding = preg_replace('/[^A-Z0-9]/is', '', $this->get('BaseEncoding')->getContent()); $className = '\\Smalot\\PdfParser\\Encoding\\'.$baseEncoding; - if (class_exists($className)) { - $class = new $className(); - $this->encoding = $class->getTranslations(); - } else { + if (!class_exists($className)) { throw new \Exception('Missing encoding data for: "'.$baseEncoding.'".'); } + $class = new $className(); + $this->encoding = $class->getTranslations(); + // Build table including differences. $differences = $this->get('Differences')->getContent(); $code = 0; @@ -86,10 +86,9 @@ public function init() } // ElementName + $this->differences[$code] = $difference; if (\is_object($difference)) { $this->differences[$code] = $difference->getContent(); - } else { - $this->differences[$code] = $difference; } // For the next char. diff --git a/src/Smalot/PdfParser/Font.php b/src/Smalot/PdfParser/Font.php index bcb21cdc..cc1cce8b 100644 --- a/src/Smalot/PdfParser/Font.php +++ b/src/Smalot/PdfParser/Font.php @@ -89,19 +89,17 @@ public function getDetails($deep = true) * @param string $char * @param bool $use_default * - * @return string + * @return string|bool */ public function translateChar($char, $use_default = true) { $dec = hexdec(bin2hex($char)); if (\array_key_exists($dec, $this->table)) { - $char = $this->table[$dec]; - } else { - $char = ($use_default ? self::MISSING : $char); + return $this->table[$dec]; } - return $char; + return $use_default ? self::MISSING : $char; } /** @@ -292,7 +290,7 @@ public static function decodeOctal($text) } /** - * @param $text + * @param string $text * * @return string */ @@ -348,6 +346,7 @@ protected function getFontSpaceLimit() */ public function decodeText($commands) { + $text = ''; $word_position = 0; $words = []; $unicode = false; @@ -456,7 +455,7 @@ public function decodeContent($text, &$unicode) if ($encoding instanceof Encoding) { if ($unicode) { $chars = preg_split( - '//s'.($unicode ? 'u' : ''), + '//su', $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY diff --git a/src/Smalot/PdfParser/Header.php b/src/Smalot/PdfParser/Header.php index 3e28e706..80a73c95 100644 --- a/src/Smalot/PdfParser/Header.php +++ b/src/Smalot/PdfParser/Header.php @@ -124,11 +124,7 @@ public function getDetails($deep = true) */ public function has($name) { - if (\array_key_exists($name, $this->elements)) { - return true; - } else { - return false; - } + return \array_key_exists($name, $this->elements); } /** @@ -142,7 +138,7 @@ public function get($name) return $this->resolveXRef($name); } - return new ElementMissing(null, null); + return new ElementMissing(); } /** @@ -161,7 +157,7 @@ protected function resolveXRef($name) $object = $this->document->getObjectById($obj->getId()); if (null === $object) { - return new ElementMissing(null, null); + return new ElementMissing(); } // Update elements list for future calls. @@ -185,18 +181,18 @@ public static function parse($content, Document $document, &$position = 0) $header = ElementStruct::parse($content, $document, $position); } else { $elements = ElementArray::parse($content, $document, $position); + $header = new self([], $document); + if ($elements) { - $header = new self($elements->getRawContent(), null); //$document); - } else { - $header = new self([], $document); + $header = new self($elements->getRawContent(), null); } } if ($header) { return $header; - } else { - // Build an empty header. - return new self([], $document); } + + // Build an empty header. + return new self([], $document); } } diff --git a/src/Smalot/PdfParser/PDFObject.php b/src/Smalot/PdfParser/PDFObject.php index 40aa9110..28430f80 100644 --- a/src/Smalot/PdfParser/PDFObject.php +++ b/src/Smalot/PdfParser/PDFObject.php @@ -100,7 +100,7 @@ public function get($name) } /** - * @param $name + * @param string $name * * @return bool */ @@ -128,7 +128,7 @@ public function getContent() } /** - * @param $content + * @param string $content */ public function cleanContent($content, $char = 'X') { @@ -190,7 +190,7 @@ public function cleanContent($content, $char = 'X') } /** - * @param $content + * @param string $content * * @return array */ @@ -232,7 +232,7 @@ public function getSectionsText($content) } /** - * @param Page + * @param Page $page * * @return string * @@ -430,7 +430,7 @@ public function getText(Page $page = null) } /** - * @param Page + * @param Page $page * * @return array * @@ -721,9 +721,7 @@ public function getCommandsText($text_part, &$offset = 0) } /** - * @param $document Document - * @param $header Header - * @param $content string + * @param string $content * * @return PDFObject */ @@ -737,11 +735,9 @@ public static function factory(Document $document, Header $header, $content) case 'Form': return new Form($document, $header, $content); - - default: - return new self($document, $header, $content); } - break; + + return new self($document, $header, $content); case 'Pages': return new Pages($document, $header, $content); @@ -758,11 +754,10 @@ public static function factory(Document $document, Header $header, $content) if (class_exists($classname)) { return new $classname($document, $header, $content); - } else { - return new Font($document, $header, $content); } - // no break + return new Font($document, $header, $content); + default: return new self($document, $header, $content); } diff --git a/src/Smalot/PdfParser/Page.php b/src/Smalot/PdfParser/Page.php index cc7085f0..f0b2e2d5 100644 --- a/src/Smalot/PdfParser/Page.php +++ b/src/Smalot/PdfParser/Page.php @@ -48,7 +48,7 @@ class Page extends PDFObject protected $xobjects = null; /** - * @var[] + * @var array */ protected $dataTm = null; @@ -85,15 +85,15 @@ public function getFonts() } return $this->fonts = $table; - } else { - return []; } + + return []; } /** * @param string $id * - * @return Font + * @return Font|null */ public function getFont($id) { @@ -101,15 +101,15 @@ public function getFont($id) if (isset($fonts[$id])) { return $fonts[$id]; - } else { - $id = preg_replace('/[^0-9\.\-_]/', '', $id); + } - if (isset($fonts[$id])) { - return $fonts[$id]; - } else { - return null; - } + $id = preg_replace('/[^0-9\.\-_]/', '', $id); + + if (isset($fonts[$id])) { + return $fonts[$id]; } + + return null; } /** @@ -145,36 +145,36 @@ public function getXObjects() } return $this->xobjects = $table; - } else { - return []; } + + return []; } /** * @param string $id * - * @return PDFObject + * @return PDFObject|null */ public function getXObject($id) { $xobjects = $this->getXObjects(); + if (isset($xobjects[$id])) { + return $xobjects[$id]; + } + + return null; + /*$id = preg_replace('/[^0-9\.\-_]/', '', $id); + if (isset($xobjects[$id])) { return $xobjects[$id]; } else { return null; - /*$id = preg_replace('/[^0-9\.\-_]/', '', $id); - - if (isset($xobjects[$id])) { - return $xobjects[$id]; - } else { - return null; - }*/ - } + }*/ } /** - * @param Page + * @param Page $page * * @return string */ @@ -221,7 +221,7 @@ public function getText(self $page = null) } /** - * @param Page + * @param Page $page * * @return array */ @@ -326,6 +326,7 @@ public function extractDecodedRawData($extractedRawData = null) if ('Tj' == $command['o'] or 'TJ' == $command['o']) { $data = $command['c']; if (!\is_array($data)) { + $tmpText = ''; if (isset($currentFont)) { $tmpText = $currentFont->decodeOctal($data); //$tmpText = $currentFont->decodeHexadecimal($tmpText, false); @@ -348,6 +349,7 @@ public function extractDecodedRawData($extractedRawData = null) continue; } $tmpText = $data[$i]['c']; + $decodedText = ''; if (isset($currentFont)) { $decodedText = $currentFont->decodeOctal($tmpText); //$tmpText = $currentFont->decodeHexadecimal($tmpText, false); @@ -664,7 +666,7 @@ public function getDataTm($dataCommands = null) * string Tj */ case "'": - $Ty -= Tl; + $Ty -= $Tl; $Tm[$y] = (string) $Ty; $extractedData[] = [$Tm, $command['c']]; break; @@ -682,7 +684,7 @@ public function getDataTm($dataCommands = null) */ case '"': $data = explode(' ', $command['c']); - $Ty -= Tl; + $Ty -= $Tl; $Tm[$y] = (string) $Ty; $extractedData[] = [$Tm, $data[2]]; //Verify break; @@ -739,45 +741,41 @@ public function getDataTm($dataCommands = null) * "near" the x,y coordinate, an empty array is returned. If Both, x * and y coordinates are null, null is returned. */ - public function getTextXY($x, $y, $xError = 0, $yError = 0) + public function getTextXY($x = null, $y = null, $xError = 0, $yError = 0) { if (!isset($this->dataTm) or !$this->dataTm) { $this->getDataTm(); } - if (isset($x)) { + + if (null !== $x) { $x = (float) $x; } - if (isset($y)) { + + if (null !== $y) { $y = (float) $y; } - if (!isset($x) and !isset($y)) { - return null; - } - if (!isset($xError)) { - $xError = 0; - } else { - $xError = (float) $xError; - } - if (!isset($yError)) { - $yError = 0; - } else { - $yError = (float) $yError; + if (null === $x and null === $y) { + return []; } + + $xError = (float) $xError; + $yError = (float) $yError; + $extractedData = []; foreach ($this->dataTm as $item) { $tm = $item[0]; $xTm = (float) $tm[4]; $yTm = (float) $tm[5]; $text = $item[1]; - if (!isset($y)) { + if (null === $y) { if (($xTm >= ($x - $xError)) and ($xTm <= ($x + $xError))) { $extractedData[] = [$tm, $text]; continue; } } - if (!isset($x)) { + if (null === $x) { if (($yTm >= ($y - $yError)) and ($yTm <= ($y + $yError))) { $extractedData[] = [$tm, $text]; diff --git a/src/Smalot/PdfParser/Pages.php b/src/Smalot/PdfParser/Pages.php index 4e08d742..47d90c5d 100644 --- a/src/Smalot/PdfParser/Pages.php +++ b/src/Smalot/PdfParser/Pages.php @@ -42,25 +42,25 @@ class Pages extends PDFObject */ public function getPages($deep = false) { - if ($this->has('Kids')) { - if (!$deep) { - return $this->get('Kids')->getContent(); - } else { - $kids = $this->get('Kids')->getContent(); - $pages = []; + if (!$this->has('Kids')) { + return []; + } - foreach ($kids as $kid) { - if ($kid instanceof self) { - $pages = array_merge($pages, $kid->getPages(true)); - } else { - $pages[] = $kid; - } - } + if (!$deep) { + return $this->get('Kids')->getContent(); + } - return $pages; + $kids = $this->get('Kids')->getContent(); + $pages = []; + + foreach ($kids as $kid) { + if ($kid instanceof self) { + $pages = array_merge($pages, $kid->getPages(true)); + } else { + $pages[] = $kid; } } - return []; + return $pages; } } diff --git a/src/Smalot/PdfParser/Parser.php b/src/Smalot/PdfParser/Parser.php index d52413d3..8cb66b84 100644 --- a/src/Smalot/PdfParser/Parser.php +++ b/src/Smalot/PdfParser/Parser.php @@ -59,7 +59,7 @@ public function __construct($cfg = []) } /** - * @param $filename + * @param string $filename * * @return Document * @@ -86,8 +86,8 @@ public function parseFile($filename) * * @return Document * - * @throws Exception if secured PDF file was detected - * @throws Exception if no object list was found + * @throws \Exception if secured PDF file was detected + * @throws \Exception if no object list was found */ public function parseContent($content) { @@ -125,7 +125,7 @@ protected function parseTrailer($structure, $document) $name = ucfirst($name); if (is_numeric($values)) { - $trailer[$name] = new ElementNumeric($values, $document); + $trailer[$name] = new ElementNumeric($values); } elseif (\is_array($values)) { $value = $this->parseTrailer($values, null); $trailer[$name] = new ElementArray($value, null); @@ -199,7 +199,7 @@ protected function parseObject($id, $structure, $document) foreach ($positions as $index => $position) { $id = $ids[$index].'_0'; $next_position = isset($positions[$index + 1]) ? $positions[$index + 1] : \strlen($content); - $sub_content = substr($content, $position, $next_position - $position); + $sub_content = substr($content, $position, (int) $next_position - (int) $position); $sub_header = Header::parse($sub_content, $document); $object = PDFObject::factory($document, $sub_header, ''); @@ -255,11 +255,11 @@ protected function parseHeader($structure, $document) } /** - * @param $type - * @param $value - * @param $document + * @param string $type + * @param string|array $value + * @param Document $document * - * @return Element|Header + * @return Element|Header|null * * @throws \Exception */ @@ -271,22 +271,21 @@ protected function parseHeaderElement($type, $value, $document) return $this->parseHeader($value, $document); case 'numeric': - return new ElementNumeric($value, $document); + return new ElementNumeric($value); case 'boolean': - return new ElementBoolean($value, $document); + return new ElementBoolean($value); case 'null': - return new ElementNull($value, $document); + return new ElementNull(); case '(': if ($date = ElementDate::parse('('.$value.')', $document)) { return $date; - } else { - return ElementString::parse('('.$value.')', $document); } - // no break + return ElementString::parse('('.$value.')', $document); + case '<': return $this->parseHeaderElement('(', ElementHexa::decode($value, $document), $document); @@ -300,10 +299,12 @@ protected function parseHeaderElement($type, $value, $document) case '[': $values = []; - foreach ($value as $sub_element) { - $sub_type = $sub_element[0]; - $sub_value = $sub_element[1]; - $values[] = $this->parseHeaderElement($sub_type, $sub_value, $document); + if (\is_array($value)) { + foreach ($value as $sub_element) { + $sub_type = $sub_element[0]; + $sub_value = $sub_element[1]; + $values[] = $this->parseHeaderElement($sub_type, $sub_value, $document); + } } return new ElementArray($values, $document); @@ -312,7 +313,7 @@ protected function parseHeaderElement($type, $value, $document) case 'obj': //I don't know what it means but got my project fixed. case '': // Nothing to do with. - break; + return null; default: throw new \Exception('Invalid type: "'.$type.'".'); diff --git a/src/Smalot/PdfParser/RawData/FilterHelper.php b/src/Smalot/PdfParser/RawData/FilterHelper.php index 3d832055..74334f02 100644 --- a/src/Smalot/PdfParser/RawData/FilterHelper.php +++ b/src/Smalot/PdfParser/RawData/FilterHelper.php @@ -210,7 +210,6 @@ protected function decodeFilterASCII85Decode($data) case 1: throw new Exception('decodeFilterASCII85Decode: invalid code'); - break; } return $decoded; diff --git a/src/Smalot/PdfParser/RawData/RawDataParser.php b/src/Smalot/PdfParser/RawData/RawDataParser.php index 1a1ff817..3df578bb 100644 --- a/src/Smalot/PdfParser/RawData/RawDataParser.php +++ b/src/Smalot/PdfParser/RawData/RawDataParser.php @@ -55,8 +55,7 @@ class RawDataParser ]; protected $filterHelper; - - protected $xrefCache; + protected $objects; /** * @param array $cfg Configuration array, default is [] @@ -467,7 +466,7 @@ protected function decodeXrefStream($pdfData, $startxref, $xref = []) protected function getIndirectObject($pdfData, $xref, $obj_ref, $offset = 0, $decoding = true) { $obj = explode('_', $obj_ref); - if ((false === $obj) or (2 != \count($obj))) { + if (2 != \count($obj)) { throw new Exception('Invalid object reference for $obj.'); } $objref = $obj[0].' '.$obj[1].' obj'; @@ -506,7 +505,7 @@ protected function getIndirectObject($pdfData, $xref, $obj_ref, $offset = 0, $de * Get the content of object, resolving indect object reference if necessary. * * @param string $pdfData PDF data - * @param string $obj Object value + * @param array $obj Object value * * @return array containing object data */ diff --git a/tests/Integration/Element/ElementArrayTest.php b/tests/Integration/Element/ElementArrayTest.php index 183eb6a5..a61b0f5b 100644 --- a/tests/Integration/Element/ElementArrayTest.php +++ b/tests/Integration/Element/ElementArrayTest.php @@ -30,7 +30,7 @@ * If not, see . */ -namespace Tests\Smalot\PDFParser\Integration\Element; +namespace Tests\Smalot\PdfParser\Integration\Element; use Smalot\PdfParser\Document; use Smalot\PdfParser\Element\ElementArray; diff --git a/tests/Integration/Element/ElementMissingTest.php b/tests/Integration/Element/ElementMissingTest.php index cdc69b30..52e35471 100644 --- a/tests/Integration/Element/ElementMissingTest.php +++ b/tests/Integration/Element/ElementMissingTest.php @@ -39,7 +39,7 @@ class ElementMissingTest extends TestCase { public function testEquals() { - $element = new ElementMissing(null); + $element = new ElementMissing(); $this->assertFalse($element->equals(null)); $this->assertFalse($element->equals(true)); $this->assertFalse($element->equals('A')); @@ -48,13 +48,13 @@ public function testEquals() public function testGetContent() { - $element = new ElementMissing(null); + $element = new ElementMissing(); $this->assertFalse($element->getContent()); } public function testContains() { - $element = new ElementMissing(null); + $element = new ElementMissing(); $this->assertFalse($element->contains(null)); $this->assertFalse($element->contains(true)); $this->assertFalse($element->contains('A')); @@ -63,7 +63,7 @@ public function testContains() public function test__toString() { - $element = new ElementMissing(null); + $element = new ElementMissing(); $this->assertEquals('', (string) $element); } } diff --git a/tests/Integration/Element/ElementNullTest.php b/tests/Integration/Element/ElementNullTest.php index c800b1de..d175bcc9 100644 --- a/tests/Integration/Element/ElementNullTest.php +++ b/tests/Integration/Element/ElementNullTest.php @@ -99,13 +99,13 @@ public function testParse() public function testGetContent() { - $element = new ElementNull('null'); + $element = new ElementNull(); $this->assertTrue(null === $element->getContent()); } public function testEquals() { - $element = new ElementNull('null'); + $element = new ElementNull(); $this->assertTrue($element->equals(null)); $this->assertFalse($element->equals(false)); $this->assertFalse($element->equals(0)); @@ -114,7 +114,7 @@ public function testEquals() public function testContains() { - $element = new ElementNull('null'); + $element = new ElementNull(); $this->assertTrue($element->contains(null)); $this->assertFalse($element->contains(false)); $this->assertFalse($element->contains(0)); @@ -122,7 +122,7 @@ public function testContains() public function test__toString() { - $element = new ElementNull('null'); + $element = new ElementNull(); $this->assertEquals('null', (string) $element); } } diff --git a/tests/Integration/Element/ElementStructTest.php b/tests/Integration/Element/ElementStructTest.php index fa97f364..a313e3e3 100644 --- a/tests/Integration/Element/ElementStructTest.php +++ b/tests/Integration/Element/ElementStructTest.php @@ -30,7 +30,7 @@ * If not, see . */ -namespace TestsIntegration\Element; +namespace Tests\Smalot\PdfParser\Integration\Element; use Smalot\PdfParser\Element\ElementStruct; use Smalot\PdfParser\Header; diff --git a/tests/Integration/FontTest.php b/tests/Integration/FontTest.php index 4936e490..573f45df 100644 Binary files a/tests/Integration/FontTest.php and b/tests/Integration/FontTest.php differ diff --git a/tests/Integration/PageTest.php b/tests/Integration/PageTest.php index 7293e6c8..b2bc4bee 100644 --- a/tests/Integration/PageTest.php +++ b/tests/Integration/PageTest.php @@ -414,6 +414,8 @@ public function testGetTextXY() $pages = $document->getPages(); $page = $pages[0]; $result = $page->getTextXY(167, 894, 1, 1); + $this->assertCount(1, $result); + $this->assertCount(2, $result[0]); $this->assertEquals( [ '1',