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
e6aa97c7
Commit
e6aa97c7
authored
Jan 12, 2012
by
stefanooldeman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix issue #58 add formatDate to query helper
parent
578f25df
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
1 deletion
+60
-1
library/Solarium/Query/Helper.php
library/Solarium/Query/Helper.php
+31
-1
tests/Solarium/Query/HelperTest.php
tests/Solarium/Query/HelperTest.php
+29
-0
No files found.
library/Solarium/Query/Helper.php
View file @
e6aa97c7
...
...
@@ -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
}
tests/Solarium/Query/HelperTest.php
View file @
e6aa97c7
...
...
@@ -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
...
...
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