Skip to content

Commit 758b260

Browse files
committed
YAML handler update.
Changelog excerpt: - Added support for !php/const tags to the YAML handler.
1 parent 407662f commit 758b260

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

.tests/YAMLTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,6 @@
606606
exit($ExitCode);
607607
}
608608

609-
$Object->AllowObjectUnserialize = true;
610609
$Object->AllowObjectSerialize = true;
611610
require_once $ClassesDir . 'Context.php';
612611
$Actual = new \Maikuolan\Common\Context();
@@ -625,9 +624,20 @@
625624
exit($ExitCode);
626625
}
627626

627+
$Object->AllowObjectUnserialize = true;
628628
$ProcessResult = $Object->process($Reconstructed, $Object->Data);
629629
$ExitCode++;
630630
if ($ProcessResult !== true || !isset($Object->Data['Serialisation test']) || !($Object->Data['Serialisation test'] instanceof \Maikuolan\Common\Context)) {
631631
echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL;
632632
exit($ExitCode);
633633
}
634+
635+
$RawYAML = 'Foo: "bar"
636+
Baz: !php/const PHP_INT_MAX
637+
';
638+
$Object = new \Maikuolan\Common\YAML($RawYAML);
639+
$ExitCode++;
640+
if (!isset($Object->Data['Baz']) || $Object->Data['Baz'] !== PHP_INT_MAX) {
641+
echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL;
642+
exit($ExitCode);
643+
}

Changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ found at:
2020
when enabled via the appropriate properties of the instance. When
2121
reconstructing strings, if a string isn't UTF-8 (e.g., binary sourced from
2222
images, archives, etc), it'll be rendered as base-64 with the !!binary tag
23-
attached.
23+
attached. Added support for !php/const tags to the YAML handler.
2424

2525
=== Version/Release 2.14.3 ===
2626
PATCH RELEASE.

_docs/YAML.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ The overall specification is quite extensive, and writing this documentation tak
708708
__Tags (specific methods implemented)__ | __Description__
709709
:--|:--
710710
`!flatten` | Flattens a multidimensional array down to a single depth (similar to merge, but rather than merging the array to the parent collection, it merges all the sub-arrays into the array being worked upon).
711+
`!php/const` | Returns a PHP constant (e.g., `!php/const PHP_INT_MAX`).
711712
`!php/object` | Allows unserialisation of PHP serialised data (when `AllowObjectUnserialize` is set to `true`; is set to `false` by default).
712713
__Tags (directly invokes PHP functions at `coerce`)__ | __Description__
713714
`!abs` | Uses PHP's `abs()` function to process the entry.

src/YAML.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,11 +1153,9 @@ private function coerce($Value, bool $EnforceScalar, string $Tag)
11531153
return $Arr;
11541154
}
11551155

1156-
/**
1157-
* Unserialising a PHP object.
1158-
*/
1159-
if ($Tag === 'php/object' && $this->AllowObjectUnserialize && is_string($Value) && $Value !== '') {
1160-
return unserialize($Value);
1156+
/** Unserialising a PHP object. */
1157+
if ($Tag === 'php/object') {
1158+
return $this->AllowObjectUnserialize && is_string($Value) && $Value !== '' ? unserialize($Value) : $Value;
11611159
}
11621160

11631161
/** For extending with other non-scalar coercion. */
@@ -1249,10 +1247,12 @@ private function coerce($Value, bool $EnforceScalar, string $Tag)
12491247
* @link https://yaml.org/type/binary.html
12501248
*/
12511249
if ($Tag === '!binary') {
1252-
if ($Value === '' || !is_string($Value)) {
1253-
return '';
1254-
}
1255-
return base64_decode(preg_replace('~\s~', '', $Value));
1250+
return is_string($Value) && $Value !== '' ? base64_decode(preg_replace('~\s~', '', $Value)) : '';
1251+
}
1252+
1253+
/** PHP constants. */
1254+
if ($Tag === 'php/const') {
1255+
return is_string($Value) && $Value !== '' && defined($Value) ? constant($Value) : $Value;
12561256
}
12571257

12581258
/** For extending with other scalar coercion. */

0 commit comments

Comments
 (0)