Commit c525d860 authored by stefanooldeman's avatar stefanooldeman

add docs to methods and simplify condition on DateTime

parent 187564e6
......@@ -123,29 +123,30 @@ class Solarium_Query_Helper
return '"' . preg_replace('/("|\\\)/', '\\\$1', $input) . '"';
}
protected function isTimestamp($timestamp)
{
try {
new DateTime($timestamp);
} catch (Exception $e) {
return false;
}
return true;
}
/**
* Format a date to the expected formatting used in SOLR
*
* This format was derived to be standards compliant (ISO 8601)
* A date field shall be of the form 1995-12-31T23:59:59Z The trailing "Z" designates UTC time and is mandatory
*
* @see http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html
*
* @param mixed $input accepted formats: timestamp, date string or DateTime
* @return string or false when input is invalid
*/
public function formatDate($input)
{
switch(true) {
case is_numeric($input) && $this->isTimestamp($input):
case is_numeric($input) && $this->_isTimestamp($input):
$dateTime = new DateTime($input);
break;
case is_string($input) && $this->isTimestamp(strtotime($input)):
case is_string($input) && $this->_isTimestamp(strtotime($input)):
$dateTime = new DateTime(strtotime($input));
break;
case !is_string($input) && !is_numeric($input) && $input instanceof DateTime:
case $input instanceof DateTime:
$dateTime = $input;
break;
......@@ -160,6 +161,23 @@ class Solarium_Query_Helper
return $iso8601;
}
/**
* Validate if date is valid
* note: do not use checkdate() and support negative timestamps
*
* @return boolean
*/
protected function _isTimestamp($timestamp)
{
try {
new DateTime($timestamp);
} catch (Exception $e) {
return false;
}
return true;
}
/**
* Render a range query
*
......
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