Merge branch '2.0'

This commit is contained in:
Richard Miller 2012-02-24 09:51:51 +00:00
commit 86e5afc469
2 changed files with 36 additions and 9 deletions

View file

@ -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
*/

View file

@ -49,12 +49,14 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
if (!method_exists($class, $getter)) {
throw new RuntimeException(sprintf('The getter %s::%s does not exist', $class, $getter));
}
$array[$key] = $this->normalizeValue($object->$getter());
if (null !== $value = $this->normalizeValue($object->$getter())) {
$array[$key] = $value;
}
}
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
$identifier = $object->$identifierGetter();
return new Elastica_Document($identifier, array_filter($array));
return new Elastica_Document($identifier, $array);
}
/**
@ -69,7 +71,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
$normalizeValue = function(&$v) {
if ($v instanceof \DateTime) {
$v = (int) $v->format('U');
} elseif (!is_int($v) && !is_float($v) && !is_bool($v) && !is_null($v)) {
} elseif (!is_scalar($v) && !is_null($v)) {
$v = (string) $v;
}
};
@ -83,4 +85,5 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
return $value;
}
}