Skip to content

Commit

Permalink
YAML handler update.
Browse files Browse the repository at this point in the history
Changelog excerpt:
- Added list functionality to the YAML handler (i.e., using flow sequences
  in keys to assign a singular value to all of those keys at once, or to
  determine the keys to use to convert corresponding flow sequences in
  values to flow maps).
  • Loading branch information
Maikuolan committed Jul 16, 2024
1 parent c444f25 commit 0f54ede
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
11 changes: 11 additions & 0 deletions .tests/YAMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@
]
]
],
'List support' => [
'aa' => 'All keys same value.',
'ab' => 'All keys same value.',
'ac' => 'All keys same value.',
'ad' => 'All keys same value.',
'ba' => 'ww',
'bb' => 'xx',
'bc' => 'yy',
'bd' => 'zz',
'[ca, cb, cc, cd]' => ['vv', 'ww', 'xx', 'yy', 'zz'],
],
'End of file' => ':-)'
];

Expand Down
4 changes: 4 additions & 0 deletions .tests/fixtures/syntax.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,8 @@ Specification examples:
stats: |
65 Home Runs
0.278 Batting Average
List support:
[aa, ab, ac, ad]: "All keys same value."
[ba, bb, bc, bd]: ["ww", "xx", "yy", "zz"] # Key-value pairs balanced; Values assigned as flow map to parent using corresponding keys.
[ca, cb, cc, cd]: ["vv", "ww", "xx", "yy", "zz"] # Key-value pairs NOT balanced; Values assigned as flow sequence named as the literal key.
End of file: ":-)"
5 changes: 5 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ found at:
- [2024.07.02; Bug-fix; Maikuolan]: 3-digit and script-based codes not
recognised by arrayFromL10nToArray; Fixed.

- [2024.07.16; Maikuolan]: Added list functionality to the YAML handler (i.e.,
using flow sequences in keys to assign a singular value to all of those keys
at once, or to determine the keys to use to convert corresponding flow
sequences in values to flow maps).

=== Version/Release 2.12.1 ===
PATCH RELEASE.

Expand Down
36 changes: 30 additions & 6 deletions src/YAML.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* YAML handler (last modified: 2023.12.08).
* YAML handler (last modified: 2024.07.16).
*
* This file is a part of the "common classes package", utilised by a number of
* packages and projects, including CIDRAM and phpMussel.
Expand Down Expand Up @@ -98,7 +98,7 @@ class YAML extends CommonAbstract
private $MultiLineFolded = false;

/**
* @var string Whether to use chomping for the current multiline block.
* @var string Whether to use chomping for the current multi-line block.
*/
private $Chomp = '';

Expand Down Expand Up @@ -264,7 +264,7 @@ public function process(string $In, array &$Arr, int $Depth = 0, bool $Refs = fa

/** Strip comments and whitespace. */
if (!($ThisLine = preg_replace(['/(?<!\\\\)#.*$/', '/\s+$/'], '', $ThisLine))) {
/** Line preservation for multiline and folded blocks. .*/
/** Line preservation for multi-line and folded blocks. .*/
if (($this->MultiLine || $this->MultiLineFolded) && strlen($SendTo)) {
$SendTo .= "\n";
}
Expand All @@ -287,7 +287,7 @@ public function process(string $In, array &$Arr, int $Depth = 0, bool $Refs = fa

/**
* Data indented further than the current depth can be gathered to
* be processed recursively (e.g., sequences, multiline data, etc).
* be processed recursively (e.g., sequences, multi-line data, etc).
*/
if ($ThisTab > $Depth) {
if ($TabLen === 0) {
Expand Down Expand Up @@ -637,6 +637,13 @@ private function processLine(string &$ThisLine, int &$ThisTab, &$Key, &$Value, a
$Key = $this->arrayKeyLast($Arr);
} elseif (($DelPos = strpos($ThisLine, ': ')) !== false) {
$Key = substr($ThisLine, $ThisTab, $DelPos - $ThisTab);
if (substr($Key, 0, 1) === '[' && substr($Key, -1) === ']') {
$TryList = [];
$this->flowControl($Key, $TryList, '[', true);
$TryListCount = count($TryList);
} else {
$TryListCount = 0;
}
$KeyLen = strlen($Key);
$this->normaliseValue($Key, true);
if (!$Key) {
Expand All @@ -651,6 +658,17 @@ private function processLine(string &$ThisLine, int &$ThisTab, &$Key, &$Value, a
if ($ValueLen > 0) {
if (($this->LastResolvedTag === '!merge' || $Key === '<<') && is_array($Value)) {
$Arr += $this->merge($Value);
} elseif ($TryListCount !== 0 && (!is_array($Value) || count($Value) === $TryListCount)) {
if (is_array($Value)) {
$Values = $Value;
foreach ($TryList as $Key) {
$Arr[$Key] = array_shift($Values);
}
} else {
foreach ($TryList as $Key) {
$Arr[$Key] = $Value;
}
}
} else {
$Arr[$Key] = $Value;
}
Expand Down Expand Up @@ -1184,9 +1202,10 @@ private function merge(array $Arr): array
* @param array $Arr Where to process that data.
* @param string $Brace The type of bracing used (determines whether the
* data should be processed as a flow sequence or as flow mappings).
* @param bool $SequencesOnly Whether to restrict operation to sequences only.
* @return bool True for process success. False for process failure.
*/
private function flowControl(string $In, array &$Arr, string $Brace): bool
private function flowControl(string $In, array &$Arr, string $Brace, bool $SequencesOnly = false): bool
{
/** Reset the array where we're processing the data. */
$Arr = [];
Expand Down Expand Up @@ -1219,11 +1238,16 @@ private function flowControl(string $In, array &$Arr, string $Brace): bool
}

foreach ($Arr as &$Working) {
$this->normaliseValue($Working);
$this->normaliseValue($Working, $SequencesOnly);
}
return true;
}

/** Guard. */
if ($SequencesOnly) {
return false;
}

/** Flow mappings. */
if ($Brace === '{') {
$Split = explode(',', substr($In, 1, -1));
Expand Down

0 comments on commit 0f54ede

Please sign in to comment.