1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* This file is part of the PdfParser library. |
6
|
|
|
* |
7
|
|
|
* @author Sébastien MALOT <[email protected]> |
8
|
|
|
* @date 2017-01-03 |
9
|
|
|
* |
10
|
|
|
* @license LGPLv3 |
11
|
|
|
* @url <https://github.com/smalot/pdfparser> |
12
|
|
|
* |
13
|
|
|
* PdfParser is a pdf library written in PHP, extraction oriented. |
14
|
|
|
* Copyright (C) 2017 - Sébastien MALOT <[email protected]> |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
18
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
19
|
|
|
* (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Lesser General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
27
|
|
|
* along with this program. |
28
|
|
|
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
namespace Smalot\PdfParser; |
32
|
|
|
|
33
|
|
|
use Smalot\PdfParser\Element\ElementArray; |
34
|
|
|
use Smalot\PdfParser\Element\ElementBoolean; |
35
|
|
|
use Smalot\PdfParser\Element\ElementDate; |
36
|
|
|
use Smalot\PdfParser\Element\ElementHexa; |
37
|
|
|
use Smalot\PdfParser\Element\ElementName; |
38
|
|
|
use Smalot\PdfParser\Element\ElementNull; |
39
|
|
|
use Smalot\PdfParser\Element\ElementNumeric; |
40
|
|
|
use Smalot\PdfParser\Element\ElementString; |
41
|
|
|
use Smalot\PdfParser\Element\ElementStruct; |
42
|
|
|
use Smalot\PdfParser\Element\ElementXRef; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Class Element |
46
|
|
|
*/ |
47
|
|
|
class Element |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @var Document |
51
|
|
|
*/ |
52
|
|
|
protected $document = null; |
53
|
|
|
|
54
|
|
|
protected $value = null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Document $document |
58
|
|
|
*/ |
59
|
74 |
|
public function __construct($value, Document $document = null) |
60
|
|
|
{ |
61
|
74 |
|
$this->value = $value; |
62
|
74 |
|
$this->document = $document; |
63
|
74 |
|
} |
64
|
|
|
|
65
|
18 |
|
public function init() |
66
|
|
|
{ |
67
|
18 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
7 |
|
public function equals($value) |
73
|
|
|
{ |
74
|
7 |
|
return $value == $this->value; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
11 |
|
public function contains($value) |
81
|
|
|
{ |
82
|
11 |
|
if (\is_array($this->value)) { |
83
|
|
|
/** @var Element $val */ |
84
|
4 |
|
foreach ($this->value as $val) { |
85
|
4 |
|
if ($val->equals($value)) { |
86
|
4 |
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
7 |
|
return $this->equals($value); |
94
|
|
|
} |
95
|
|
|
|
96
|
45 |
|
public function getContent() |
97
|
|
|
{ |
98
|
45 |
|
return $this->value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
25 |
|
public function __toString() |
105
|
|
|
{ |
106
|
25 |
|
return (string) ($this->value); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $content |
111
|
|
|
* @param Document $document |
112
|
|
|
* @param int $position |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
* |
116
|
|
|
* @throws \Exception |
117
|
|
|
*/ |
118
|
17 |
|
public static function parse($content, Document $document = null, &$position = 0) |
119
|
|
|
{ |
120
|
17 |
|
$args = \func_get_args(); |
121
|
17 |
|
$only_values = isset($args[3]) ? $args[3] : false; |
122
|
17 |
|
$content = trim($content); |
123
|
17 |
|
$values = []; |
124
|
|
|
|
125
|
|
|
do { |
126
|
17 |
|
$old_position = $position; |
127
|
|
|
|
128
|
17 |
|
if (!$only_values) { |
129
|
15 |
|
if (!preg_match('/^\s*(?P<name>\/[A-Z0-9\._]+)(?P<value>.*)/si', substr($content, $position), $match)) { |
130
|
1 |
|
break; |
131
|
|
|
} else { |
132
|
15 |
|
$name = ltrim($match['name'], '/'); |
133
|
15 |
|
$value = $match['value']; |
134
|
15 |
|
$position = strpos($content, $value, $position + \strlen($match['name'])); |
135
|
|
|
} |
136
|
|
|
} else { |
137
|
7 |
|
$name = \count($values); |
138
|
7 |
|
$value = substr($content, $position); |
139
|
|
|
} |
140
|
|
|
|
141
|
17 |
|
if ($element = ElementName::parse($value, $document, $position)) { |
142
|
15 |
|
$values[$name] = $element; |
143
|
11 |
|
} elseif ($element = ElementXRef::parse($value, $document, $position)) { |
144
|
8 |
|
$values[$name] = $element; |
145
|
7 |
|
} elseif ($element = ElementNumeric::parse($value, $document, $position)) { |
146
|
5 |
|
$values[$name] = $element; |
147
|
5 |
|
} elseif ($element = ElementStruct::parse($value, $document, $position)) { |
148
|
3 |
|
$values[$name] = $element; |
149
|
5 |
|
} elseif ($element = ElementBoolean::parse($value, $document, $position)) { |
150
|
1 |
|
$values[$name] = $element; |
151
|
5 |
|
} elseif ($element = ElementNull::parse($value, $document, $position)) { |
152
|
1 |
|
$values[$name] = $element; |
153
|
5 |
|
} elseif ($element = ElementDate::parse($value, $document, $position)) { |
154
|
3 |
|
$values[$name] = $element; |
155
|
5 |
|
} elseif ($element = ElementString::parse($value, $document, $position)) { |
156
|
3 |
|
$values[$name] = $element; |
157
|
4 |
|
} elseif ($element = ElementHexa::parse($value, $document, $position)) { |
158
|
1 |
|
$values[$name] = $element; |
159
|
4 |
|
} elseif ($element = ElementArray::parse($value, $document, $position)) { |
160
|
4 |
|
$values[$name] = $element; |
161
|
|
|
} else { |
162
|
2 |
|
$position = $old_position; |
163
|
2 |
|
break; |
164
|
|
|
} |
165
|
17 |
|
} while ($position < \strlen($content)); |
166
|
|
|
|
167
|
17 |
|
return $values; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|