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