Require PropertyAccess component
This commit is contained in:
parent
55920a3397
commit
83e27ede93
|
@ -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;
|
||||
|
|
|
@ -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)];
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
<parameter key="fos_elastica.manager.class">FOS\ElasticaBundle\Manager\RepositoryManager</parameter>
|
||||
<parameter key="fos_elastica.elastica_to_model_transformer.collection.class">FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection</parameter>
|
||||
<parameter key="fos_elastica.provider_registry.class">FOS\ElasticaBundle\Provider\ProviderRegistry</parameter>
|
||||
<parameter key="fos_elastica.property_accessor.class">Symfony\Component\PropertyAccess\PropertyAccessor</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
|
@ -58,7 +59,7 @@
|
|||
<service id="fos_elastica.model_to_elastica_transformer.prototype.auto" class="FOS\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer" public="false" abstract="true">
|
||||
<argument /> <!-- options -->
|
||||
<call method="setPropertyAccessor">
|
||||
<argument type="service" id="property_accessor" on-invalid="null" />
|
||||
<argument type="service" id="fos_elastica.property_accessor" />
|
||||
</call>
|
||||
</service>
|
||||
|
||||
|
@ -75,6 +76,9 @@
|
|||
<service id="fos_elastica.paginator.subscriber" class="FOS\ElasticaBundle\Subscriber\PaginateElasticaQuerySubscriber">
|
||||
<tag name="knp_paginator.subscriber" />
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.property_accessor" class="%fos_elastica.property_accessor.class%">
|
||||
</service>
|
||||
</services>
|
||||
|
||||
</container>
|
||||
|
|
|
@ -24,6 +24,9 @@
|
|||
<argument type="service" id="doctrine_mongodb" />
|
||||
<argument /> <!-- model -->
|
||||
<argument type="collection" /> <!-- options -->
|
||||
<call method="setPropertyAccessor">
|
||||
<argument type="service" id="fos_elastica.property_accessor" />
|
||||
</call>
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.manager.mongodb" class="FOS\ElasticaBundle\Doctrine\RepositoryManager">
|
||||
|
|
|
@ -25,6 +25,9 @@
|
|||
<argument type="service" id="doctrine" />
|
||||
<argument /> <!-- model -->
|
||||
<argument type="collection" /> <!-- options -->
|
||||
<call method="setPropertyAccessor">
|
||||
<argument type="service" id="fos_elastica.property_accessor" />
|
||||
</call>
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.manager.orm" class="FOS\ElasticaBundle\Doctrine\RepositoryManager">
|
||||
|
|
|
@ -14,6 +14,9 @@
|
|||
<service id="fos_elastica.elastica_to_model_transformer.prototype.propel" class="FOS\ElasticaBundle\Propel\ElasticaToModelTransformer" public="false">
|
||||
<argument /> <!-- model -->
|
||||
<argument type="collection" /> <!-- options -->
|
||||
<call method="setPropertyAccessor">
|
||||
<argument type="service" id="fos_elastica.property_accessor" />
|
||||
</call>
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.manager.propel" class="%fos_elastica.manager.class%">
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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.*",
|
||||
|
|
Loading…
Reference in a new issue