From 238589d4e8135f896dd0cbc16fd1853bf6476422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Sun, 19 Apr 2015 18:34:12 +0200 Subject: [PATCH 01/12] Add $objectForMap as argument of Yaml::parse() --- src/Symfony/Component/Yaml/Yaml.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index c9295b094bb0a..e17698a07c74f 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -41,6 +41,7 @@ class Yaml * @param string $input Path to a YAML file or a string containing YAML * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise * @param bool $objectSupport True if object support is enabled, false otherwise + * @param bool $objectForMap True if maps should return a stdClass instead of array() * * @return array The YAML converted to a PHP array * @@ -50,7 +51,7 @@ class Yaml * * @api */ - public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false) + public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false) { // if input is a file, process it $file = ''; @@ -68,7 +69,7 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup $yaml = new Parser(); try { - return $yaml->parse($input, $exceptionOnInvalidType, $objectSupport); + return $yaml->parse($input, $exceptionOnInvalidType, $objectSupport, $objectForMap); } catch (ParseException $e) { if ($file) { $e->setParsedFile($file); From 3daae6481fb0128d0099c2c92a56716f8a4afce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Mon, 20 Apr 2015 12:26:39 +0200 Subject: [PATCH 02/12] [Yaml] Add an option to parse timestamps as DateTime --- src/Symfony/Component/Yaml/Inline.php | 8 +++++- src/Symfony/Component/Yaml/Parser.php | 28 ++++++++++--------- .../Component/Yaml/Tests/InlineTest.php | 21 ++++++++++++++ src/Symfony/Component/Yaml/Yaml.php | 5 ++-- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 52ea724cf4070..5ab08e1feb3e0 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -26,6 +26,7 @@ class Inline private static $exceptionOnInvalidType = false; private static $objectSupport = false; private static $objectForMap = false; + private static $timestampAsDateTime = false; /** * Converts a YAML string to a PHP array. @@ -35,16 +36,18 @@ class Inline * @param bool $objectSupport true if object support is enabled, false otherwise * @param bool $objectForMap true if maps should return a stdClass instead of array() * @param array $references Mapping of variable names to values + * @param bool $timestampAsDateTime true if timestamps must be parsed as DateTime objects rather than Unix timestamps (integers) * * @return array A PHP array representing the YAML string * * @throws ParseException */ - public static function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false, $references = array()) + public static function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false, $references = array(), $timestampAsDateTime = false) { self::$exceptionOnInvalidType = $exceptionOnInvalidType; self::$objectSupport = $objectSupport; self::$objectForMap = $objectForMap; + self::$timestampAsDateTime = $timestampAsDateTime; $value = trim($value); @@ -502,6 +505,9 @@ private static function evaluateScalar($scalar, $references = array()) case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar): return (float) str_replace(',', '', $scalar); case preg_match(self::getTimestampRegex(), $scalar): + if (self::$timestampAsDateTime) { + return new \DateTime($scalar); + } return strtotime($scalar); } default: diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index d72c7639c8337..e74ffbfd8e8f1 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -45,12 +45,13 @@ public function __construct($offset = 0) * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise * @param bool $objectSupport true if object support is enabled, false otherwise * @param bool $objectForMap true if maps should return a stdClass instead of array() + * @param bool $timestampAsDateTime true if timestamps must be parsed as DateTime objects rather than Unix timestamps (integers) * * @return mixed A PHP value * * @throws ParseException If the YAML is not valid */ - public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false) + public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false, $timestampAsDateTime = false) { if (!preg_match('//u', $value)) { throw new ParseException('The YAML value does not appear to be valid UTF-8.'); @@ -95,7 +96,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = $c = $this->getRealCurrentLineNb() + 1; $parser = new self($c); $parser->refs = &$this->refs; - $data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport, $objectForMap); + $data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport, $objectForMap, $timestampAsDateTime); } else { if (isset($values['leadspaces']) && preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P.+?))?\s*$#u', $values['value'], $matches) @@ -110,9 +111,9 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + strlen($values['leadspaces']) + 1); } - $data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport, $objectForMap); + $data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport, $objectForMap, $timestampAsDateTime); } else { - $data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap); + $data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $timestampAsDateTime); } } } elseif (preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P.+?))?\s*$#u', $this->currentLine, $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))) { @@ -122,7 +123,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = $context = 'mapping'; // force correct settings - Inline::parse(null, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs); + Inline::parse(null, $exceptionOnInvalidType, $objectSupport, $objectForMap, null, $timestampAsDateTime); try { $key = Inline::parseScalar($values['key']); } catch (ParseException $e) { @@ -161,7 +162,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = $c = $this->getRealCurrentLineNb() + 1; $parser = new self($c); $parser->refs = &$this->refs; - $parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap); + $parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $timestampAsDateTime); if (!is_array($parsed)) { throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine); @@ -212,7 +213,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = $c = $this->getRealCurrentLineNb() + 1; $parser = new self($c); $parser->refs = &$this->refs; - $value = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap); + $value = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap, $timestampAsDateTime); // Spec: Keys MUST be unique; first one wins. // But overwriting is allowed when a merge node is used in current block. if ($allowOverwrite || !isset($data[$key])) { @@ -220,7 +221,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = } } } else { - $value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap); + $value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $timestampAsDateTime); // Spec: Keys MUST be unique; first one wins. // But overwriting is allowed when a merge node is used in current block. if ($allowOverwrite || !isset($data[$key])) { @@ -236,7 +237,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = // 1-liner optionally followed by newline(s) if ($this->lines[0] === trim($value)) { try { - $value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs); + $value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs, $timestampAsDateTime); } catch (ParseException $e) { $e->setParsedLine($this->getRealCurrentLineNb() + 1); $e->setSnippet($this->currentLine); @@ -433,15 +434,16 @@ private function moveToPreviousLine() * Parses a YAML value. * * @param string $value A YAML value - * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise - * @param bool $objectSupport True if object support is enabled, false otherwise + * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types false otherwise + * @param bool $objectSupport true if object support is enabled, false otherwise * @param bool $objectForMap true if maps should return a stdClass instead of array() + * @param bool $timestampAsDateTime true if timestamps must be parsed as DateTime objects rather than Unix timestamps (integers) * * @return mixed A PHP value * * @throws ParseException When reference does not exist */ - private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap) + private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $timestampAsDateTime) { if (0 === strpos($value, '*')) { if (false !== $pos = strpos($value, '#')) { @@ -464,7 +466,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob } try { - return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs); + return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs, $timestampAsDateTime); } catch (ParseException $e) { $e->setParsedLine($this->getRealCurrentLineNb() + 1); $e->setSnippet($this->currentLine); diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index 320eb5072132b..e40e980b12525 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -33,6 +33,16 @@ public function testParseWithMapObjects($yaml, $value) $this->assertSame(serialize($value), serialize($actual)); } + /** + * @dataProvider getTestsForParseWithTimestampAsDateTime + */ + public function testParseTimestampAsDateTime($yamlTimestamp) + { + $expected = serialize(new \DateTime($yamlTimestamp)); + $actual = serialize(Inline::parse($yamlTimestamp, false, false, false, null, true)); + $this->assertSame($expected, $actual); + } + /** * @dataProvider getTestsForDump */ @@ -323,6 +333,17 @@ public function getTestsForParseWithMapObjects() ); } + public function getTestsForParseWithTimestampAsDateTime() + { + return array( + array('2007-10-30'), + array('2007-10-30T02:59:43Z'), + array('2007-10-30 02:59:43 Z'), + array('1960-10-30 02:59:43 Z'), + array('1730-10-30T02:59:43Z'), + ); + } + public function getTestsForDump() { return array( diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index e17698a07c74f..69a5b0299af01 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -42,6 +42,7 @@ class Yaml * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise * @param bool $objectSupport True if object support is enabled, false otherwise * @param bool $objectForMap True if maps should return a stdClass instead of array() + * @param bool $timestampAsDateTime True if timestamps must be parsed as DateTime objects rather than Unix timestamps (integers) * * @return array The YAML converted to a PHP array * @@ -51,7 +52,7 @@ class Yaml * * @api */ - public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false) + public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false, $timestampAsDateTime = false) { // if input is a file, process it $file = ''; @@ -69,7 +70,7 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup $yaml = new Parser(); try { - return $yaml->parse($input, $exceptionOnInvalidType, $objectSupport, $objectForMap); + return $yaml->parse($input, $exceptionOnInvalidType, $objectSupport, $objectForMap, $timestampAsDateTime); } catch (ParseException $e) { if ($file) { $e->setParsedFile($file); From 9db5c6ffc6878242e82eed5c9620cee8126092b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Mon, 20 Apr 2015 14:56:48 +0200 Subject: [PATCH 03/12] [Yaml] Add DateTime support to the Dumper --- src/Symfony/Component/Yaml/Dumper.php | 9 +++++---- src/Symfony/Component/Yaml/Inline.php | 16 +++++++++++----- .../Component/Yaml/Tests/InlineTest.php | 18 ++++++++++++++++-- src/Symfony/Component/Yaml/Yaml.php | 5 +++-- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 39cdcfc536a1b..953e0b898c41a 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -43,16 +43,17 @@ public function setIndentation($num) * @param int $indent The level of indentation (used internally) * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise * @param bool $objectSupport true if object support is enabled, false otherwise + * @param bool $dateTimeSupport true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported * * @return string The YAML representation of the PHP value */ - public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $objectSupport = false) + public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $objectSupport = false, $dateTimeSupport = false) { $output = ''; $prefix = $indent ? str_repeat(' ', $indent) : ''; if ($inline <= 0 || !is_array($input) || empty($input)) { - $output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $objectSupport); + $output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport); } else { $isAHash = array_keys($input) !== range(0, count($input) - 1); @@ -61,9 +62,9 @@ public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = $output .= sprintf('%s%s%s%s', $prefix, - $isAHash ? Inline::dump($key, $exceptionOnInvalidType, $objectSupport).':' : '-', + $isAHash ? Inline::dump($key, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport).':' : '-', $willBeInlined ? ' ' : "\n", - $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $objectSupport) + $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport) ).($willBeInlined ? "\n" : ''); } } diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 5ab08e1feb3e0..165f490fd878b 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -92,12 +92,13 @@ public static function parse($value, $exceptionOnInvalidType = false, $objectSup * @param mixed $value The PHP variable to convert * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise * @param bool $objectSupport true if object support is enabled, false otherwise + * @param bool $dateTimeSupport true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported * * @return string The YAML string representing the PHP array * * @throws DumpException When trying to dump PHP resource */ - public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false) + public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false, $dateTimeSupport = false) { switch (true) { case is_resource($value): @@ -107,6 +108,10 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp return 'null'; case is_object($value): + if ($value instanceof \DateTime) { + return $value->format(\DateTime::W3C); + } + if ($objectSupport) { return '!!php/object:'.serialize($value); } @@ -117,7 +122,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp return 'null'; case is_array($value): - return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport); + return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport); case null === $value: return 'null'; case true === $value: @@ -166,10 +171,11 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp * @param array $value The PHP array to dump * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise * @param bool $objectSupport true if object support is enabled, false otherwise + * @param bool $dateTimeSupport true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported * * @return string The YAML string representing the PHP array */ - private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport) + private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport) { // array $keys = array_keys($value); @@ -179,7 +185,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor ) { $output = array(); foreach ($value as $val) { - $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport); + $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport); } return sprintf('[%s]', implode(', ', $output)); @@ -188,7 +194,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor // mapping $output = array(); foreach ($value as $key => $val) { - $output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport)); + $output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport)); } return sprintf('{ %s }', implode(', ', $output)); diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index e40e980b12525..76e4b8833fc53 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -39,8 +39,8 @@ public function testParseWithMapObjects($yaml, $value) public function testParseTimestampAsDateTime($yamlTimestamp) { $expected = serialize(new \DateTime($yamlTimestamp)); - $actual = serialize(Inline::parse($yamlTimestamp, false, false, false, null, true)); - $this->assertSame($expected, $actual); + $actual = serialize(Inline::parse($yamlTimestamp, false, false, false, null, true)); + $this->assertSame($expected, $actual); } /** @@ -53,6 +53,14 @@ public function testDump($yaml, $value) $this->assertSame($value, Inline::parse(Inline::dump($value)), 'check consistency'); } + /** + * @dataProvider getTestsForDumpWithDateTimeSupport + */ + public function testDumpWithDateTimeSupport($yaml, $value) + { + $this->assertSame($yaml, Inline::dump($value, false, false, true)); + } + public function testDumpNumericValueWithLocale() { $locale = setlocale(LC_NUMERIC, 0); @@ -398,4 +406,10 @@ public function getTestsForDump() array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')), ); } + + public function getTestsForDumpWithDateTimeSupport () { + return array( + array('2015-04-21T05:30:30-08:00', new \DateTime('2015-04-21 05:30:30 -8')), + ); + } } diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index 69a5b0299af01..12bad23b363c3 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -91,16 +91,17 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup * @param int $indent The amount of spaces to use for indentation of nested nodes. * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise * @param bool $objectSupport true if object support is enabled, false otherwise + * @param bool $dateTimeSupport true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported * * @return string A YAML string representing the original PHP array * * @api */ - public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false) + public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false, $dateTimeSupport = false) { $yaml = new Dumper(); $yaml->setIndentation($indent); - return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport); + return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport); } } From 663a9ca92264c8dd3ab338fe69b8b4af0b19ec98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Mon, 20 Apr 2015 16:45:49 +0200 Subject: [PATCH 04/12] [Yaml] Dump human-readable timestamps --- src/Symfony/Component/Yaml/Inline.php | 8 +++++++- src/Symfony/Component/Yaml/Tests/InlineTest.php | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 165f490fd878b..e042ad3286aa3 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -109,9 +109,15 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp return 'null'; case is_object($value): if ($value instanceof \DateTime) { + if ($value->getTimezone()->getName() === date_default_timezone_get()) { + if (0 === (int) $value->format('His')) { + return $value->format('Y-m-d'); + } + return $value->format('Y-m-d H:i:s'); + } return $value->format(\DateTime::W3C); } - + if ($objectSupport) { return '!!php/object:'.serialize($value); } diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index 76e4b8833fc53..21d8059192539 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -410,6 +410,10 @@ public function getTestsForDump() public function getTestsForDumpWithDateTimeSupport () { return array( array('2015-04-21T05:30:30-08:00', new \DateTime('2015-04-21 05:30:30 -8')), + array('2015-04-21T00:00:00-02:00', new \DateTime('2015-04-21 -2')), + array('2015-04-21T00:00:00+00:00', new \DateTime('2015-04-21 -0')), + array('2015-04-21 05:30:30', new \DateTime('2015-04-21 05:30:30')), + array('2015-04-21', new \DateTime('2015-04-21')), ); } } From 41c5f12c7403bc53bdc108f8d95113bb165e62e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Mon, 20 Apr 2015 18:30:12 +0200 Subject: [PATCH 05/12] [Yaml] Depreciate to not use DateTime --- UPGRADE-2.7.md | 12 ++++++++++-- src/Symfony/Component/Yaml/Yaml.php | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/UPGRADE-2.7.md b/UPGRADE-2.7.md index f491c7d471ba2..4efc902f0f190 100644 --- a/UPGRADE-2.7.md +++ b/UPGRADE-2.7.md @@ -525,6 +525,14 @@ PropertyAccess Config ------ - * The `__toString()` method of the `\Symfony\Component\Config\ConfigCache` is marked as + * The `__toString()` method of the `\Symfony\Component\Config\ConfigCache` is marked as deprecated in favor of the new `getPath()` method. - + +Yaml +----- + + * The ability to pass $timestampAsDateTime = false to the Yaml::parse method is + deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead. + * The ability to pass $dateTimeSupport = false to the Yaml::dump method is deprecated + since version 2.7. The argument will be removed in 3.0. Pass true instead. + diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index 12bad23b363c3..04a301cd423c8 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -48,16 +48,21 @@ class Yaml * * @throws ParseException If the YAML is not valid * - * @deprecated The ability to pass file names to the Yaml::parse method is deprecated since version 2.2 and will be removed in 3.0. Pass the YAML contents of the file instead. + * @deprecated The ability to pass file names to the Yaml::parse method is deprecated since version 2.7 and will be removed in 3.0. Pass the YAML contents of the file instead. + * @deprecated The ability to pass $timestampAsDateTime = false to the Yaml::parse method is deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead. * * @api */ public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false, $timestampAsDateTime = false) { + if ($timestampAsDateTime) { + trigger_error('The ability to pass $timestampAsDateTime = false to the '.__METHOD__.' method is deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); + } + // if input is a file, process it $file = ''; if (strpos($input, "\n") === false && is_file($input)) { - trigger_error('The ability to pass file names to the '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Pass the YAML contents of the file instead.', E_USER_DEPRECATED); + trigger_error('The ability to pass file names to the '.__METHOD__.' method is deprecated since version 2.7 and will be removed in 3.0. Pass the YAML contents of the file instead.', E_USER_DEPRECATED); if (false === is_readable($input)) { throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input)); @@ -95,10 +100,16 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup * * @return string A YAML string representing the original PHP array * + * @deprecated The ability to pass $dateTimeSupport = false to the Yaml::dump method is deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead. + * * @api */ public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false, $dateTimeSupport = false) { + if ($dateTimeSupport) { + trigger_error('The ability to pass $dateTimeSupport = false to the '.__METHOD__.' method is deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); + } + $yaml = new Dumper(); $yaml->setIndentation($indent); From dd4798e40e39f0425f39a7d4d8bb799fc7cb644f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Tue, 21 Apr 2015 10:18:04 +0200 Subject: [PATCH 06/12] Worship fabbot.io --- src/Symfony/Component/Yaml/Inline.php | 4 +++- src/Symfony/Component/Yaml/Tests/InlineTest.php | 2 +- src/Symfony/Component/Yaml/Yaml.php | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index e042ad3286aa3..432f3a2ebf7a8 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -113,11 +113,13 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp if (0 === (int) $value->format('His')) { return $value->format('Y-m-d'); } + return $value->format('Y-m-d H:i:s'); } + return $value->format(\DateTime::W3C); } - + if ($objectSupport) { return '!!php/object:'.serialize($value); } diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index 21d8059192539..b900807edaa41 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -308,7 +308,7 @@ public function getTestsForParseWithMapObjects() array('{foo: \'bar\', bar: \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')), array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')), array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', (object) array('foo\'' => 'bar', "bar\"" => 'foo: bar')), - array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', (object) array('foo: ' => 'bar', "bar: " => 'foo: bar')), + array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', (object) array('foo: ' => 'bar', 'bar: ' => 'foo: bar')), // nested sequences and mappings array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))), diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index 04a301cd423c8..d330c1f8828f0 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -58,7 +58,7 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup if ($timestampAsDateTime) { trigger_error('The ability to pass $timestampAsDateTime = false to the '.__METHOD__.' method is deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); } - + // if input is a file, process it $file = ''; if (strpos($input, "\n") === false && is_file($input)) { @@ -109,7 +109,7 @@ public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvali if ($dateTimeSupport) { trigger_error('The ability to pass $dateTimeSupport = false to the '.__METHOD__.' method is deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); } - + $yaml = new Dumper(); $yaml->setIndentation($indent); From 1f4269e8c9a653711554cd1ba8eff2bf2bd56fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Sat, 2 May 2015 12:14:21 +0200 Subject: [PATCH 07/12] [Yaml] Accept DateTimeImmutable instances --- src/Symfony/Component/Yaml/Inline.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 432f3a2ebf7a8..7b5229e5a4132 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -108,7 +108,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp return 'null'; case is_object($value): - if ($value instanceof \DateTime) { + if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) { if ($value->getTimezone()->getName() === date_default_timezone_get()) { if (0 === (int) $value->format('His')) { return $value->format('Y-m-d'); From 028f2d72a19f8f9d8a397d19804c11b9b075414a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Sat, 2 May 2015 12:17:37 +0200 Subject: [PATCH 08/12] [Yaml] Delete a cast --- src/Symfony/Component/Yaml/Inline.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 7b5229e5a4132..b30e594bc6c3e 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -110,7 +110,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp case is_object($value): if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) { if ($value->getTimezone()->getName() === date_default_timezone_get()) { - if (0 === (int) $value->format('His')) { + if ('000000' === $value->format('His')) { return $value->format('Y-m-d'); } From 5d75dd0abfcc7a76384ca741e67af58a089d3c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Sat, 2 May 2015 21:41:12 +0200 Subject: [PATCH 09/12] [Yaml] Prepare merging in 2.8 rather than 2.7 --- UPGRADE-2.7.md | 12 ++---------- UPGRADE-2.8.md | 10 ++++++++++ src/Symfony/Component/Yaml/CHANGELOG.md | 8 ++++++++ src/Symfony/Component/Yaml/Inline.php | 1 + src/Symfony/Component/Yaml/Yaml.php | 8 ++++---- 5 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 UPGRADE-2.8.md diff --git a/UPGRADE-2.7.md b/UPGRADE-2.7.md index 4efc902f0f190..f491c7d471ba2 100644 --- a/UPGRADE-2.7.md +++ b/UPGRADE-2.7.md @@ -525,14 +525,6 @@ PropertyAccess Config ------ - * The `__toString()` method of the `\Symfony\Component\Config\ConfigCache` is marked as + * The `__toString()` method of the `\Symfony\Component\Config\ConfigCache` is marked as deprecated in favor of the new `getPath()` method. - -Yaml ------ - - * The ability to pass $timestampAsDateTime = false to the Yaml::parse method is - deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead. - * The ability to pass $dateTimeSupport = false to the Yaml::dump method is deprecated - since version 2.7. The argument will be removed in 3.0. Pass true instead. - + diff --git a/UPGRADE-2.8.md b/UPGRADE-2.8.md new file mode 100644 index 0000000000000..d048c9f62dc34 --- /dev/null +++ b/UPGRADE-2.8.md @@ -0,0 +1,10 @@ +UPGRADE FROM 2.7 to 2.8 +======================= + +Yaml +----- + + * The ability to pass $timestampAsDateTime = false to the Yaml::parse method is + deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead. + * The ability to pass $dateTimeSupport = false to the Yaml::dump method is deprecated + since version 2.8. The argument will be removed in 3.0. Pass true instead. diff --git a/src/Symfony/Component/Yaml/CHANGELOG.md b/src/Symfony/Component/Yaml/CHANGELOG.md index 096cf654d8058..fdc810cd1a810 100644 --- a/src/Symfony/Component/Yaml/CHANGELOG.md +++ b/src/Symfony/Component/Yaml/CHANGELOG.md @@ -6,3 +6,11 @@ CHANGELOG * Yaml::parse() does not evaluate loaded files as PHP files by default anymore (call Yaml::enablePhpParsing() to get back the old behavior) + +2.8 +--- + + * The ability to pass $timestampAsDateTime = false to the Yaml::parse method is + deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead. + * The ability to pass $dateTimeSupport = false to the Yaml::dump method is deprecated + since version 2.8. The argument will be removed in 3.0. Pass true instead. diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index b30e594bc6c3e..bdc6b2d0d2e14 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -522,6 +522,7 @@ private static function evaluateScalar($scalar, $references = array()) if (self::$timestampAsDateTime) { return new \DateTime($scalar); } + return strtotime($scalar); } default: diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index d330c1f8828f0..14402c5b9116a 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -49,14 +49,14 @@ class Yaml * @throws ParseException If the YAML is not valid * * @deprecated The ability to pass file names to the Yaml::parse method is deprecated since version 2.7 and will be removed in 3.0. Pass the YAML contents of the file instead. - * @deprecated The ability to pass $timestampAsDateTime = false to the Yaml::parse method is deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead. + * @deprecated The ability to pass $timestampAsDateTime = false to the Yaml::parse method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead. * * @api */ public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false, $timestampAsDateTime = false) { if ($timestampAsDateTime) { - trigger_error('The ability to pass $timestampAsDateTime = false to the '.__METHOD__.' method is deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); + trigger_error('The ability to pass $timestampAsDateTime = false to the '.__METHOD__.' method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); } // if input is a file, process it @@ -100,14 +100,14 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup * * @return string A YAML string representing the original PHP array * - * @deprecated The ability to pass $dateTimeSupport = false to the Yaml::dump method is deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead. + * @deprecated The ability to pass $dateTimeSupport = false to the Yaml::dump method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead. * * @api */ public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false, $dateTimeSupport = false) { if ($dateTimeSupport) { - trigger_error('The ability to pass $dateTimeSupport = false to the '.__METHOD__.' method is deprecated since version 2.7. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); + trigger_error('The ability to pass $dateTimeSupport = false to the '.__METHOD__.' method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); } $yaml = new Dumper(); From 72abdeeba05ae5fd2853d725a97bdd445f2c042a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Sun, 3 May 2015 11:04:20 +0200 Subject: [PATCH 10/12] [Yaml] Rename $dateTimeSupport argument as $timestampAsDateTime --- src/Symfony/Component/Yaml/CHANGELOG.md | 3 ++- src/Symfony/Component/Yaml/Dumper.php | 10 +++++----- src/Symfony/Component/Yaml/Inline.php | 14 +++++++------- src/Symfony/Component/Yaml/Yaml.php | 12 ++++++------ 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Component/Yaml/CHANGELOG.md b/src/Symfony/Component/Yaml/CHANGELOG.md index fdc810cd1a810..37e4dda18fa5f 100644 --- a/src/Symfony/Component/Yaml/CHANGELOG.md +++ b/src/Symfony/Component/Yaml/CHANGELOG.md @@ -10,7 +10,8 @@ CHANGELOG 2.8 --- + * Added a $timestampAsDateTime argument to the Yaml::parse() and Yaml::dump() methods. * The ability to pass $timestampAsDateTime = false to the Yaml::parse method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead. - * The ability to pass $dateTimeSupport = false to the Yaml::dump method is deprecated + * The ability to pass $timestampAsDateTime = false to the Yaml::dump method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead. diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 953e0b898c41a..4178f4dedf3c1 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -43,17 +43,17 @@ public function setIndentation($num) * @param int $indent The level of indentation (used internally) * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise * @param bool $objectSupport true if object support is enabled, false otherwise - * @param bool $dateTimeSupport true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported + * @param bool $timestampAsDateTime true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported * * @return string The YAML representation of the PHP value */ - public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $objectSupport = false, $dateTimeSupport = false) + public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $objectSupport = false, $timestampAsDateTime = false) { $output = ''; $prefix = $indent ? str_repeat(' ', $indent) : ''; if ($inline <= 0 || !is_array($input) || empty($input)) { - $output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport); + $output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime); } else { $isAHash = array_keys($input) !== range(0, count($input) - 1); @@ -62,9 +62,9 @@ public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = $output .= sprintf('%s%s%s%s', $prefix, - $isAHash ? Inline::dump($key, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport).':' : '-', + $isAHash ? Inline::dump($key, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime).':' : '-', $willBeInlined ? ' ' : "\n", - $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport) + $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime) ).($willBeInlined ? "\n" : ''); } } diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index bdc6b2d0d2e14..b060e985abf7b 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -92,13 +92,13 @@ public static function parse($value, $exceptionOnInvalidType = false, $objectSup * @param mixed $value The PHP variable to convert * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise * @param bool $objectSupport true if object support is enabled, false otherwise - * @param bool $dateTimeSupport true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported + * @param bool $timestampAsDateTime true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported * * @return string The YAML string representing the PHP array * * @throws DumpException When trying to dump PHP resource */ - public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false, $dateTimeSupport = false) + public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false, $timestampAsDateTime = false) { switch (true) { case is_resource($value): @@ -130,7 +130,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp return 'null'; case is_array($value): - return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport); + return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime); case null === $value: return 'null'; case true === $value: @@ -179,11 +179,11 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp * @param array $value The PHP array to dump * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise * @param bool $objectSupport true if object support is enabled, false otherwise - * @param bool $dateTimeSupport true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported + * @param bool $timestampAsDateTime true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported * * @return string The YAML string representing the PHP array */ - private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport) + private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime) { // array $keys = array_keys($value); @@ -193,7 +193,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor ) { $output = array(); foreach ($value as $val) { - $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport); + $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime); } return sprintf('[%s]', implode(', ', $output)); @@ -202,7 +202,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor // mapping $output = array(); foreach ($value as $key => $val) { - $output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport)); + $output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime), self::dump($val, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime)); } return sprintf('{ %s }', implode(', ', $output)); diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index 14402c5b9116a..edb5d21302a97 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -96,23 +96,23 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup * @param int $indent The amount of spaces to use for indentation of nested nodes. * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise * @param bool $objectSupport true if object support is enabled, false otherwise - * @param bool $dateTimeSupport true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported + * @param bool $timestampAsDateTime true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported * * @return string A YAML string representing the original PHP array * - * @deprecated The ability to pass $dateTimeSupport = false to the Yaml::dump method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead. + * @deprecated The ability to pass $timestampAsDateTime = false to the Yaml::dump method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead. * * @api */ - public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false, $dateTimeSupport = false) + public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false, $timestampAsDateTime = false) { - if ($dateTimeSupport) { - trigger_error('The ability to pass $dateTimeSupport = false to the '.__METHOD__.' method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); + if ($timestampAsDateTime) { + trigger_error('The ability to pass $timestampAsDateTime = false to the '.__METHOD__.' method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); } $yaml = new Dumper(); $yaml->setIndentation($indent); - return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport, $dateTimeSupport); + return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime); } } From 1b970f3bdefae75b9a09a7ebe03ce1b627464fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Wed, 6 May 2015 14:08:51 +0200 Subject: [PATCH 11/12] [Yaml] Fix deprecation --- src/Symfony/Component/Yaml/Yaml.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index edb5d21302a97..a9a6c6beee387 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -55,7 +55,7 @@ class Yaml */ public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false, $timestampAsDateTime = false) { - if ($timestampAsDateTime) { + if (!$timestampAsDateTime) { trigger_error('The ability to pass $timestampAsDateTime = false to the '.__METHOD__.' method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); } @@ -106,7 +106,7 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup */ public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false, $timestampAsDateTime = false) { - if ($timestampAsDateTime) { + if (!$timestampAsDateTime) { trigger_error('The ability to pass $timestampAsDateTime = false to the '.__METHOD__.' method is deprecated since version 2.8. The argument will be removed in 3.0. Pass true instead.', E_USER_DEPRECATED); } From 422d8380da82b0434193c555d87f1c35c0978848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Blaise?= Date: Wed, 6 May 2015 15:18:15 +0200 Subject: [PATCH 12/12] [Yaml] Fix usage of $timestampAsDateTime --- src/Symfony/Component/Yaml/Inline.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index b060e985abf7b..c265a612bf3b2 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -100,6 +100,8 @@ public static function parse($value, $exceptionOnInvalidType = false, $objectSup */ public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false, $timestampAsDateTime = false) { + self::$timestampAsDateTime = $timestampAsDateTime; + switch (true) { case is_resource($value): if ($exceptionOnInvalidType) { @@ -108,7 +110,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp return 'null'; case is_object($value): - if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) { + if (self::$timestampAsDateTime && ($value instanceof \DateTime || $value instanceof \DateTimeImmutable)) { if ($value->getTimezone()->getName() === date_default_timezone_get()) { if ('000000' === $value->format('His')) { return $value->format('Y-m-d'); @@ -130,7 +132,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp return 'null'; case is_array($value): - return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime); + return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport); case null === $value: return 'null'; case true === $value: @@ -179,11 +181,10 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp * @param array $value The PHP array to dump * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise * @param bool $objectSupport true if object support is enabled, false otherwise - * @param bool $timestampAsDateTime true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported * * @return string The YAML string representing the PHP array */ - private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime) + private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport) { // array $keys = array_keys($value); @@ -193,7 +194,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor ) { $output = array(); foreach ($value as $val) { - $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime); + $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport); } return sprintf('[%s]', implode(', ', $output)); @@ -202,7 +203,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor // mapping $output = array(); foreach ($value as $key => $val) { - $output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime), self::dump($val, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime)); + $output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport)); } return sprintf('{ %s }', implode(', ', $output));