FOSElasticaBundle/Transformer/ModelToElasticaAutoTransformer.php

152 lines
4.7 KiB
PHP
Raw Normal View History

<?php
namespace FOS\ElasticaBundle\Transformer;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Elastica\Document;
/**
* Maps Elastica documents with Doctrine objects
* This mapper assumes an exact match between
2015-03-12 11:20:00 +01:00
* elastica documents ids and doctrine object ids.
*/
class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterface
{
/**
2015-03-12 11:20:00 +01:00
* Optional parameters.
*
* @var array
*/
protected $options = array(
2015-03-12 11:20:00 +01:00
'identifier' => 'id',
);
/**
2015-03-12 11:20:00 +01:00
* PropertyAccessor instance.
*
* @var PropertyAccessorInterface
*/
2013-04-04 15:44:42 +02:00
protected $propertyAccessor;
/**
2015-03-12 11:20:00 +01:00
* Instanciates a new Mapper.
*
* @param array $options
*/
public function __construct(array $options = array())
{
2012-04-19 22:26:19 +02:00
$this->options = array_merge($this->options, $options);
}
/**
2015-03-12 11:20:00 +01:00
* Set the PropertyAccessor.
*
* @param PropertyAccessorInterface $propertyAccessor
*/
2013-04-04 15:44:42 +02:00
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{
$this->propertyAccessor = $propertyAccessor;
}
/**
2015-03-12 11:20:00 +01:00
* Transforms an object into an elastica object having the required keys.
*
* @param object $object the object to convert
* @param array $fields the keys we want to have in the returned array
*
* @return Document
**/
public function transform($object, array $fields)
{
2013-04-04 15:44:42 +02:00
$identifier = $this->propertyAccessor->getValue($object, $this->options['identifier']);
$document = new Document($identifier);
2012-04-19 22:26:19 +02:00
foreach ($fields as $key => $mapping) {
2013-06-17 21:19:11 +02:00
if ($key == '_parent') {
2015-03-12 11:20:00 +01:00
$property = (null !== $mapping['property']) ? $mapping['property'] : $mapping['type'];
2013-06-19 13:57:15 +02:00
$value = $this->propertyAccessor->getValue($object, $property);
2013-06-17 21:19:11 +02:00
$document->setParent($this->propertyAccessor->getValue($value, $mapping['identifier']));
continue;
}
2013-06-17 21:19:11 +02:00
$value = $this->propertyAccessor->getValue($object, $key);
if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object')) && isset($mapping['properties']) && !empty($mapping['properties'])) {
/* $value is a nested document or object. Transform $value into
* an array of documents, respective the mapped properties.
*/
$document->set($key, $this->transformNested($value, $mapping['properties']));
continue;
}
if (isset($mapping['type']) && $mapping['type'] == 'attachment') {
// $value is an attachment. Add it to the document.
if ($value instanceof \SplFileInfo) {
$document->addFile($key, $value->getPathName());
2012-04-19 22:26:19 +02:00
} else {
$document->addFileContent($key, $value);
2012-04-19 22:26:19 +02:00
}
continue;
}
$document->set($key, $this->normalizeValue($value));
}
2012-04-19 22:26:19 +02:00
return $document;
}
/**
2015-03-12 11:20:00 +01:00
* transform a nested document or an object property into an array of ElasticaDocument.
*
* @param array|\Traversable|\ArrayAccess $objects the object to convert
2015-03-12 11:20:00 +01:00
* @param array $fields the keys we want to have in the returned array
*
* @return array
*/
protected function transformNested($objects, array $fields)
{
if (is_array($objects) || $objects instanceof \Traversable || $objects instanceof \ArrayAccess) {
$documents = array();
foreach ($objects as $object) {
$document = $this->transform($object, $fields);
$documents[] = $document->getData();
}
return $documents;
} elseif (null !== $objects) {
$document = $this->transform($objects, $fields);
return $document->getData();
}
return array();
}
/**
2015-03-12 11:20:00 +01:00
* Attempts to convert any type to a string or an array of strings.
*
* @param mixed $value
2011-11-28 17:24:16 +01:00
*
* @return string|array
*/
protected function normalizeValue($value)
{
2015-03-12 11:20:00 +01:00
$normalizeValue = function (&$v) {
2011-11-28 17:24:16 +01:00
if ($v instanceof \DateTime) {
$v = $v->format('c');
} elseif (!is_scalar($v) && !is_null($v)) {
2015-03-12 11:20:00 +01:00
$v = (string) $v;
}
};
2012-04-19 22:26:19 +02:00
if (is_array($value) || $value instanceof \Traversable || $value instanceof \ArrayAccess) {
$value = is_array($value) ? $value : iterator_to_array($value, false);
2011-11-28 17:24:16 +01:00
array_walk_recursive($value, $normalizeValue);
} else {
2011-11-28 17:24:16 +01:00
$normalizeValue($value);
}
return $value;
}
}