1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stinger Entity Search package. |
5
|
|
|
* |
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
7
|
|
|
* (c) Florian Meyer <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
namespace StingerSoft\EntitySearchBundle\Services\Mapping; |
13
|
|
|
|
14
|
|
|
use StingerSoft\EntitySearchBundle\Model\SearchableEntity; |
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
16
|
|
|
use StingerSoft\EntitySearchBundle\Model\Document; |
17
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
18
|
|
|
use StingerSoft\EntitySearchBundle\Services\SearchService; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Handles the creation of documents out of entities |
22
|
|
|
*/ |
23
|
|
|
class EntityToDocumentMapper implements EntityToDocumentMapperInterface { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @var SearchService |
28
|
|
|
*/ |
29
|
|
|
protected $searchService; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @var string[string] |
34
|
|
|
*/ |
35
|
|
|
protected $mapping = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
* @var string[string] |
40
|
|
|
*/ |
41
|
|
|
protected $cachedMapping = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @param SearchService $searchService |
47
|
|
|
*/ |
48
|
|
|
public function __construct(SearchService $searchService, array $mapping = array()) { |
49
|
|
|
$this->searchService = $searchService; |
50
|
|
|
foreach($mapping as $key => $config) { |
51
|
|
|
if(!isset($config['mappings'])) { |
52
|
|
|
throw new \InvalidArgumentException($key . ' has no mapping defined!'); |
53
|
|
|
} |
54
|
|
|
if(!isset($config['persistence'])) { |
55
|
|
|
throw new \InvalidArgumentException($key . ' has no persistence defined!'); |
56
|
|
|
} |
57
|
|
|
if(!isset($config['persistence']['model'])) { |
58
|
|
|
throw new \InvalidArgumentException($key . ' has no model defined!'); |
59
|
|
|
} |
60
|
|
|
$map = array(); |
61
|
|
|
foreach($config['mappings'] as $fieldKey => $fieldConfig) { |
62
|
|
|
$map[$fieldKey] = isset($fieldConfig['propertyPath']) && $fieldConfig['propertyPath'] ? $fieldConfig['propertyPath'] : $fieldKey; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->mapping[$config['persistence']['model']] = $map; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
* |
73
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface::isIndexable() |
74
|
|
|
*/ |
75
|
|
|
public function isIndexable($object) { |
76
|
|
|
if($object instanceof SearchableEntity) { |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
if(count($this->getMapping(get_class($object))) > 0) { |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
* |
89
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface::isClassIndexable() |
90
|
|
|
*/ |
91
|
|
|
public function isClassIndexable($clazz) { |
92
|
|
|
$reflectionClass = new \ReflectionClass($clazz); |
93
|
|
|
if(array_key_exists(SearchableEntity::class, $reflectionClass->getInterfaces())) { |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
if(count($this->getMapping($clazz)) > 0) { |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritDoc} |
105
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface::createDocument() |
106
|
|
|
*/ |
107
|
|
|
public function createDocument(ObjectManager $manager, $object) { |
108
|
|
|
if(!$this->isIndexable($object)) |
109
|
|
|
return false; |
110
|
|
|
$document = $this->getSearchService($manager)->createEmptyDocumentFromEntity($object); |
111
|
|
|
$index = $this->fillDocument($document, $object); |
112
|
|
|
if($index == false) |
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
|
115
|
|
|
return $document; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Fills the given document based on the object |
120
|
|
|
* |
121
|
|
|
* @param Document $document |
122
|
|
|
* @param object $object |
123
|
|
|
* @return boolean |
124
|
|
|
*/ |
125
|
|
|
protected function fillDocument(Document &$document, $object) { |
126
|
|
|
if($object instanceof SearchableEntity) { |
127
|
|
|
return $object->indexEntity($document); |
128
|
|
|
} |
129
|
|
|
$mapping = $this->getMapping(get_class($object)); |
130
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
131
|
|
|
foreach($mapping as $fieldName => $propertyPath) { |
132
|
|
|
$document->addField($fieldName, $accessor->getValue($object, $propertyPath)); |
133
|
|
|
} |
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Fetches the mapping for the given object including the mapping of superclasses |
139
|
|
|
* |
140
|
|
|
* @param object $object |
|
|
|
|
141
|
|
|
* @return \StingerSoft\EntitySearchBundle\Services\string[string] |
|
|
|
|
142
|
|
|
*/ |
143
|
|
|
protected function getMapping($clazz) { |
144
|
|
|
if(isset($this->cachedMapping[$clazz])) { |
145
|
|
|
return $this->cachedMapping[$clazz]; |
146
|
|
|
} |
147
|
|
|
$ref = new \ReflectionClass($clazz); |
148
|
|
|
|
149
|
|
|
$mapping = array(); |
150
|
|
|
|
151
|
|
|
foreach($this->mapping as $className => $config) { |
152
|
|
|
if($clazz == $className || $ref->isSubclassOf($className)) { |
153
|
|
|
$mapping = array_merge($mapping, $config); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->cachedMapping[$clazz] = $mapping; |
158
|
|
|
|
159
|
|
|
return $mapping; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns the search service |
164
|
|
|
* |
165
|
|
|
* @return SearchService |
166
|
|
|
*/ |
167
|
|
|
protected function getSearchService(ObjectManager $manager) { |
168
|
|
|
$this->searchService->setObjectManager($manager); |
169
|
|
|
return $this->searchService; |
170
|
|
|
} |
171
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.