8000 added support for \DateTimeInterface default · symfony/symfony@e31f24b · GitHub
[go: up one dir, main page]

Skip to content

Commit e31f24b

Browse files
committed
added support for \DateTimeInterface default
1 parent e7b221b commit e31f24b

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,21 @@ public function getBoolean($key, $default = false)
190190
/**
191191
* Returns the parameter value converted to a DateTime object.
192192
*
193-
* @param string $key The parameter key
194-
* @param string $format The expected date format
195-
* @param string $default The default value to be converted to a DateTime object if the parameter key does not exist
196-
* @param \DateTimeZone|null $timeZone A DateTimeZone object representing the desired time zone
193+
* @param string $key The parameter key
194+
* @param string $format The expected date format
195+
* @param \DateTimeInterface|string $default The default value to be converted to a DateTime object if the parameter key does not exist
196+
* @param \DateTimeZone|null $timeZone A DateTimeZone object representing the desired time zone
197197
*
198-
* @return \DateTime|null
198+
* @return \DateTimeInterface|null
199199
*/
200200
public function getDate($key, $format, $default = null, \DateTimeZone $timeZone = null)
201201
{
202202
$time = $this->get($key, $default);
203203

204+
if ($time instanceof \DateTimeInterface) {
205+
return $default;
206+
}
207+
204208
// if the user has specified a timezone then pass that
205209
// otherwise do not even attempt to put a value but rather let the runtime decide
206210
// the default value by itself

src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,14 @@ public function testGetDate()
138138
$this->assertEquals(0, $diff->days, '->getDate() returns a date via the format specified');
139139
$this->assertNull($bag->getDate('d1', 'd/m/Y'), '->getDate() returns null if the format is not valid');
140140
$this->assertNull($bag->getDate('d2', 'd/m/Y'), '->getDate() returns null if the parameter is not found');
141+
$this->assertEquals($date, $bag->getDate('d1', 'Y-m-d', new \DateTime('2016-12-01')), '->getDate() parses the value if the key is present');
141142

142143
$date = $bag->getDate('iso', \DateTime::ISO8601);
143144
$this->assertEquals(new \DateTime($isoDate), $date);
144145
$this->assertEquals('UTC', $date->getTimezone()->getName());
145146

146147
$this->assertEquals($date, $bag->getDate('nokey', \DateTime::ISO8601, $isoDate));
148+
$this->assertInstanceOf(\DateTimeInterface::class, $bag->getDate('nokey', \DateTime::ISO8601, $date));
147149
$this->assertNull($bag->getDate('nokey', 'd/m/Y', $isoDate), '->getDate() returns null when the default value is not in the specified format');
148150

149151
$tz = $bag->getDate('d1', 'Y-m-d', null, new \DateTimeZone('Europe/Tirane'))->getTimezone()->getName();

0 commit comments

Comments
 (0)
0