Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/Document/Dictionary/DictionaryValue/Array/ArrayValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,54 @@ public static function fromValue(string $valueString): self|ReferenceValueArray|
$sanitizedValueString = str_replace(['/', "\n"], [' /', ' '], rtrim(ltrim($sanitizedValueString, '[ '), ' ]'));
$sanitizedValueString = preg_replace('/\s+/', ' ', $sanitizedValueString)
?? throw new RuntimeException('An error occurred while removing duplicate spaces from array value');
$values = explode(' ', $sanitizedValueString);

$values = [];
$buffer = null;
$strlen = strlen($sanitizedValueString);
for ($i = 0; $i < $strlen; $i++) {
$char = $sanitizedValueString[$i];
if ($char === '[') {
if ($buffer !== null) {
$values[] = $buffer;
$buffer = null;
}

$nestingLevel = 0;
for ($j = $i + 1; $j < $strlen; $j++) {
$char = $sanitizedValueString[$j];
if ($char === '[') {
$nestingLevel++;
continue;
}

if ($char === ']') {
if ($nestingLevel !== 0) {
$nestingLevel--;
continue;
}

$values[] = substr($sanitizedValueString, $i, $j - $i + 1);
$i = $j;
continue 2;
}
}
}

if ($char === ' ') {
if ($buffer !== null) {
$values[] = $buffer;
$buffer = null;
}

continue;
}

$buffer .= $char;
}
if ($buffer !== null) {
$values[] = $buffer;
}

if (count($values) % 3 === 0
&& array_key_exists(2, $values)
&& $values[2] === 'R'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ public function testFromValue(): void {
EOD,
),
);
static::assertEquals(
new ArrayValue([1, new ArrayValue([2, 3]), 4]),
ArrayValue::fromValue(
<<<EOD
[ 1 [ 2 3 ] 4 ]
EOD,
),
);
static::assertEquals(
new ArrayValue([1, new ArrayValue([2, 3]), 4]),
ArrayValue::fromValue(
<<<EOD
[1[2 3]4]
EOD,
),
);
static::assertEquals(
new ArrayValue([1, 2, new ArrayValue([3, new ArrayValue([9, 0]), 4]), new ArrayValue([5, 6]), 7, 8]),
ArrayValue::fromValue(
<<<EOD
[1 2 [ 3 [9 0 ] 4
] [ 5 6 ] 7 8]
EOD,
),
);
}

public function testToString(): void {
Expand Down
Loading