Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
solarium
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
solarium
Commits
d7a64a59
Commit
d7a64a59
authored
Jan 18, 2012
by
Bas de Nooijer
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' of github.com:basdenooijer/solarium into develop
parents
977c0a70
b7a7a5b5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
132 additions
and
1 deletion
+132
-1
library/Solarium/Query/Helper.php
library/Solarium/Query/Helper.php
+56
-1
tests/Solarium/Query/HelperTest.php
tests/Solarium/Query/HelperTest.php
+76
-0
No files found.
library/Solarium/Query/Helper.php
View file @
d7a64a59
...
...
@@ -123,6 +123,61 @@ class Solarium_Query_Helper
return
'"'
.
preg_replace
(
'/("|\\\)/'
,
'\\\$1'
,
$input
)
.
'"'
;
}
/**
* 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
)
:
$dateTime
=
new
DateTime
(
$input
);
break
;
case
is_string
(
$input
)
&&
$this
->
_isTimestamp
(
strtotime
(
$input
))
:
$dateTime
=
new
DateTime
(
strtotime
(
$input
));
break
;
case
$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
;
}
/**
* 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
*
...
...
@@ -346,4 +401,4 @@ class Solarium_Query_Helper
return
$this
->
qparser
(
'join'
,
array
(
'from'
=>
$from
,
'to'
=>
$to
),
$dereferenced
);
}
}
\ No newline at end of file
}
tests/Solarium/Query/HelperTest.php
View file @
d7a64a59
...
...
@@ -185,6 +185,82 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase
);
}
public
function
testFormatDateInputTimestamp
()
{
$this
->
assertFalse
(
$this
->
_helper
->
formatDate
(
strtotime
(
'2011---'
)),
'Expects invalid strtotime/timestamp input (false) not to be accpted'
);
//allow negative dates.
$this
->
assertNotEquals
(
false
,
$this
->
_helper
->
formatDate
(
strtotime
(
strtotime
(
'2011-10-01'
))),
'Expects negative timestamp input to be accpted'
);
//@todo find out if we need to any test for php versions / platforms which do not support negative timestamp
$this
->
assertFalse
(
$this
->
_helper
->
formatDate
(
strtotime
(
'2010-31-02'
)),
'Expects invalid timestamp input (not in calendar) not to be accpted'
);
$this
->
assertEquals
(
$this
->
_mockFormatDateOutput
(
strtotime
(
'2011-10-01'
)),
$this
->
_helper
->
formatDate
(
strtotime
(
'2011-10-01'
)),
'Expects formatDate with Timstamp input to output ISO8601 with stripped timezone'
);
}
public
function
testFormatDateInputString
()
{
$this
->
assertFalse
(
$this
->
_helper
->
formatDate
(
date
(
'Y-m-d'
,
strtotime
(
'2011-11-31'
))),
'Expects an invalid date string input not to be accepted'
);
$this
->
assertEquals
(
$this
->
_mockFormatDateOutput
(
strtotime
(
'2011-10-01'
)),
$this
->
_helper
->
formatDate
(
date
(
'Y-m-d'
,
strtotime
(
'2011-10-01'
))),
'Expects formatDate with String input to output ISO8601 with stripped timezone'
);
}
public
function
testFormatDateInputDateTime
()
{
$this
->
assertFalse
(
$this
->
_helper
->
formatDate
(
new
stdClass
()),
'Expect any other object not to be accepted'
);
$this
->
assertEquals
(
$this
->
_mockFormatDateOutput
(
strtotime
(
'2011-10-01'
)),
$this
->
_helper
->
formatDate
(
new
DateTime
(
strtotime
(
'2011-10-01'
))),
'Expects formatDate with DateTime input to output ISO8601 with stripped timezone'
);
}
public
function
testFormatDate
()
{
//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'
);
$this
->
assertEquals
(
$this
->
_mockFormatDateOutput
(
time
()),
$this
->
_helper
->
formatDate
(
time
())
);
}
protected
function
_mockFormatDateOutput
(
$timestamp
)
{
$date
=
new
DateTime
(
$timestamp
);
return
strstr
(
$date
->
format
(
DateTime
::
ISO8601
),
'+'
,
true
)
.
'Z'
;
}
public
function
testAssemble
()
{
// test single basic placeholder
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment