Commit 30dae1fe authored by Baldur Rensch's avatar Baldur Rensch

Filter by default in documents

Filter control characters in update documents by default
This fixes #220
parent 3da1cf40
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
*/ */
namespace Solarium\QueryType\Update\Query\Document; namespace Solarium\QueryType\Update\Query\Document;
use Solarium\Core\Query\Helper;
use Solarium\QueryType\Select\Result\AbstractDocument; use Solarium\QueryType\Select\Result\AbstractDocument;
use Solarium\Exception\RuntimeException; use Solarium\Exception\RuntimeException;
...@@ -139,6 +140,15 @@ class Document extends AbstractDocument implements DocumentInterface ...@@ -139,6 +140,15 @@ class Document extends AbstractDocument implements DocumentInterface
*/ */
protected $version; protected $version;
/**
* Helper instance
*
* @var Helper
*/
protected $helper;
protected $filterControlCharacters = true;
/** /**
* Constructor * Constructor
* *
...@@ -175,6 +185,10 @@ class Document extends AbstractDocument implements DocumentInterface ...@@ -175,6 +185,10 @@ class Document extends AbstractDocument implements DocumentInterface
$this->fields[$key] = array($this->fields[$key]); $this->fields[$key] = array($this->fields[$key]);
} }
if ($this->filterControlCharacters && is_string($value)) {
$value = $this->getHelper()->filterControlCharacters($value);
}
$this->fields[$key][] = $value; $this->fields[$key][] = $value;
$this->setFieldBoost($key, $boost); $this->setFieldBoost($key, $boost);
if ($modifier !== null) { if ($modifier !== null) {
...@@ -203,6 +217,10 @@ class Document extends AbstractDocument implements DocumentInterface ...@@ -203,6 +217,10 @@ class Document extends AbstractDocument implements DocumentInterface
if ($value === null && $modifier == null) { if ($value === null && $modifier == null) {
$this->removeField($key); $this->removeField($key);
} else { } else {
if ($this->filterControlCharacters && is_string($value)) {
$value = $this->getHelper()->filterControlCharacters($value);
}
$this->fields[$key] = $value; $this->fields[$key] = $value;
$this->setFieldBoost($key, $boost); $this->setFieldBoost($key, $boost);
if ($modifier !== null) { if ($modifier !== null) {
...@@ -427,4 +445,40 @@ class Document extends AbstractDocument implements DocumentInterface ...@@ -427,4 +445,40 @@ class Document extends AbstractDocument implements DocumentInterface
{ {
return $this->version; return $this->version;
} }
/**
* Get a helper instance
*
* Uses lazy loading: the helper is instantiated on first use
*
* @return Helper
*/
public function getHelper()
{
if (null === $this->helper) {
$this->helper = new Helper($this);
}
return $this->helper;
}
/**
* Whether values should be filtered for control characters automatically
*
* @param boolean $filterControlCharacters
*/
public function setFilterControlCharacters($filterControlCharacters)
{
$this->filterControlCharacters = $filterControlCharacters;
}
/**
* Returns whether values should be filtered automatically or control characters
*
* @return boolean
*/
public function getFilterControlCharacters()
{
return $this->filterControlCharacters;
}
} }
...@@ -458,4 +458,36 @@ class DocumentTest extends \PHPUnit_Framework_TestCase ...@@ -458,4 +458,36 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
$this->doc->getVersion() $this->doc->getVersion()
); );
} }
public function testEscapeByDefaultSetField()
{
$this->doc->setField('foo', 'bar' . chr(15));
$this->assertEquals('bar ', $this->doc->foo);
}
public function testEscapeByDefaultAddField()
{
$this->doc->setField('foo', 'bar' . chr(15));
$this->doc->addField('foo', 'bar' . chr(15) . chr(8));
$this->assertEquals(array('bar ', 'bar '), $this->doc->foo);
}
public function testNoEscapeSetField()
{
$this->doc->setFilterControlCharacters(false);
$this->doc->setField('foo', $value = 'bar' . chr(15));
$this->assertEquals($value, $this->doc->foo);
}
public function testNoEscapeAddField()
{
$this->doc->setFilterControlCharacters(false);
$this->doc->setField('foo', $value1 = 'bar' . chr(15));
$this->doc->addField('foo', $value2 = 'bar' . chr(15) . chr(8));
$this->assertEquals(array($value1, $value2), $this->doc->foo);
}
} }
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