From 5d2b98270a814a8dd1e09bfc9723d28bee0e956a Mon Sep 17 00:00:00 2001 From: Caleb Mazalevskis Date: Sat, 20 Feb 2021 16:15:07 +0800 Subject: [PATCH] Anchors and __toString patch. Changelog excerpt: - Added support for __toString and for anchors to the YAML handler. - Also updated some PHPDocs. --- Changelog.txt | 3 +++ src/ComplexStringHandler.php | 14 +++++++--- src/YAML.php | 50 +++++++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index b17250f..7ea2646 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -24,6 +24,9 @@ found at: values to the YAML handler, plus some additional aliases for true and false. Also updated some of the test files. +- [2021.02.20; Maikuolan]: Added support for __toString and for anchors to the + YAML handler. Also updated some PHPDocs. + === Version/Release 2.4.0 === MINOR RELEASE. diff --git a/src/ComplexStringHandler.php b/src/ComplexStringHandler.php index 39b5696..1006330 100644 --- a/src/ComplexStringHandler.php +++ b/src/ComplexStringHandler.php @@ -1,6 +1,6 @@ recompile(); diff --git a/src/YAML.php b/src/YAML.php index b8da37a..acd617a 100644 --- a/src/YAML.php +++ b/src/YAML.php @@ -1,6 +1,6 @@ Anchors[$AnchorMatches[1]] = $Value; + $ValueLen = strlen($Value); + $ValueLow = strtolower($Value); + } elseif ( + preg_match('~^\*([\dA-Za-z]+)$~', $Value, $AnchorMatches) && + isset($AnchorMatches[1], $this->Anchors[$AnchorMatches[1]]) + ) { + $Value = $this->Anchors[$AnchorMatches[1]]; + $ValueLen = strlen($Value); + $ValueLow = strtolower($Value); + } + + /** Check for string quotes. */ foreach ([ ['"', '"', 1], ["'", "'", 1], @@ -85,6 +108,7 @@ private function normaliseValue(&$Value, int $ValueLen, $ValueLow) return; } } + if ($ValueLow === 'true' || $ValueLow === 'y' || $Value === '+') { $Value = true; } elseif ($ValueLow === 'false' || $ValueLow === 'n' || $Value === '-') { @@ -201,7 +225,11 @@ public function process(string $In, array &$Arr, int $Depth = 0): bool */ private function processLine(string &$ThisLine, int &$ThisTab, &$Key, &$Value, array &$Arr): bool { - if (substr($ThisLine, -1) === ':' && strpos($ThisLine, ': ') === false) { + if ($ThisLine === '---') { + $Key = '---'; + $Value = false; + $Arr[$Key] = $Value; + } elseif (substr($ThisLine, -1) === ':' && strpos($ThisLine, ': ') === false) { $Key = substr($ThisLine, $ThisTab, -1); $KeyLen = strlen($Key); $KeyLow = strtolower($Key); @@ -317,4 +345,14 @@ public function reconstruct(array $Arr): string $this->processInner($Arr, $Out); return $Out . "\n"; } + + /** + * PHP's magic "__toString" method to act as an alias for "reconstruct". + * + * @return string + */ + public function __toString(): string + { + return $this->reconstruct($this->Data); + } }