|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file This file is part of the PdfParser library. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Konrad Abicht <[email protected]> |
|
7
|
|
|
* @date 2020-06-01 |
|
8
|
|
|
* |
|
9
|
|
|
* @author Sébastien MALOT <[email protected]> |
|
10
|
|
|
* @date 2017-01-03 |
|
11
|
|
|
* |
|
12
|
|
|
* @license LGPLv3 |
|
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 <[email protected]> |
|
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 Tests\Smalot\PdfParser\Integration; |
|
34
|
|
|
|
|
35
|
|
|
use Smalot\PdfParser\Element; |
|
36
|
|
|
use Smalot\PdfParser\Element\ElementArray; |
|
37
|
|
|
use Smalot\PdfParser\Element\ElementBoolean; |
|
38
|
|
|
use Smalot\PdfParser\Element\ElementDate; |
|
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\ElementXRef; |
|
44
|
|
|
use Smalot\PdfParser\Header; |
|
45
|
|
|
use Tests\Smalot\PdfParser\TestCase; |
|
46
|
|
|
|
|
47
|
|
|
class ElementTest extends TestCase |
|
48
|
|
|
{ |
|
49
|
|
|
public function testParse() |
|
50
|
|
|
{ |
|
51
|
|
|
$document = $this->getDocumentInstance(); |
|
52
|
|
|
|
|
53
|
|
|
// Only_values = false. |
|
54
|
|
|
$content = '/NameType /FlateDecode |
|
55
|
|
|
/Contents[4 0 R 42]/Fonts<</F1 41/F2 43>>/NullType |
|
56
|
|
|
null/StringType(hello)/DateType(D:20130901235555+02\'00\')/XRefType 2 0 R |
|
57
|
|
|
/NumericType 8/HexaType<0020>/BooleanType false'; |
|
58
|
|
|
$offset = 0; |
|
59
|
|
|
|
|
60
|
|
|
$elements = Element::parse($content, $document, $offset, false); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertTrue(\array_key_exists('NameType', $elements)); |
|
63
|
|
|
$this->assertTrue($elements['NameType'] instanceof ElementName); |
|
64
|
|
|
$this->assertEquals('FlateDecode', $elements['NameType']->getContent()); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertTrue(\array_key_exists('Contents', $elements)); |
|
67
|
|
|
$this->assertTrue($elements['Contents'] instanceof ElementArray); |
|
68
|
|
|
$this->assertTrue($elements['Contents']->contains(42)); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertTrue(\array_key_exists('Fonts', $elements)); |
|
71
|
|
|
$this->assertTrue($elements['Fonts'] instanceof Header); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertTrue(\array_key_exists('NullType', $elements)); |
|
74
|
|
|
$this->assertTrue($elements['NullType'] instanceof ElementNull); |
|
75
|
|
|
$this->assertEquals('null', (string) $elements['NullType']); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertTrue(\array_key_exists('StringType', $elements)); |
|
78
|
|
|
$this->assertTrue($elements['StringType'] instanceof ElementString); |
|
79
|
|
|
$this->assertEquals('hello', $elements['StringType']->getContent()); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertTrue(\array_key_exists('DateType', $elements)); |
|
82
|
|
|
$this->assertTrue($elements['DateType'] instanceof ElementDate); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertTrue(\array_key_exists('XRefType', $elements)); |
|
85
|
|
|
$this->assertTrue($elements['XRefType'] instanceof ElementXRef); |
|
86
|
|
|
$this->assertEquals('2_0', $elements['XRefType']->getId()); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertTrue(\array_key_exists('NumericType', $elements)); |
|
89
|
|
|
$this->assertTrue($elements['NumericType'] instanceof ElementNumeric); |
|
90
|
|
|
$this->assertEquals('8', (string) $elements['NumericType']); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertTrue(\array_key_exists('HexaType', $elements)); |
|
93
|
|
|
$this->assertTrue($elements['HexaType'] instanceof ElementString); |
|
94
|
|
|
$this->assertEquals(' ', (string) $elements['HexaType']); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertTrue(\array_key_exists('BooleanType', $elements)); |
|
97
|
|
|
$this->assertTrue($elements['BooleanType'] instanceof ElementBoolean); |
|
98
|
|
|
$this->assertFalse($elements['BooleanType']->getContent()); |
|
99
|
|
|
|
|
100
|
|
|
// Only_values = true. |
|
101
|
|
|
$content = '/NameType /FlateDecode'; |
|
102
|
|
|
$offset = 0; |
|
103
|
|
|
$elements = Element::parse($content, $document, $offset, true); |
|
104
|
|
|
$this->assertEquals(2, \count($elements)); |
|
105
|
|
|
$this->assertEquals(22, $offset); |
|
106
|
|
|
|
|
107
|
|
|
// Test error. |
|
108
|
|
|
$content = '/NameType /FlateDecode $$$'; |
|
109
|
|
|
$offset = 0; |
|
110
|
|
|
$elements = Element::parse($content, $document, $offset, false); |
|
111
|
|
|
$this->assertEquals(1, \count($elements)); |
|
112
|
|
|
$this->assertEquals(22, $offset); |
|
113
|
|
|
$this->assertEquals('NameType', key($elements)); |
|
114
|
|
|
$this->assertTrue(current($elements) instanceof ElementName); |
|
115
|
|
|
|
|
116
|
|
|
$content = '/NameType $$$'; |
|
117
|
|
|
$offset = 0; |
|
118
|
|
|
$elements = Element::parse($content, $document, $offset, false); |
|
119
|
|
|
$this->assertEquals(0, $offset); |
|
120
|
|
|
$this->assertEquals(0, \count($elements)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testGetContent() |
|
124
|
|
|
{ |
|
125
|
|
|
$element = $this->getElementInstance(42); |
|
126
|
|
|
$content = $element->getContent(); |
|
127
|
|
|
$this->assertEquals(42, $content); |
|
128
|
|
|
|
|
129
|
|
|
$element = $this->getElementInstance([4, 2]); |
|
130
|
|
|
$this->assertEquals(2, \count($element->getContent())); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testEquals() |
|
134
|
|
|
{ |
|
135
|
|
|
$element = $this->getElementInstance(2); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertTrue($element->equals(2)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testContains() |
|
141
|
|
|
{ |
|
142
|
|
|
$element = $this->getElementInstance([$this->getElementInstance(4), $this->getElementInstance(2)]); |
|
143
|
|
|
|
|
144
|
|
|
$this->assertTrue($element->contains(2)); |
|
145
|
|
|
$this->assertFalse($element->contains(8)); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function testToString() |
|
149
|
|
|
{ |
|
150
|
|
|
$this->assertEquals((string) $this->getElementInstance('2'), '2'); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|