Changed to only filter truly null value in Transformer
This commit is contained in:
parent
5e19a37344
commit
a3f4c95e9c
|
@ -6,12 +6,14 @@ use FOQ\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer;
|
|||
|
||||
class POPO
|
||||
{
|
||||
public $id = 123;
|
||||
public $name = 'someName';
|
||||
public $desc = 'desc';
|
||||
public $float = 7.2;
|
||||
public $bool = true;
|
||||
public $id = 123;
|
||||
public $name = 'someName';
|
||||
public $desc = 'desc';
|
||||
public $float = 7.2;
|
||||
public $bool = true;
|
||||
public $falseBool = false;
|
||||
public $date;
|
||||
public $nullValue;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
@ -54,6 +56,11 @@ class POPO
|
|||
return $this->bool;
|
||||
}
|
||||
|
||||
public function getFalseBool()
|
||||
{
|
||||
return $this->falseBool;
|
||||
}
|
||||
|
||||
public function getFloat()
|
||||
{
|
||||
return $this->float;
|
||||
|
@ -64,6 +71,11 @@ class POPO
|
|||
return $this->date;
|
||||
}
|
||||
|
||||
public function getNullValue()
|
||||
{
|
||||
return $this->nullValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
||||
|
@ -89,7 +101,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
|||
public function testThatCanTransformObjectWithCorrectTypes()
|
||||
{
|
||||
$transformer = new ModelToElasticaAutoTransformer();
|
||||
$document = $transformer->transform(new POPO(), array('name', 'float', 'bool', 'date'));
|
||||
$document = $transformer->transform(new POPO(), array('name', 'float', 'bool', 'date', 'falseBool'));
|
||||
$data = $document->getData();
|
||||
|
||||
$this->assertInstanceOf('Elastica_Document', $document);
|
||||
|
@ -97,6 +109,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertEquals('someName', $data['name']);
|
||||
$this->assertEquals(7.2, $data['float']);
|
||||
$this->assertEquals(true, $data['bool']);
|
||||
$this->assertEquals(false, $data['falseBool']);
|
||||
$expectedDate = new \DateTime('1979-05-05');
|
||||
$this->assertEquals($expectedDate->format('U'), $data['date']);
|
||||
}
|
||||
|
@ -135,6 +148,17 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
|||
);
|
||||
}
|
||||
|
||||
public function testThatNullValuesAreFilteredOut()
|
||||
{
|
||||
$transformer = new ModelToElasticaAutoTransformer();
|
||||
$document = $transformer->transform(new POPO(), array('nullValue'));
|
||||
$data = $document->getData();
|
||||
|
||||
$this->assertInstanceOf('Elastica_Document', $document);
|
||||
$this->assertEquals(123, $document->getId());
|
||||
$this->assertFalse(array_key_exists('nullValue', $data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException RuntimeException
|
||||
*/
|
||||
|
|
|
@ -54,7 +54,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
|
|||
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
|
||||
$identifier = $object->$identifierGetter();
|
||||
|
||||
return new Elastica_Document($identifier, array_filter($array));
|
||||
return new Elastica_Document($identifier, $this->filterOutStrictlyNullValues($array));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,4 +83,12 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
|
|||
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function filterOutStrictlyNullValues(array $unfiltered)
|
||||
{
|
||||
return array_filter($unfiltered, function($value) {
|
||||
return $value !== null;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue