Added transforming mapped attachments
This commit is contained in:
parent
38bdc44185
commit
146cf891cd
4 changed files with 66 additions and 32 deletions
|
|
@ -173,7 +173,7 @@ class FOQElasticaExtension extends Extension
|
|||
if (isset($type['mappings'])) {
|
||||
$this->indexConfigs[$indexName]['config']['mappings'][$name]['properties'] = $type['mappings'];
|
||||
$typeName = sprintf('%s/%s', $indexName, $name);
|
||||
$this->typeFields[$typeName] = array_keys($type['mappings']);
|
||||
$this->typeFields[$typeName] = $type['mappings'];
|
||||
}
|
||||
if (isset($type['persistence'])) {
|
||||
$this->loadTypePersistenceIntegration($type['persistence'], $container, $typeDef, $indexName, $name);
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase
|
|||
$typeMock->expects($this->once())
|
||||
->method('addDocument');
|
||||
|
||||
$fields = array('name');
|
||||
$fields = array('name' => array());
|
||||
|
||||
$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
|
||||
$objectPersister->replaceOne(new POPO());
|
||||
|
|
@ -71,7 +71,7 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase
|
|||
$typeMock->expects($this->never())
|
||||
->method('addDocument');
|
||||
|
||||
$fields = array('name');
|
||||
$fields = array('name' => array());
|
||||
|
||||
$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
|
||||
$objectPersister->replaceOne(new POPO());
|
||||
|
|
@ -89,7 +89,7 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase
|
|||
$typeMock->expects($this->once())
|
||||
->method('addDocument');
|
||||
|
||||
$fields = array('name');
|
||||
$fields = array('name' => array());
|
||||
|
||||
$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
|
||||
$objectPersister->insertOne(new POPO());
|
||||
|
|
@ -110,7 +110,7 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase
|
|||
$typeMock->expects($this->never())
|
||||
->method('addDocument');
|
||||
|
||||
$fields = array('name');
|
||||
$fields = array('name' => array());
|
||||
|
||||
$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
|
||||
$objectPersister->insertOne(new POPO());
|
||||
|
|
@ -128,7 +128,7 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase
|
|||
$typeMock->expects($this->never())
|
||||
->method('addDocument');
|
||||
|
||||
$fields = array('name');
|
||||
$fields = array('name' => array());
|
||||
|
||||
$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
|
||||
$objectPersister->deleteOne(new POPO());
|
||||
|
|
@ -149,7 +149,7 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase
|
|||
$typeMock->expects($this->never())
|
||||
->method('addDocument');
|
||||
|
||||
$fields = array('name');
|
||||
$fields = array('name' => array());
|
||||
|
||||
$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
|
||||
$objectPersister->deleteOne(new POPO());
|
||||
|
|
@ -169,7 +169,7 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase
|
|||
$typeMock->expects($this->once())
|
||||
->method('addDocuments');
|
||||
|
||||
$fields = array('name');
|
||||
$fields = array('name' => array());
|
||||
|
||||
$objectPersister = new ObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
|
||||
$objectPersister->insertMany(array(new POPO(), new POPO()));
|
||||
|
|
@ -192,7 +192,7 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase
|
|||
$typeMock->expects($this->never())
|
||||
->method('addDocuments');
|
||||
|
||||
$fields = array('name');
|
||||
$fields = array('name' => array());
|
||||
|
||||
$objectPersister = new InvalidObjectPersister($typeMock, $modelTransformer, 'SomeClass', $fields);
|
||||
$objectPersister->insertMany(array(new POPO(), new POPO()));
|
||||
|
|
|
|||
|
|
@ -14,10 +14,14 @@ class POPO
|
|||
public $falseBool = false;
|
||||
public $date;
|
||||
public $nullValue;
|
||||
public $file;
|
||||
public $fileContents;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->date = new \DateTime('1979-05-05');
|
||||
$this->date = new \DateTime('1979-05-05');
|
||||
$this->file = new \SplFileInfo(__DIR__ . '/../fixtures/attachment.odt');
|
||||
$this->fileContents = file_get_contents(__DIR__ . '/../fixtures/attachment.odt');
|
||||
}
|
||||
|
||||
public function getId()
|
||||
|
|
@ -76,6 +80,15 @@ class POPO
|
|||
return $this->nullValue;
|
||||
}
|
||||
|
||||
public function getFile()
|
||||
{
|
||||
return $this->file;
|
||||
}
|
||||
|
||||
public function getFileContents()
|
||||
{
|
||||
return $this->file;
|
||||
}
|
||||
}
|
||||
|
||||
class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
||||
|
|
@ -90,7 +103,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
|||
public function testThatCanTransformObject()
|
||||
{
|
||||
$transformer = new ModelToElasticaAutoTransformer();
|
||||
$document = $transformer->transform(new POPO(), array('name'));
|
||||
$document = $transformer->transform(new POPO(), array('name' => array()));
|
||||
$data = $document->getData();
|
||||
|
||||
$this->assertInstanceOf('Elastica_Document', $document);
|
||||
|
|
@ -101,7 +114,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
|||
public function testThatCanTransformObjectWithCorrectTypes()
|
||||
{
|
||||
$transformer = new ModelToElasticaAutoTransformer();
|
||||
$document = $transformer->transform(new POPO(), array('name', 'float', 'bool', 'date', 'falseBool'));
|
||||
$document = $transformer->transform(new POPO(), array('name' => array(), 'float' => array(), 'bool' => array(), 'date' => array(), 'falseBool' => array()));
|
||||
$data = $document->getData();
|
||||
|
||||
$this->assertInstanceOf('Elastica_Document', $document);
|
||||
|
|
@ -117,7 +130,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
|||
public function testThatCanTransformObjectWithIteratorValue()
|
||||
{
|
||||
$transformer = new ModelToElasticaAutoTransformer();
|
||||
$document = $transformer->transform(new POPO(), array('iterator'));
|
||||
$document = $transformer->transform(new POPO(), array('iterator' => array()));
|
||||
$data = $document->getData();
|
||||
|
||||
$this->assertEquals(array('value1'), $data['iterator']);
|
||||
|
|
@ -126,7 +139,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
|||
public function testThatCanTransformObjectWithArrayValue()
|
||||
{
|
||||
$transformer = new ModelToElasticaAutoTransformer();
|
||||
$document = $transformer->transform(new POPO(), array('array'));
|
||||
$document = $transformer->transform(new POPO(), array('array' => array()));
|
||||
$data = $document->getData();
|
||||
|
||||
$this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $data['array']);
|
||||
|
|
@ -135,7 +148,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
|||
public function testThatCanTransformObjectWithMultiDimensionalArrayValue()
|
||||
{
|
||||
$transformer = new ModelToElasticaAutoTransformer();
|
||||
$document = $transformer->transform(new POPO(), array('multiArray'));
|
||||
$document = $transformer->transform(new POPO(), array('multiArray' => array()));
|
||||
$data = $document->getData();
|
||||
|
||||
$expectedDate = new \DateTime('1978-09-07');
|
||||
|
|
@ -151,7 +164,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
|||
public function testThatNullValuesAreNotFilteredOut()
|
||||
{
|
||||
$transformer = new ModelToElasticaAutoTransformer();
|
||||
$document = $transformer->transform(new POPO(), array('nullValue'));
|
||||
$document = $transformer->transform(new POPO(), array('nullValue' => array()));
|
||||
$data = $document->getData();
|
||||
|
||||
$this->assertTrue(array_key_exists('nullValue', $data));
|
||||
|
|
@ -163,6 +176,24 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
|||
public function testThatCannotTransformObjectWhenGetterDoesNotExists()
|
||||
{
|
||||
$transformer = new ModelToElasticaAutoTransformer();
|
||||
$transformer->transform(new POPO(), array('desc'));
|
||||
$transformer->transform(new POPO(), array('desc' => array()));
|
||||
}
|
||||
|
||||
public function testFileAddedForAttachmentMapping()
|
||||
{
|
||||
$transformer = new ModelToElasticaAutoTransformer();
|
||||
$document = $transformer->transform(new POPO(), array('file' => array('type' => 'attachment')));
|
||||
$data = $document->getData();
|
||||
|
||||
$this->assertEquals(base64_encode(file_get_contents(__DIR__ . '/../fixtures/attachment.odt')), $data['file']);
|
||||
}
|
||||
|
||||
public function testFileContentsAddedForAttachmentMapping()
|
||||
{
|
||||
$transformer = new ModelToElasticaAutoTransformer();
|
||||
$document = $transformer->transform(new POPO(), array('fileContents' => array('type' => 'attachment')));
|
||||
$data = $document->getData();
|
||||
|
||||
$this->assertEquals(base64_encode(file_get_contents(__DIR__ . '/../fixtures/attachment.odt')), $data['fileContents']);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,6 @@
|
|||
|
||||
namespace FOQ\ElasticaBundle\Transformer;
|
||||
|
||||
use Elastica_Document;
|
||||
use Traversable;
|
||||
use ArrayAccess;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Maps Elastica documents with Doctrine objects
|
||||
* This mapper assumes an exact match between
|
||||
|
|
@ -30,7 +25,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
|
|||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
$this->options = array_merge($this->options, $options);
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -42,18 +37,26 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
|
|||
**/
|
||||
public function transform($object, array $fields)
|
||||
{
|
||||
$array = array();
|
||||
foreach ($fields as $key) {
|
||||
$identifierGetter = 'get' . ucfirst($this->options['identifier']);
|
||||
$identifier = $object->$identifierGetter();
|
||||
$document = new \Elastica_Document($identifier);
|
||||
foreach ($fields as $key => $mapping) {
|
||||
$getter = 'get'.ucfirst($key);
|
||||
if (!is_callable(array($object, $getter))) {
|
||||
throw new RuntimeException(sprintf('The method %s::%s is not callable', get_class($object), $getter));
|
||||
throw new \RuntimeException(sprintf('The method %s::%s is not callable', get_class($object), $getter));
|
||||
}
|
||||
if (isset($mapping['type']) && $mapping['type'] == 'attachment') {
|
||||
$attachment = $object->$getter();
|
||||
if($attachment instanceof \SplFileInfo) {
|
||||
$document->addFile($key, $attachment->getPathName());
|
||||
} else {
|
||||
$document->addFileContent($key, $attachment);
|
||||
}
|
||||
} else {
|
||||
$document->add($key, $this->normalizeValue($object->$getter()));
|
||||
}
|
||||
$array[$key] = $this->normalizeValue($object->$getter());
|
||||
}
|
||||
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
|
||||
$identifier = $object->$identifierGetter();
|
||||
|
||||
return new Elastica_Document($identifier, $array);
|
||||
return $document;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -73,7 +76,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
|
|||
}
|
||||
};
|
||||
|
||||
if (is_array($value) || $value instanceof Traversable || $value instanceof ArrayAccess) {
|
||||
if (is_array($value) || $value instanceof \Traversable || $value instanceof \ArrayAccess) {
|
||||
$value = is_array($value) ? $value : iterator_to_array($value);
|
||||
array_walk_recursive($value, $normalizeValue);
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue