Skip to content

Commit 7f73d89

Browse files
committed
documentation and testing
1 parent 9bb269c commit 7f73d89

13 files changed

Lines changed: 598 additions & 23 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ endif
203203
## Format the source code
204204
.PHONY: format
205205
format:
206-
./vendor/bin/mago fmt src test
206+
./vendor/bin/mago fmt src test example
207207

208208
## Analyze and Lint the source code
209209
.PHONY: lint

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,3 @@ require_once '/usr/share/php/Com/Tecnick/Pdf/Parser/autoload.php';
105105

106106
Contributions are welcome. Please review [CONTRIBUTING.md](CONTRIBUTING.md), [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md), and [SECURITY.md](SECURITY.md).
107107

108-
---
109-
110-
## Contact
111-
112-
Nicola Asuni - <info@tecnick.com>

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.2.0
1+
3.3.0

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"require": {
2727
"php": ">=8.2",
2828
"ext-pcre": "*",
29-
"tecnickcom/tc-lib-pdf-filter": "^2.2"
29+
"tecnickcom/tc-lib-pdf-filter": "^2.3"
3030
},
3131
"minimum-stability": "dev",
3232
"prefer-stable": true,

example/index.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* index.php
45
*
@@ -14,7 +15,7 @@
1415
*/
1516

1617
// autoloader when using Composer
17-
require(__DIR__ . '/../vendor/autoload.php');
18+
require __DIR__ . '/../vendor/autoload.php';
1819

1920
// autoloader when using RPM or DEB package installation
2021
//require ('/usr/share/php/Com/Tecnick/Pdf/Parser/autoload.php');

resources/debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ Vcs-Browser: https://github.com/~#VENDOR#~/~#PROJECT#~
1212
Package: ~#PKGNAME#~
1313
Provides: php-~#PROJECT#~
1414
Architecture: all
15-
Depends: php (>= 8.2.0), php-tecnickcom-tc-lib-pdf-filter (<< 2.0.0), php-tecnickcom-tc-lib-pdf-filter (>= 2.2.0), ${misc:Depends}
15+
Depends: php (>= 8.2.0), php-tecnickcom-tc-lib-pdf-filter (<< 2.0.0), php-tecnickcom-tc-lib-pdf-filter (>= 2.3.0), ${misc:Depends}
1616
Description: PHP PDF Parser Library
1717
PHP library to parse PDF documents.

resources/rpm/rpm.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ BuildArch: noarch
1616

1717
Requires: php(language) >= 8.2.0
1818
Requires: php-composer(%{c_vendor}/tc-lib-pdf-filter) < 2.0.0
19-
Requires: php-composer(%{c_vendor}/tc-lib-pdf-filter) >= 2.2.0
19+
Requires: php-composer(%{c_vendor}/tc-lib-pdf-filter) >= 2.3.0
2020
Requires: php-pcre
2121

2222
Provides: php-composer(%{c_vendor}/%{gh_project}) = %{version}

src/Process/Xref.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,7 @@ abstract class Xref extends \Com\Tecnick\Pdf\Parser\Process\XrefStream
9797
* @param int $offset Object offset.
9898
* @param bool $decoding If true decode streams.
9999
*
100-
* @return array< int, array{
101-
* 0: string,
102-
* 1: string,
103-
* 2: int,
104-
* 3?: array{string, array<string>},
105-
* }> Object data.
100+
* @return array<int, RawObjectArray> Object data.
106101
*/
107102
abstract protected function getIndirectObject(string $obj_ref, int $offset = 0, bool $decoding = true): array;
108103

test/ParserHarness.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ public function getDecodedStreamPublic(array $filters, string $stream, array $pa
179179
return $this->getDecodedStream($filters, $stream, $params);
180180
}
181181

182+
/**
183+
* @param array<int, RawObjectArray> $sdic
184+
*
185+
* @return array{0:string,1:array<string>}
186+
*
187+
* @throws \Com\Tecnick\Pdf\Parser\Exception
188+
*/
189+
public function decodeStreamPublic(array $sdic, string $stream): array
190+
{
191+
return $this->decodeStream($sdic, $stream);
192+
}
193+
182194
/**
183195
* @return array<int, RawObjectArray>
184196
*

test/ParserProcessingTest.php

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,199 @@ public function testGetDecodedStreamThrowsWhenFilterErrorsAreNotIgnored(): void
208208
$this->expectException(PPException::class);
209209
$parser->getDecodedStreamPublic(['UnknownFilter'], 'sample-data');
210210
}
211+
212+
/**
213+
* @throws \Com\Tecnick\Pdf\Parser\Exception
214+
*/
215+
public function testParentIndirectObjectFindsObjectAfterOffsetShift(): void
216+
{
217+
$parser = new ParserHarness();
218+
$parser->setPdfDataPublic("%PDF-1.7\nX1 0 obj\nendobj\n");
219+
$parser->setRawObjectQueue([
220+
['numeric', '7', 16],
221+
['endobj', 'endobj', 22],
222+
]);
223+
224+
$obj = $parser->callParentGetIndirectObject('1_0', 9, true);
225+
226+
$this->assertSame([['numeric', '7', 16]], $obj);
227+
}
228+
229+
/**
230+
* @throws \Com\Tecnick\Pdf\Parser\Exception
231+
*/
232+
public function testGetFiltersHandlesMissingAndInvalidArrayPayload(): void
233+
{
234+
$parser = new ParserHarness();
235+
236+
$filters = ['FlateDecode'];
237+
$this->assertSame($filters, $parser->getFiltersPublic($filters, [['/', 'Filter', 0]], 0));
238+
239+
$invalid = [
240+
['/', 'Filter', 0],
241+
['[', 'invalid', 0],
242+
];
243+
$this->assertSame([], $parser->getFiltersPublic([], $invalid, 0));
244+
}
245+
246+
/**
247+
* @throws \Com\Tecnick\Pdf\Parser\Exception
248+
*/
249+
public function testGetDecodeParmsHandlesDictionaryArrayAndMissingValues(): void
250+
{
251+
$parser = new ParserHarness();
252+
253+
$this->assertSame([], $parser->getDecodeParmsPublic([['/', 'DecodeParms', 0]], 0));
254+
255+
$dict = [
256+
['/', 'DecodeParms', 0],
257+
[
258+
'<<',
259+
[
260+
['/', 'Columns', 0],
261+
['numeric', '5', 0],
262+
['/', 'EarlyChange', 0],
263+
['true', 'true', 0],
264+
['/', 'FilterName', 0],
265+
['/', 'FlateDecode', 0],
266+
['/', 'Text', 0],
267+
['string', 'abc', 0],
268+
['/', 'Ignored', 0],
269+
['[', [], 0],
270+
],
271+
0,
272+
],
273+
];
274+
275+
$this->assertSame(
276+
[
277+
'Columns' => 5,
278+
'EarlyChange' => true,
279+
'FilterName' => 'FlateDecode',
280+
'Text' => 'abc',
281+
],
282+
$parser->getDecodeParmsPublic($dict, 0),
283+
);
284+
285+
$array = [
286+
['/', 'DecodeParms', 0],
287+
[
288+
'[',
289+
[
290+
['null', 'null', 0],
291+
[
292+
'<<',
293+
[
294+
['/', 'Rows', 0],
295+
['numeric', '2', 0],
296+
['/', 'Enabled', 0],
297+
['false', 'false', 0],
298+
],
299+
0,
300+
],
301+
],
302+
0,
303+
],
304+
];
305+
306+
$this->assertSame(
307+
[
308+
'Rows' => 2,
309+
'Enabled' => false,
310+
],
311+
$parser->getDecodeParmsPublic($array, 0),
312+
);
313+
}
314+
315+
/**
316+
* @throws \Com\Tecnick\Pdf\Parser\Exception
317+
*/
318+
public function testDecodeStreamHandlesEmptyStreamAndDecodeParmsExtraction(): void
319+
{
320+
$parser = new ParserHarness(['ignore_filter_errors' => true]);
321+
322+
$this->assertSame(['', []], $parser->decodeStreamPublic([], ''));
323+
324+
$sdic = [
325+
['/', 'Filter', 0],
326+
['/', 'UnknownFilter', 0],
327+
['/', 'DecodeParms', 0],
328+
[
329+
'<<',
330+
[
331+
['/', 'Columns', 0],
332+
['numeric', '3', 0],
333+
['/', 'Predictor', 0],
334+
['numeric', '12', 0],
335+
],
336+
0,
337+
],
338+
];
339+
340+
$result = $parser->decodeStreamPublic($sdic, 'abc');
341+
$this->assertSame('abc', $result[0]);
342+
$this->assertSame(['UnknownFilter'], $result[1]);
343+
}
344+
345+
/**
346+
* @throws \Com\Tecnick\Pdf\Parser\Exception
347+
*/
348+
public function testParentIndirectObjectReturnsNullObjectWhenSearchMissesTwice(): void
349+
{
350+
$parser = new ParserHarness();
351+
$parser->setPdfDataPublic("%PDF-1.7\nno objects\n");
352+
353+
$obj = $parser->callParentGetIndirectObject('1_0', 2, true);
354+
355+
$this->assertSame([['null', 'null', 3]], $obj);
356+
}
357+
358+
/**
359+
* @throws \Com\Tecnick\Pdf\Parser\Exception
360+
*/
361+
public function testGetFiltersSkipsSlashEntriesWithNonStringNames(): void
362+
{
363+
$parser = new ParserHarness();
364+
$sdic = [
365+
['/', 'Filter', 0],
366+
[
367+
'[',
368+
[
369+
['/', [['numeric', '1', 0]], 0],
370+
['/', 'FlateDecode', 0],
371+
],
372+
0,
373+
],
374+
];
375+
376+
$filters = $parser->getFiltersPublic([], $sdic, 0);
377+
$this->assertSame(['FlateDecode'], $filters);
378+
}
379+
380+
/**
381+
* @throws \Com\Tecnick\Pdf\Parser\Exception
382+
*/
383+
public function testGetDecodeParmsSkipsInvalidPairsAcrossLoopChecks(): void
384+
{
385+
$parser = new ParserHarness();
386+
$sdic = [
387+
['/', 'DecodeParms', 0],
388+
[
389+
'<<',
390+
[
391+
['numeric', '0', 0],
392+
['numeric', '1', 0],
393+
['/', 'MissingValue', 0],
394+
['null', 'null', 0],
395+
['/', 'Valid', 0],
396+
['numeric', '7', 0],
397+
['/', 'DanglingKey', 0],
398+
],
399+
0,
400+
],
401+
];
402+
403+
$params = $parser->getDecodeParmsPublic($sdic, 0);
404+
$this->assertSame(['Valid' => 7], $params);
405+
}
211406
}

0 commit comments

Comments
 (0)