| 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\ElementBoolean; |
| 37 |
use Smalot\PdfParser\Element\ElementDate; |
| 38 |
use Smalot\PdfParser\Element\ElementHexa; |
| 39 |
use Smalot\PdfParser\Element\ElementName; |
| 40 |
use Smalot\PdfParser\Element\ElementNull; |
| 41 |
use Smalot\PdfParser\Element\ElementNumeric; |
| 42 |
use Smalot\PdfParser\Element\ElementString; |
| 43 |
use Smalot\PdfParser\Element\ElementStruct; |
| 44 |
use Smalot\PdfParser\Element\ElementXRef; |
| 45 |
|
| 46 |
/** |
| 47 |
* Class Element |
| 48 |
*/ |
| 49 |
class Element |
| 50 |
{ |
| 51 |
/** |
| 52 |
* @var Document|null |
| 53 |
*/ |
| 54 |
protected $document; |
| 55 |
|
| 56 |
protected $value; |
| 57 |
|
| 58 |
public function __construct($value, ?Document $document = null) |
| 59 |
{ |
| 60 |
$this->value = $value; |
| 61 |
$this->document = $document; |
| 62 |
} |
| 63 |
|
| 64 |
public function init() |
| 65 |
{ |
| 66 |
} |
| 67 |
|
| 68 |
public function equals($value): bool |
| 69 |
{ |
| 70 |
return $value == $this->value; |
| 71 |
} |
| 72 |
|
| 73 |
public function contains($value): bool |
| 74 |
{ |
| 75 |
if (\is_array($this->value)) { |
| 76 |
/** @var Element $val */ |
| 77 |
foreach ($this->value as $val) { |
| 78 |
if ($val->equals($value)) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
return $this->equals($value); |
| 87 |
} |
| 88 |
|
| 89 |
public function getContent() |
| 90 |
{ |
| 91 |
return $this->value; |
| 92 |
} |
| 93 |
|
| 94 |
public function __toString(): string |
| 95 |
{ |
| 96 |
return (string) $this->value; |
| 97 |
} |
| 98 |
|
| 99 |
public static function parse(string $content, ?Document $document = null, int &$position = 0) |
| 100 |
{ |
| 101 |
$args = \func_get_args(); |
| 102 |
$only_values = isset($args[3]) ? $args[3] : false; |
| 103 |
$content = trim($content); |
| 104 |
$values = []; |
| 105 |
|
| 106 |
do { |
| 107 |
$old_position = $position; |
| 108 |
|
| 109 |
if (!$only_values) { |
| 110 |
if (!preg_match('/\G\s*(?P<name>\/[A-Z#0-9\._]+)(?P<value>.*)/si', $content, $match, 0, $position)) { |
| 111 |
break; |
| 112 |
} else { |
| 113 |
$name = preg_replace_callback( |
| 114 |
'/#([0-9a-f]{2})/i', |
| 115 |
function ($m) { |
| 116 |
return \chr(base_convert($m[1], 16, 10)); |
| 117 |
}, |
| 118 |
ltrim($match['name'], '/') |
| 119 |
); |
| 120 |
$value = $match['value']; |
| 121 |
$position = strpos($content, $value, $position + \strlen($match['name'])); |
| 122 |
} |
| 123 |
} else { |
| 124 |
$name = \count($values); |
| 125 |
$value = substr($content, $position); |
| 126 |
} |
| 127 |
|
| 128 |
if ($element = ElementName::parse($value, $document, $position)) { |
| 129 |
$values[$name] = $element; |
| 130 |
} elseif ($element = ElementXRef::parse($value, $document, $position)) { |
| 131 |
$values[$name] = $element; |
| 132 |
} elseif ($element = ElementNumeric::parse($value, $document, $position)) { |
| 133 |
$values[$name] = $element; |
| 134 |
} elseif ($element = ElementStruct::parse($value, $document, $position)) { |
| 135 |
$values[$name] = $element; |
| 136 |
} elseif ($element = ElementBoolean::parse($value, $document, $position)) { |
| 137 |
$values[$name] = $element; |
| 138 |
} elseif ($element = ElementNull::parse($value, $document, $position)) { |
| 139 |
$values[$name] = $element; |
| 140 |
} elseif ($element = ElementDate::parse($value, $document, $position)) { |
| 141 |
$values[$name] = $element; |
| 142 |
} elseif ($element = ElementString::parse($value, $document, $position)) { |
| 143 |
$values[$name] = $element; |
| 144 |
} elseif ($element = ElementHexa::parse($value, $document, $position)) { |
| 145 |
$values[$name] = $element; |
| 146 |
} elseif ($element = ElementArray::parse($value, $document, $position)) { |
| 147 |
$values[$name] = $element; |
| 148 |
} else { |
| 149 |
$position = $old_position; |
| 150 |
break; |
| 151 |
} |
| 152 |
} while ($position < \strlen($content)); |
| 153 |
|
| 154 |
return $values; |
| 155 |
} |
| 156 |
} |
| 157 |
|