| 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\Element; |
| 34 |
|
| 35 |
use Smalot\PdfParser\Document; |
| 36 |
use Smalot\PdfParser\Element; |
| 37 |
use Smalot\PdfParser\Header; |
| 38 |
use Smalot\PdfParser\PDFObject; |
| 39 |
|
| 40 |
/** |
| 41 |
* Class ElementArray |
| 42 |
*/ |
| 43 |
class ElementArray extends Element |
| 44 |
{ |
| 45 |
public function __construct($value, ?Document $document = null) |
| 46 |
{ |
| 47 |
parent::__construct($value, $document); |
| 48 |
} |
| 49 |
|
| 50 |
public function getContent() |
| 51 |
{ |
| 52 |
foreach ($this->value as $name => $element) { |
| 53 |
$this->resolveXRef($name); |
| 54 |
} |
| 55 |
|
| 56 |
return parent::getContent(); |
| 57 |
} |
| 58 |
|
| 59 |
public function getRawContent(): array |
| 60 |
{ |
| 61 |
return $this->value; |
| 62 |
} |
| 63 |
|
| 64 |
public function getDetails(bool $deep = true): array |
| 65 |
{ |
| 66 |
$values = []; |
| 67 |
$elements = $this->getContent(); |
| 68 |
|
| 69 |
foreach ($elements as $key => $element) { |
| 70 |
if ($element instanceof Header && $deep) { |
| 71 |
$values[$key] = $element->getDetails($deep); |
| 72 |
} elseif ($element instanceof PDFObject && $deep) { |
| 73 |
$values[$key] = $element->getDetails(false); |
| 74 |
} elseif ($element instanceof self) { |
| 75 |
if ($deep) { |
| 76 |
$values[$key] = $element->getDetails(); |
| 77 |
} |
| 78 |
} elseif ($element instanceof Element && !($element instanceof self)) { |
| 79 |
$values[$key] = $element->getContent(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return $values; |
| 84 |
} |
| 85 |
|
| 86 |
public function __toString(): string |
| 87 |
{ |
| 88 |
return implode(',', $this->value); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @return Element|PDFObject |
| 93 |
*/ |
| 94 |
protected function resolveXRef(string $name) |
| 95 |
{ |
| 96 |
if (($obj = $this->value[$name]) instanceof ElementXRef) { |
| 97 |
/** @var ElementXRef $obj */ |
| 98 |
$obj = $this->document->getObjectById($obj->getId()); |
| 99 |
$this->value[$name] = $obj; |
| 100 |
} |
| 101 |
|
| 102 |
return $this->value[$name]; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @todo: These methods return mixed and mismatched types throughout the hierarchy |
| 107 |
* |
| 108 |
* @return bool|ElementArray |
| 109 |
*/ |
| 110 |
public static function parse(string $content, ?Document $document = null, int &$offset = 0) |
| 111 |
{ |
| 112 |
if (preg_match('/^\s*\[(?P<array>.*)/is', $content, $match)) { |
| 113 |
preg_match_all('/(.*?)(\[|\])/s', trim($content), $matches); |
| 114 |
|
| 115 |
$level = 0; |
| 116 |
$sub = ''; |
| 117 |
foreach ($matches[0] as $part) { |
| 118 |
$sub .= $part; |
| 119 |
$level += (false !== strpos($part, '[') ? 1 : -1); |
| 120 |
if ($level <= 0) { |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// Removes 1 level [ and ]. |
| 126 |
$sub = substr(trim($sub), 1, -1); |
| 127 |
$sub_offset = 0; |
| 128 |
$values = Element::parse($sub, $document, $sub_offset, true); |
| 129 |
|
| 130 |
$offset += strpos($content, '[') + 1; |
| 131 |
// Find next ']' position |
| 132 |
$offset += \strlen($sub) + 1; |
| 133 |
|
| 134 |
return new self($values, $document); |
| 135 |
} |
| 136 |
|
| 137 |
return false; |
| 138 |
} |
| 139 |
} |
| 140 |
|