diff --git a/Doctrine/AbstractElasticaToModelTransformer.php b/Doctrine/AbstractElasticaToModelTransformer.php index 0b339fb..f796eec 100755 --- a/Doctrine/AbstractElasticaToModelTransformer.php +++ b/Doctrine/AbstractElasticaToModelTransformer.php @@ -5,7 +5,7 @@ namespace FOS\ElasticaBundle\Doctrine; use FOS\ElasticaBundle\HybridResult; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use FOS\ElasticaBundle\Transformer\HighlightableModelInterface; -use Symfony\Component\Form\Util\PropertyPath; +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; /** * Maps Elastica documents with Doctrine objects @@ -33,9 +33,16 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran */ protected $options = array( 'hydrate' => true, - 'identifier' => 'id' + 'identifier' => 'id' ); + /** + * PropertyAccessor instance + * + * @var PropertyAccessorInterface + */ + protected $propertyAccessor; + /** * Instantiates a new Mapper * @@ -60,6 +67,16 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran return $this->objectClass; } + /** + * Set the PropertyAccessor + * + * @param PropertyAccessorInterface $propertyAccessor + */ + public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor) + { + $this->propertyAccessor = $propertyAccessor; + } + /** * Transforms an array of elastica objects into an array of * model objects fetched from the doctrine repository @@ -87,13 +104,13 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran } } - $identifierProperty = new PropertyPath($this->options['identifier']); - // sort objects in the order of ids $idPos = array_flip($ids); - usort($objects, function($a, $b) use ($idPos, $identifierProperty) + $identifier = $this->options['identifier']; + $propertyAccessor = $this->propertyAccessor; + usort($objects, function($a, $b) use ($idPos, $identifier, $propertyAccessor) { - return $idPos[$identifierProperty->getValue($a)] > $idPos[$identifierProperty->getValue($b)]; + return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)]; }); return $objects; diff --git a/Propel/ElasticaToModelTransformer.php b/Propel/ElasticaToModelTransformer.php index 76e4233..2678cd6 100644 --- a/Propel/ElasticaToModelTransformer.php +++ b/Propel/ElasticaToModelTransformer.php @@ -4,7 +4,7 @@ namespace FOS\ElasticaBundle\Propel; use FOS\ElasticaBundle\HybridResult; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; -use Symfony\Component\Form\Util\PropertyPath; +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; /** * Maps Elastica documents with Propel objects @@ -32,6 +32,13 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface 'identifier' => 'id' ); + /** + * PropertyAccessor instance + * + * @var PropertyAccessorInterface + */ + protected $propertyAccessor; + /** * Instantiates a new Mapper * @@ -44,6 +51,16 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface $this->options = array_merge($this->options, $options); } + /** + * Set the PropertyAccessor + * + * @param PropertyAccessorInterface $propertyAccessor + */ + public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor) + { + $this->propertyAccessor = $propertyAccessor; + } + /** * Transforms an array of elastica objects into an array of * model objects fetched from the propel repository @@ -59,17 +76,17 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface $objects = $this->findByIdentifiers($ids, $this->options['hydrate']); - $identifierProperty = new PropertyPath($this->options['identifier']); - // sort objects in the order of ids $idPos = array_flip($ids); + $identifier = $this->options['identifier']; + $propertyAccessor = $this->propertyAccessor; if (is_object($objects)) { - $objects->uasort(function($a, $b) use ($idPos, $identifierProperty) { - return $idPos[$identifierProperty->getValue($a)] > $idPos[$identifierProperty->getValue($b)]; + $objects->uasort(function($a, $b) use ($idPos, $identifier, $propertyAccessor) { + return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)]; }); } else { - usort($objects, function($a, $b) use ($idPos, $identifierProperty) { - return $idPos[$identifierProperty->getValue($a)] > $idPos[$identifierProperty->getValue($b)]; + usort($objects, function($a, $b) use ($idPos, $identifier, $propertyAccessor) { + return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)]; }); } diff --git a/Resources/config/config.xml b/Resources/config/config.xml index a0819fb..bf9d40d 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -13,6 +13,7 @@ FOS\ElasticaBundle\Manager\RepositoryManager FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection FOS\ElasticaBundle\Provider\ProviderRegistry + Symfony\Component\PropertyAccess\PropertyAccessor @@ -58,7 +59,7 @@ - + @@ -75,6 +76,9 @@ + + + diff --git a/Resources/config/mongodb.xml b/Resources/config/mongodb.xml index 53c82f9..e60e3dc 100644 --- a/Resources/config/mongodb.xml +++ b/Resources/config/mongodb.xml @@ -24,6 +24,9 @@ + + + diff --git a/Resources/config/orm.xml b/Resources/config/orm.xml index b47cfc1..4fd6ae7 100644 --- a/Resources/config/orm.xml +++ b/Resources/config/orm.xml @@ -25,6 +25,9 @@ + + + diff --git a/Resources/config/propel.xml b/Resources/config/propel.xml index a6fc32f..7a7d93e 100644 --- a/Resources/config/propel.xml +++ b/Resources/config/propel.xml @@ -14,6 +14,9 @@ + + + diff --git a/Tests/Persister/ObjectPersisterTest.php b/Tests/Persister/ObjectPersisterTest.php index 0a46553..e672e12 100644 --- a/Tests/Persister/ObjectPersisterTest.php +++ b/Tests/Persister/ObjectPersisterTest.php @@ -213,10 +213,7 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase private function getTransformer() { $transformer = new ModelToElasticaAutoTransformer(); - - if (class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) { - $transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor()); - } + $transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor()); return $transformer; } diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php index 798ea38..5ac13e6 100644 --- a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php +++ b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php @@ -203,12 +203,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase public function testThatCannotTransformObjectWhenGetterDoesNotExistForPrivateMethod() { - // Support both Symfony 2.1 (Form component) and 2.2 (PropertyAccess component) - $expectedException = class_exists('Symfony\Component\PropertyAccess\PropertyAccess') - ? 'Symfony\Component\PropertyAccess\Exception\PropertyAccessDeniedException' - : 'Symfony\Component\Form\Exception\PropertyAccessDeniedException'; - - $this->setExpectedException($expectedException); + $this->setExpectedException('Symfony\Component\PropertyAccess\Exception\PropertyAccessDeniedException'); $transformer = $this->getTransformer(); $transformer->transform(new POPO(), array('desc' => array())); @@ -302,10 +297,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase private function getTransformer() { $transformer = new ModelToElasticaAutoTransformer(); - - if (class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) { - $transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor()); - } + $transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor()); return $transformer; } diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index 4565199..ad20529 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -2,7 +2,6 @@ namespace FOS\ElasticaBundle\Transformer; -use Symfony\Component\Form\Util\PropertyPath; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; /** @@ -22,11 +21,11 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf ); /** - * PropertyAccessor instance (will be used if available) + * PropertyAccessor instance * * @var PropertyAccessorInterface */ - private $propertyAccessor; + protected $propertyAccessor; /** * Instanciates a new Mapper @@ -43,7 +42,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf * * @param PropertyAccessorInterface $propertyAccessor */ - public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor = null) + public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor) { $this->propertyAccessor = $propertyAccessor; } @@ -58,17 +57,17 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf **/ public function transform($object, array $fields) { - $identifier = $this->getPropertyValue($object, $this->options['identifier']); + $identifier = $this->propertyAccessor->getValue($object, $this->options['identifier']); $document = new \Elastica_Document($identifier); foreach ($fields as $key => $mapping) { - $value = $this->getPropertyValue($object, $key); + $value = $this->propertyAccessor->getValue($object, $key); if (isset($mapping['_parent']['identifier'])) { /* $value is the parent. Read its identifier and set that as the * document's parent. */ - $document->setParent($this->getPropertyValue($value, $mapping['_parent']['identifier'])); + $document->setParent($this->propertyAccessor->getValue($value, $mapping['_parent']['identifier'])); continue; } @@ -96,26 +95,6 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf return $document; } - /** - * Get the value of an object property. - * - * This method will use Symfony 2.2's PropertyAccessor if it is available. - * - * @param object $object - * @param string $property - * @return mixed - */ - protected function getPropertyValue($object, $property) - { - if (isset($this->propertyAccessor)) { - return $this->propertyAccessor->getValue($object, $property); - } - - $propertyPath = new PropertyPath($property); - - return $propertyPath->getValue($object); - } - /** * transform a nested document or an object property into an array of ElasticaDocument * diff --git a/composer.json b/composer.json index a4969a0..3629fda 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "symfony/framework-bundle": ">=2.1.0,<2.3.0-dev", "symfony/console": ">=2.1.0,<2.3.0-dev", "symfony/form": ">=2.1.0,<2.3.0-dev", + "symfony/property-access": "2.2.*", "ruflin/elastica": "0.19.8" }, "require-dev":{ @@ -25,7 +26,6 @@ "knplabs/knp-components": "1.2.*" }, "suggest": { - "symfony/property-access": "2.2.*", "doctrine/orm": ">=2.2,<2.5-dev", "doctrine/mongodb-odm": "1.0.*@dev", "propel/propel1": "1.6.*",