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