1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Persister; |
13
|
|
|
|
14
|
|
|
use Elastica\Document; |
15
|
|
|
use Elastica\Type; |
16
|
|
|
use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Inserts, replaces and deletes single objects in an elastica type, making use |
20
|
|
|
* of elastica's serializer support to convert objects in to elastica documents. |
21
|
|
|
* Accepts domain model objects and passes them directly to elastica. |
22
|
|
|
* |
23
|
|
|
* @author Lea Haensenberber <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ObjectSerializerPersister extends ObjectPersister |
26
|
|
|
{ |
27
|
|
|
protected $serializer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Type $type |
31
|
|
|
* @param ModelToElasticaTransformerInterface $transformer |
32
|
|
|
* @param string $objectClass |
33
|
|
|
* @param callable $serializer |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, $serializer) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($type, $transformer, $objectClass, []); |
38
|
|
|
|
39
|
|
|
$this->serializer = $serializer; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Transforms an object to an elastica document |
44
|
|
|
* with just the identifier set. |
45
|
|
|
* |
46
|
|
|
* @param object $object |
47
|
|
|
* |
48
|
|
|
* @return Document the elastica document |
49
|
|
|
*/ |
50
|
|
|
public function transformToElasticaDocument($object) |
51
|
|
|
{ |
52
|
|
|
$document = $this->transformer->transform($object, []); |
53
|
|
|
|
54
|
|
|
$data = call_user_func($this->serializer, $object); |
55
|
|
|
$document->setData($data); |
56
|
|
|
|
57
|
|
|
return $document; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|