|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <https://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\Index; |
|
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 callable $serializer |
|
31
|
|
|
*/ |
|
32
|
6 |
|
public function __construct(Index $index, ModelToElasticaTransformerInterface $transformer, string $objectClass, $serializer, array $options = []) |
|
33
|
|
|
{ |
|
34
|
6 |
|
parent::__construct($index, $transformer, $objectClass, [], $options); |
|
35
|
|
|
|
|
36
|
6 |
|
$this->serializer = $serializer; |
|
37
|
6 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Transforms an object to an elastica document |
|
41
|
|
|
* with just the identifier set. |
|
42
|
|
|
*/ |
|
43
|
6 |
|
public function transformToElasticaDocument(object $object): Document |
|
44
|
|
|
{ |
|
45
|
6 |
|
$document = $this->transformer->transform($object, []); |
|
46
|
|
|
|
|
47
|
6 |
|
$data = \call_user_func($this->serializer, $object); |
|
48
|
6 |
|
$document->setData($data); |
|
49
|
|
|
|
|
50
|
6 |
|
return $document; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|