Commit 7970cb38 authored by Bas de Nooijer's avatar Bas de Nooijer

Added support for DateTime objects as field value in an update document

parent e320f506
...@@ -80,7 +80,7 @@ class RequestBuilder extends BaseRequestBuilder ...@@ -80,7 +80,7 @@ class RequestBuilder extends BaseRequestBuilder
foreach ($query->getCommands() as $command) { foreach ($query->getCommands() as $command) {
switch ($command->getType()) { switch ($command->getType()) {
case UpdateQuery::COMMAND_ADD: case UpdateQuery::COMMAND_ADD:
$xml .= $this->buildAddXml($command); $xml .= $this->buildAddXml($command, $query);
break; break;
case UpdateQuery::COMMAND_DELETE: case UpdateQuery::COMMAND_DELETE:
$xml .= $this->buildDeleteXml($command); $xml .= $this->buildDeleteXml($command);
...@@ -108,9 +108,10 @@ class RequestBuilder extends BaseRequestBuilder ...@@ -108,9 +108,10 @@ class RequestBuilder extends BaseRequestBuilder
* Build XML for an add command * Build XML for an add command
* *
* @param Query\Command\Add $command * @param Query\Command\Add $command
* @param UpdateQuery $query
* @return string * @return string
*/ */
public function buildAddXml($command) public function buildAddXml($command, $query = null)
{ {
$xml = '<add'; $xml = '<add';
$xml .= $this->boolAttrib('overwrite', $command->getOverwrite()); $xml .= $this->boolAttrib('overwrite', $command->getOverwrite());
...@@ -127,10 +128,10 @@ class RequestBuilder extends BaseRequestBuilder ...@@ -127,10 +128,10 @@ class RequestBuilder extends BaseRequestBuilder
$modifier = $doc->getFieldModifier($name); $modifier = $doc->getFieldModifier($name);
if (is_array($value)) { if (is_array($value)) {
foreach ($value as $multival) { foreach ($value as $multival) {
$xml .= $this->buildFieldXml($name, $boost, $multival, $modifier); $xml .= $this->buildFieldXml($name, $boost, $multival, $modifier, $query);
} }
} else { } else {
$xml .= $this->buildFieldXml($name, $boost, $value, $modifier); $xml .= $this->buildFieldXml($name, $boost, $value, $modifier, $query);
} }
} }
...@@ -156,10 +157,15 @@ class RequestBuilder extends BaseRequestBuilder ...@@ -156,10 +157,15 @@ class RequestBuilder extends BaseRequestBuilder
* @param float $boost * @param float $boost
* @param mixed $value * @param mixed $value
* @param string $modifier * @param string $modifier
* @param UpdateQuery $query
* @return string * @return string
*/ */
protected function buildFieldXml($name, $boost, $value, $modifier = null) protected function buildFieldXml($name, $boost, $value, $modifier = null, $query = null)
{ {
if ($value instanceof \DateTime) {
$value = $query->getHelper()->formatDate($value);
}
$xml = '<field name="' . $name . '"'; $xml = '<field name="' . $name . '"';
$xml .= $this->attrib('boost', $boost); $xml .= $this->attrib('boost', $boost);
$xml .= $this->attrib('update', $modifier); $xml .= $this->attrib('update', $modifier);
......
...@@ -208,6 +208,17 @@ class RequestBuilderTest extends \PHPUnit_Framework_TestCase ...@@ -208,6 +208,17 @@ class RequestBuilderTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testBuildAddXmlWithDateTime()
{
$command = new AddCommand;
$command->addDocument(new Document(array('id' => 1, 'datetime' => new \DateTime('2013-01-15 14:41:58'))));
$this->assertEquals(
'<add><doc><field name="id">1</field><field name="datetime">2013-01-15T14:41:58Z</field></doc></add>',
$this->builder->buildAddXml($command, $this->query)
);
}
public function testBuildDeleteXml() public function testBuildDeleteXml()
{ {
$command = new DeleteCommand; $command = new DeleteCommand;
......
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