3.4.0
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user