10000 added getDate/getDateTime methods to the parameterbag class · symfony/symfony@319b955 · GitHub
[go: up one dir, main page]

Skip to content

Commit 319b955

Browse files
committed
added getDate/getDateTime methods to the parameterbag class
1 parent 5213778 commit 319b955

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 48 additions & 0 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,54 @@ public function getBoolean($key, $default = false)
187187
return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN);
188188
}
189189

190+
/**
191+
* Returns the parameter value converted to a DateTime object.
192+
*
193+
* @param string $key The parameter key
194+
* @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
197+
*
198+
* @return \DateTime|null
199+
*/
200+
public function getDate($key, $format = 'Y-m-d', $default = null, $timeZone = null)
201+
{
202+
if (!$this->has($key)) {
203+
return $default;
204+
}
205+
206+
$time = $this->get($key);
207+
208+
// if the user has specified a timezone then pass that
209+
// otherwise do not even attempt to put a value but rather let the runtime decide
210+
// the default value by itself
211+
// this is in order to ensure compatibility with all php versions since
212+
// some accept null as a TimeZone parameter and others do not
213+
if ($timeZone !== null) {
214+
$result = \DateTime::createFromFormat($format, $time, $timeZone);
215+
} else {
216+
$result = \DateTime::createFromFormat($format, $time);
217+
}
218+
219+
// Failure to parse the date according to the specified format will return null
220+
return false === $result ? null : $result;
221+
}
222+
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+
190238
/**
191239
* Filter key.
192240
*

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,37 @@ 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+
136+
public function testGetDate()
137+
{
138+
$isoDate = '2016-07-05T15:30:00UTC';
139+
$bag = new ParameterBag(array(
140+
'd1' => '2016-01-01',
141+
'iso' => $isoDate,
142+
));
143+
144+
$date = \DateTime::createFromFormat('Y-m-d', '2016-01-01');
145+
$diff = $date->diff($bag->getDate('d1'));
146+
147+
$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');
149+
$this->assertNull($bag->getDate('d2', 'd/m/Y'), '->getDate() returns null if the parameter is not found');
150+
151+
$date = $bag->getDate('iso', \DateTime::ISO8601);
152+
$this->assertEquals(new \DateTime($isoDate), $date);
153+
$this->assertEquals('UTC', $date->getTimezone()->getName());
154+
155+
$this->assertEquals($date, $bag->getDate('nokey', 'Y-m-d', $date));
156+
}
157+
127158
public function testFilter()
128159
{
129160
$bag = new ParameterBag(array(
@@ -133,7 +164,7 @@ public function testFilter()
133164
'dec' => '256',
134165
'hex' => '0x100',
135166
'array' => array('bang'),
136-
));
167+
));
137168

138169
$this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
139170

0 commit comments

Comments
 (0)
0