Skip to content

Commit 6eedf2e

Browse files
authored
Extend auto resolving of reference value to nameValues and dictionaries (#383)
1 parent 90af6ed commit 6eedf2e

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/Document/Dictionary/Dictionary.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,18 @@ public function getValueForKey(?Document $document, DictionaryKey|ExtendedDictio
4848
?? throw new InvalidArgumentException('Value type is dictionary but subdictionary not found');
4949
}
5050

51-
if ($value instanceof ReferenceValue && $document !== null && $expectedValueType !== ReferenceValue::class && is_a($expectedValueType, DictionaryValue::class, true)) {
52-
$value = $expectedValueType::fromValue(
53-
$content = trim(($document->getObject($value->objectNumber) ?? throw new InvalidArgumentException(sprintf('Object with nr %d not found', $value->objectNumber)))
54-
->getStream()->toString()),
55-
) ?? throw new ParseFailureException(sprintf('Unable to parse content "%s" of referenced object %d as %s', $content, $value->objectNumber, $expectedValueType));
51+
if ($value instanceof ReferenceValue && $document !== null && $expectedValueType !== ReferenceValue::class) {
52+
$content = ($document->getObject($value->objectNumber) ?? throw new InvalidArgumentException(sprintf('Object with nr %d not found', $value->objectNumber)))
53+
->getStream();
54+
if ($expectedValueType === Dictionary::class) {
55+
$value = DictionaryParser::parse(null, $content, 0, $content->getSizeInBytes());
56+
} elseif (is_a($expectedValueType, NameValue::class, true)) {
57+
$value = $expectedValueType::tryFrom(trim($content->toString()))
58+
?? throw new ParseFailureException(sprintf('Unable to parse content "%s" of referenced object %d as %s', $content->toString(), $value->objectNumber, $expectedValueType));
59+
} elseif (is_a($expectedValueType, DictionaryValue::class, true)) {
60+
$value = $expectedValueType::fromValue(trim($content->toString()))
61+
?? throw new ParseFailureException(sprintf('Unable to parse content "%s" of referenced object %d as %s', $content->toString(), $value->objectNumber, $expectedValueType));
62+
}
5663
}
5764

5865
if (is_a($value, $expectedValueType) === false) {

src/Document/Dictionary/DictionaryValue/Name/NameValue.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
namespace PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Name;
55

66
/** @api */
7-
interface NameValue {}
7+
interface NameValue {
8+
public static function tryFrom(string $value): ?static;
9+
}

0 commit comments

Comments
 (0)