8000 removed getDateTime method; $default param is now a string wich gets… · symfony/symfony@ba3af51 · GitHub
[go: up one dir, main page]

Skip to content

Commit ba3af51

Browse files
committed
removed getDateTime method; $default param is now a string wich gets converted to a datetime; added more tests
1 parent 319b955 commit ba3af51

File tree

2 files changed

+12
-36
lines changed

2 files changed

+12
-36
lines changed

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -192,25 +192,21 @@ public function getBoolean($key, $default = false)
192192
*
193193
* @param string $key The parameter key
194194
* @param string $format The expected date format
195-
* @param \DateTime|null $default The default value if the parameter key does not exist
196-
* @param \DateTimeZone|null $timeZone
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
197197
*
198198
* @return \DateTime|null
199199
*/
200-
public function getDate($key, $format = 'Y-m-d', $default = null, $timeZone = null)
200+
public function getDate($key, $format, $default = null, $timeZone = null)
201201
{
202-
if (!$this->has($key)) {
203-
return $default;
204-
}
205-
206-
$time = $this->get($key);
202+
$time = $this->get($key, (string) $default);
207203

208204
// if the user has specified a timezone then pass that
209205
// otherwise do not even attempt to put a value but rather let the runtime decide
210206
// the default value by itself
211207
// this is in order to ensure compatibility with all php versions since
212208
// some accept null as a TimeZone parameter and others do not
213-
if ($timeZone !== null) {
209+
if (null !== $timeZone) {
214210
$result = \DateTime::createFromFormat($format, $time, $timeZone);
215211
} else {
216212
$result = \DateTime::createFromFormat($format, $time);
@@ -220,21 +216,6 @@ public function getDate($key, $format = 'Y-m-d', $default = null, $timeZone = nu
220216
return false === $result ? null : $result;
221217
}
222218

223-
/**
224-
* Returns the parameter value converted to a DateTime object while also parsing the time.
225-
*
226-
* @param string $key The parameter key
227-
* @param string $format The expected date format
228-
* @param \DateTime|null $default The default value if the parameter key does not exist
229-
* @param \DateTimeZone|null $timeZone
230-
*
231-
* @return \DateTime|null
232-
*/
233-
public function getDateTime($key, $format = 'Y-m-d H:i:s', $default = null, \DateTimeZone $timeZone = null)
234-
{
235-
return $this->getDate($key, $format, $default, $timeZone);
236-
}
237-
238219
/**
239220
* Filter key.
240221
*

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,6 @@ public function testGetInt()
124124
$this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
125125
}
126126

127-
public function testGetDateTime()
128-
{
129-
$format = 'Y-m-d H:i:s';
130-
$bag = new ParameterBag(array('d1' => '2016-01-01 00:00:00'));
131-
$date = \DateTime::createFromFormat($format, '2016-01-01 00:00:00');
132-
133-
$this->assertEquals($date, $bag->getDateTime('d1', $format), '->getDateTime() returns a date from the specified format');
134-
}
135-
136127
public function testGetDate()
137128
{
138129
$isoDate = '2016-07-05T15:30:00UTC';
@@ -142,17 +133,21 @@ public function testGetDate()
142133
));
143134

144135
$date = \DateTime::createFromFormat('Y-m-d', '2016-01-01');
145-
$diff = $date->diff($bag->getDate('d1'));
136+
$diff = $date->diff($bag->getDate('d1', 'Y-m-d'));
146137

147138
$this->assertEquals(0, $diff->days, '->getDate() returns a date via the format specified');
148-
$this->assertNull($bag->getDate('d1', 'd/m/Y'), '->getDate() returns false if the format is not valid');
139+
$this->assertNull($bag->getDate('d1', 'd/m/Y'), '->getDate() returns null if the format is not valid');
149140
$this->assertNull($bag->getDate('d2', 'd/m/Y'), '->getDate() returns null if the parameter is not found');
150141

151142
$date = $bag->getDate('iso', \DateTime::ISO8601);
152143
$this->assertEquals(new \DateTime($isoDate), $date);
153144
$this->assertEquals('UTC', $date->getTimezone()->getName());
154145

155-
$this->assertEquals($date, $bag->getDate('nokey', 'Y-m-d', $date));
146+
$this->assertEquals($date, $bag->getDate('nokey', \DateTime::ISO8601, $isoDate));
147+
$this->assertNull($bag->getDate('nokey', 'd/m/Y', $isoDate), '->getDate() returns null when the default value is not in the specified format');
148+
149+
$tz = $bag->getDate('d1', 'Y-m-d', null, new \DateTimeZone('Europe/Tirane'))->getTimezone()->getName();
150+
$this->assertEquals('Europe/Tirane', $tz, '->getDate() accepts a DateTimeZone object which specifies the preferred timezone');
156151
}
157152

158153
public function testFilter()

0 commit comments

Comments
 (0)
0