This commit is contained in:
Bastian Allgeier
2020-07-07 12:40:13 +02:00
parent 5f025ac2c2
commit f79d2e960c
176 changed files with 10532 additions and 5343 deletions

View File

@@ -2,7 +2,7 @@
namespace Kirby\Data;
use Exception;
use Kirby\Exception\InvalidArgumentException;
use Spyc;
/**
@@ -42,29 +42,33 @@ class Yaml extends Handler
/**
* Parses an encoded YAML string and returns a multi-dimensional array
*
* @param string $yaml
* @param mixed $string
* @return array
*/
public static function decode($yaml): array
public static function decode($string): array
{
if ($yaml === null) {
if ($string === null) {
return [];
}
if (is_array($yaml) === true) {
return $yaml;
if (is_array($string) === true) {
return $string;
}
if (is_string($string) === false) {
throw new InvalidArgumentException('Invalid YAML data; please pass a string');
}
// remove BOM
$yaml = str_replace("\xEF\xBB\xBF", '', $yaml);
$result = Spyc::YAMLLoadString($yaml);
$string = str_replace("\xEF\xBB\xBF", '', $string);
$result = Spyc::YAMLLoadString($string);
if (is_array($result)) {
return $result;
} else {
// apparently Spyc always returns an array, even for invalid YAML syntax
// so this Exception should currently never be thrown
throw new Exception('YAML string is invalid'); // @codeCoverageIgnore
throw new InvalidArgumentException('The YAML data cannot be parsed'); // @codeCoverageIgnore
}
}
}