Commit e6aa97c7 authored by stefanooldeman's avatar stefanooldeman

fix issue #58 add formatDate to query helper

parent 578f25df
......@@ -123,6 +123,36 @@ class Solarium_Query_Helper
return '"' . preg_replace('/("|\\\)/', '\\\$1', $input) . '"';
}
public function formatDate($input)
{
$isTimestamp = function($input) {
return checkdate(date('m', $input), date('d', $input), date('Y', $input));
};
switch(true) {
case is_numeric($input) && $isTimestamp($input):
$dateTime = new DateTime($input);
break;
case is_string($input) && $isTimestamp(strtotime($input)):
$dateTime = new DateTime(strtotime($input));
break;
case !is_string($input) && !is_numeric($input) && $input instanceof DateTime:
$dateTime = $input;
break;
//if input does not match any of these requirements then fail
default:
return false;
}
$iso8601 = $dateTime->format(DateTime::ISO8601);
$iso8601 = strstr($iso8601, '+', true); //strip timezone
$iso8601 .= 'Z';
return $iso8601;
}
/**
* Render a range query
*
......@@ -346,4 +376,4 @@ class Solarium_Query_Helper
return $this->qparser('join', array('from' => $from, 'to' => $to), $dereferenced);
}
}
\ No newline at end of file
}
......@@ -185,6 +185,35 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase
);
}
public function testFormatDate()
{
//accepts a time
$this->assertNotEquals(
false,
$this->_helper->formatDate(strtotime('2011-10-01')),
'Expects timestamp input to be accpted'
);
//accepts a date
$this->assertNotEquals(
false,
$this->_helper->formatDate(date('Y-m-d', strtotime('2011-10-01'))),
'Expects date string inputs to be accepted'
);
//accepts a DateTime object
$this->assertNotEquals(
false,
$this->_helper->formatDate(new DateTime(strtotime('2011-10-01'))),
'Expects DateTime object to be accepted'
);
//check if timezone is stripped
$expected = strtoupper('Z');
$actual = substr($this->_helper->formatDate(time()), 19, 20);
$this->assertEquals($expected, $actual, 'Expects last charachter to be uppercased Z');
}
public function testAssemble()
{
// test single basic placeholder
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment