$dictionaryValue * @throws PdfParserException */ public static function fromKeyValuePair(string $keyString, string|array $dictionaryValue): ?DictionaryEntry { $dictionaryKey = DictionaryKey::tryFromKeyString($keyString) ?? ExtendedDictionaryKey::fromKeyString($keyString); return new DictionaryEntry($dictionaryKey, self::getValue($dictionaryKey, $dictionaryValue)); } /** * @param string|array $value * @throws PdfParserException */ protected static function getValue(DictionaryKey|ExtendedDictionaryKey $dictionaryKey, string|array $value): Dictionary|DictionaryValue|NameValue { $allowedValueTypes = $dictionaryKey->getValueTypes(); if ((in_array(Dictionary::class, $allowedValueTypes, true) || in_array(ArrayValue::class, $allowedValueTypes, true)) && is_array($value)) { return DictionaryFactory::fromArray($value); } if ((in_array(Dictionary::class, $allowedValueTypes, true) || in_array(ArrayValue::class, $allowedValueTypes, true)) && is_string($value) && preg_match('/^[0-9]+ [0-9]+ R$/', $value) === 1 && ($referenceValue = ReferenceValue::fromValue($value)) !== null) { return $referenceValue; } foreach ($allowedValueTypes as $allowedValueType) { if (is_a($allowedValueType, BackedEnum::class, true) && is_string($value) && ($resolvedValue = $allowedValueType::tryFrom(NameValueNormalizer::normalize($value))) !== null) { return $resolvedValue; } } foreach ($allowedValueTypes as $allowedValueType) { if (!is_a($allowedValueType, DictionaryValue::class, true) || $allowedValueType === TextStringValue::class) { // TextStrings accept everything, so we check that last continue; } if (!is_string($value) || ($valueObject = $allowedValueType::fromValue($value)) === null) { continue; } return $valueObject; } if (in_array(TextStringValue::class, $allowedValueTypes, true) && is_string($value)) { return TextStringValue::fromValue($value); } throw new ParseFailureException(sprintf('Value "%s" for dictionary key %s could not be parsed to a valid value type', is_array($value) ? 'array()' : $value, $dictionaryKey->value)); } }