[Yaml] Add flag to dump NULL as ~
This commit is contained in:
parent
b5f592e06c
commit
749c11d94c
@ -1,6 +1,11 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
4.4.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Added support to dump `null` as `~` by using the `Yaml::DUMP_NULL_AS_TILDE` flag.
|
||||||
|
|
||||||
4.3.0
|
4.3.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@ class Inline
|
|||||||
private static $objectSupport = false;
|
private static $objectSupport = false;
|
||||||
private static $objectForMap = false;
|
private static $objectForMap = false;
|
||||||
private static $constantSupport = false;
|
private static $constantSupport = false;
|
||||||
|
private static $nullAsTilde = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $flags
|
* @param int $flags
|
||||||
@ -45,6 +46,7 @@ class Inline
|
|||||||
self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
|
self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
|
||||||
self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
|
self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
|
||||||
self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags);
|
self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags);
|
||||||
|
self::$nullAsTilde = (bool) (Yaml::DUMP_NULL_AS_TILDE & $flags);
|
||||||
self::$parsedFilename = $parsedFilename;
|
self::$parsedFilename = $parsedFilename;
|
||||||
|
|
||||||
if (null !== $parsedLineNumber) {
|
if (null !== $parsedLineNumber) {
|
||||||
@ -129,7 +131,7 @@ class Inline
|
|||||||
throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
|
throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'null';
|
return self::dumpNull($flags);
|
||||||
case $value instanceof \DateTimeInterface:
|
case $value instanceof \DateTimeInterface:
|
||||||
return $value->format('c');
|
return $value->format('c');
|
||||||
case \is_object($value):
|
case \is_object($value):
|
||||||
@ -155,11 +157,11 @@ class Inline
|
|||||||
throw new DumpException('Object support when dumping a YAML file has been disabled.');
|
throw new DumpException('Object support when dumping a YAML file has been disabled.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'null';
|
return self::dumpNull($flags);
|
||||||
case \is_array($value):
|
case \is_array($value):
|
||||||
return self::dumpArray($value, $flags);
|
return self::dumpArray($value, $flags);
|
||||||
case null === $value:
|
case null === $value:
|
||||||
return 'null';
|
return self::dumpNull($flags);
|
||||||
case true === $value:
|
case true === $value:
|
||||||
return 'true';
|
return 'true';
|
||||||
case false === $value:
|
case false === $value:
|
||||||
@ -256,6 +258,15 @@ class Inline
|
|||||||
return sprintf('{ %s }', implode(', ', $output));
|
return sprintf('{ %s }', implode(', ', $output));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function dumpNull(int $flags): string
|
||||||
|
{
|
||||||
|
if (Yaml::DUMP_NULL_AS_TILDE & $flags) {
|
||||||
|
return '~';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'null';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses a YAML scalar.
|
* Parses a YAML scalar.
|
||||||
*
|
*
|
||||||
|
@ -513,6 +513,11 @@ YAML;
|
|||||||
{
|
{
|
||||||
new Dumper(-4);
|
new Dumper(-4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDumpNullAsTilde()
|
||||||
|
{
|
||||||
|
$this->assertSame('{ foo: ~ }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class A
|
class A
|
||||||
|
@ -33,6 +33,7 @@ class Yaml
|
|||||||
const PARSE_CONSTANT = 256;
|
const PARSE_CONSTANT = 256;
|
||||||
const PARSE_CUSTOM_TAGS = 512;
|
const PARSE_CUSTOM_TAGS = 512;
|
||||||
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
|
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
|
||||||
|
const DUMP_NULL_AS_TILDE = 2048;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses a YAML file into a PHP value.
|
* Parses a YAML file into a PHP value.
|
||||||
|
Reference in New Issue
Block a user