@@ -48,18 +48,18 @@ discard block |
||
48 | 48 | */ |
49 | 49 | public function __construct(SearchService $searchService, array $mapping = array()) { |
50 | 50 | $this->searchService = $searchService; |
51 | - foreach($mapping as $key => $config) { |
|
52 | - if(!isset($config['mappings'])) { |
|
53 | - throw new \InvalidArgumentException($key . ' has no mapping defined!'); |
|
51 | + foreach ($mapping as $key => $config) { |
|
52 | + if (!isset($config['mappings'])) { |
|
53 | + throw new \InvalidArgumentException($key.' has no mapping defined!'); |
|
54 | 54 | } |
55 | - if(!isset($config['persistence'])) { |
|
56 | - throw new \InvalidArgumentException($key . ' has no persistence defined!'); |
|
55 | + if (!isset($config['persistence'])) { |
|
56 | + throw new \InvalidArgumentException($key.' has no persistence defined!'); |
|
57 | 57 | } |
58 | - if(!isset($config['persistence']['model'])) { |
|
59 | - throw new \InvalidArgumentException($key . ' has no model defined!'); |
|
58 | + if (!isset($config['persistence']['model'])) { |
|
59 | + throw new \InvalidArgumentException($key.' has no model defined!'); |
|
60 | 60 | } |
61 | 61 | $map = array(); |
62 | - foreach($config['mappings'] as $fieldKey => $fieldConfig) { |
|
62 | + foreach ($config['mappings'] as $fieldKey => $fieldConfig) { |
|
63 | 63 | $map[$fieldKey] = isset($fieldConfig['propertyPath']) && $fieldConfig['propertyPath'] ? $fieldConfig['propertyPath'] : $fieldKey; |
64 | 64 | } |
65 | 65 | |
@@ -74,10 +74,10 @@ discard block |
||
74 | 74 | * @see \StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface::isIndexable() |
75 | 75 | */ |
76 | 76 | public function isIndexable(object $object) : bool { |
77 | - if($object instanceof SearchableEntity) { |
|
77 | + if ($object instanceof SearchableEntity) { |
|
78 | 78 | return true; |
79 | 79 | } |
80 | - if(count($this->getMapping(get_class($object))) > 0) { |
|
80 | + if (count($this->getMapping(get_class($object))) > 0) { |
|
81 | 81 | return true; |
82 | 82 | } |
83 | 83 | return false; |
@@ -92,10 +92,10 @@ discard block |
||
92 | 92 | */ |
93 | 93 | public function isClassIndexable(string $clazz) : bool { |
94 | 94 | $reflectionClass = new \ReflectionClass($clazz); |
95 | - if(array_key_exists(SearchableEntity::class, $reflectionClass->getInterfaces())) { |
|
95 | + if (array_key_exists(SearchableEntity::class, $reflectionClass->getInterfaces())) { |
|
96 | 96 | return true; |
97 | 97 | } |
98 | - if(count($this->getMapping($clazz)) > 0) { |
|
98 | + if (count($this->getMapping($clazz)) > 0) { |
|
99 | 99 | return true; |
100 | 100 | } |
101 | 101 | return false; |
@@ -107,11 +107,11 @@ discard block |
||
107 | 107 | * @see \StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface::createDocument() |
108 | 108 | */ |
109 | 109 | public function createDocument(ObjectManager $manager, object $object) : ?Document { |
110 | - if(!$this->isIndexable($object)) |
|
110 | + if (!$this->isIndexable($object)) |
|
111 | 111 | return null; |
112 | 112 | $document = $this->searchService->createEmptyDocumentFromEntity($object); |
113 | 113 | $index = $this->fillDocument($document, $object); |
114 | - if($index === false) |
|
114 | + if ($index === false) |
|
115 | 115 | return null; |
116 | 116 | |
117 | 117 | return $document; |
@@ -125,12 +125,12 @@ discard block |
||
125 | 125 | * @return boolean |
126 | 126 | */ |
127 | 127 | protected function fillDocument(Document $document, object $object) : bool { |
128 | - if($object instanceof SearchableEntity) { |
|
128 | + if ($object instanceof SearchableEntity) { |
|
129 | 129 | return $object->indexEntity($document); |
130 | 130 | } |
131 | 131 | $mapping = $this->getMapping(\get_class($object)); |
132 | 132 | $accessor = PropertyAccess::createPropertyAccessor(); |
133 | - foreach($mapping as $fieldName => $propertyPath) { |
|
133 | + foreach ($mapping as $fieldName => $propertyPath) { |
|
134 | 134 | $document->addField($fieldName, $accessor->getValue($object, $propertyPath)); |
135 | 135 | } |
136 | 136 | return true; |
@@ -143,15 +143,15 @@ discard block |
||
143 | 143 | * @throws \ReflectionException |
144 | 144 | */ |
145 | 145 | protected function getMapping(string $clazz) : array { |
146 | - if(isset($this->cachedMapping[$clazz])) { |
|
146 | + if (isset($this->cachedMapping[$clazz])) { |
|
147 | 147 | return $this->cachedMapping[$clazz]; |
148 | 148 | } |
149 | 149 | $ref = new \ReflectionClass($clazz); |
150 | 150 | |
151 | 151 | $mapping = array(); |
152 | 152 | |
153 | - foreach($this->mapping as $className => $config) { |
|
154 | - if($clazz === $className || $ref->isSubclassOf($className)) { |
|
153 | + foreach ($this->mapping as $className => $config) { |
|
154 | + if ($clazz === $className || $ref->isSubclassOf($className)) { |
|
155 | 155 | $mapping = \array_merge($mapping, $config); |
156 | 156 | } |
157 | 157 | } |
@@ -107,12 +107,14 @@ |
||
107 | 107 | * @see \StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface::createDocument() |
108 | 108 | */ |
109 | 109 | public function createDocument(ObjectManager $manager, object $object) : ?Document { |
110 | - if(!$this->isIndexable($object)) |
|
111 | - return null; |
|
110 | + if(!$this->isIndexable($object)) { |
|
111 | + return null; |
|
112 | + } |
|
112 | 113 | $document = $this->searchService->createEmptyDocumentFromEntity($object); |
113 | 114 | $index = $this->fillDocument($document, $object); |
114 | - if($index === false) |
|
115 | - return null; |
|
115 | + if($index === false) { |
|
116 | + return null; |
|
117 | + } |
|
116 | 118 | |
117 | 119 | return $document; |
118 | 120 | } |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @param LifecycleEventArgs $args |
96 | 96 | */ |
97 | 97 | public function postPersist(LifecycleEventArgs $args): void { |
98 | - if(!$this->enableIndexing) { |
|
98 | + if (!$this->enableIndexing) { |
|
99 | 99 | return; |
100 | 100 | } |
101 | 101 | $this->updateEntity($args->getObject(), $args->getObjectManager()); |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | * @throws \Doctrine\ORM\OptimisticLockException |
108 | 108 | */ |
109 | 109 | public function postFlush(PostFlushEventArgs $eventArgs): void { |
110 | - if($this->needsFlush) { |
|
110 | + if ($this->needsFlush) { |
|
111 | 111 | $this->needsFlush = false; |
112 | 112 | $eventArgs->getEntityManager()->flush(); |
113 | 113 | } |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | * @param LifecycleEventArgs $args |
120 | 120 | */ |
121 | 121 | public function preRemove(LifecycleEventArgs $args): void { |
122 | - if(!$this->enableIndexing) { |
|
122 | + if (!$this->enableIndexing) { |
|
123 | 123 | return; |
124 | 124 | } |
125 | 125 | $this->removeEntity($args->getObject(), $args->getObjectManager()); |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | * @param LifecycleEventArgs $args |
132 | 132 | */ |
133 | 133 | public function postUpdate(LifecycleEventArgs $args): void { |
134 | - if(!$this->enableIndexing) return; |
|
134 | + if (!$this->enableIndexing) return; |
|
135 | 135 | $this->updateEntity($args->getObject(), $args->getObjectManager()); |
136 | 136 | } |
137 | 137 | |
@@ -156,11 +156,11 @@ discard block |
||
156 | 156 | * @param object $entity |
157 | 157 | */ |
158 | 158 | protected function updateEntity(object $entity, ObjectManager $manager): void { |
159 | - if($entity instanceof SearchableAlias) { |
|
159 | + if ($entity instanceof SearchableAlias) { |
|
160 | 160 | $entity = $entity->getEntityToIndex(); |
161 | 161 | } |
162 | 162 | $document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity); |
163 | - if($document !== null) { |
|
163 | + if ($document !== null) { |
|
164 | 164 | $this->getSearchService()->saveDocument($document); |
165 | 165 | $this->needsFlush = true; |
166 | 166 | } |
@@ -171,11 +171,11 @@ discard block |
||
171 | 171 | * @param object $entity |
172 | 172 | */ |
173 | 173 | protected function removeEntity(object $entity, ObjectManager $manager): void { |
174 | - if($entity instanceof SearchableAlias) { |
|
174 | + if ($entity instanceof SearchableAlias) { |
|
175 | 175 | $entity = $entity->getEntityToIndex(); |
176 | 176 | } |
177 | 177 | $document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity); |
178 | - if($document !== null) { |
|
178 | + if ($document !== null) { |
|
179 | 179 | $this->getSearchService()->removeDocument($document); |
180 | 180 | $this->needsFlush = true; |
181 | 181 | } |
@@ -131,7 +131,9 @@ |
||
131 | 131 | * @param LifecycleEventArgs $args |
132 | 132 | */ |
133 | 133 | public function postUpdate(LifecycleEventArgs $args): void { |
134 | - if(!$this->enableIndexing) return; |
|
134 | + if(!$this->enableIndexing) { |
|
135 | + return; |
|
136 | + } |
|
135 | 137 | $this->updateEntity($args->getObject(), $args->getObjectManager()); |
136 | 138 | } |
137 | 139 |
@@ -66,9 +66,9 @@ discard block |
||
66 | 66 | */ |
67 | 67 | public function autocomplete(string $search, int $maxResults = 10): array { |
68 | 68 | $words = array(); |
69 | - foreach($this->index as $doc) { |
|
70 | - foreach($doc->getFields() as $content) { |
|
71 | - if(is_string($content)) { |
|
69 | + foreach ($this->index as $doc) { |
|
70 | + foreach ($doc->getFields() as $content) { |
|
71 | + if (is_string($content)) { |
|
72 | 72 | $words = array_merge($words, explode(' ', $content)); |
73 | 73 | } |
74 | 74 | } |
@@ -92,10 +92,10 @@ discard block |
||
92 | 92 | $facets = new FacetSetAdapter(); |
93 | 93 | |
94 | 94 | $hits = array(); |
95 | - foreach($this->index as $key => $doc) { |
|
96 | - foreach($doc->getFields() as $content) { |
|
97 | - if(is_string($content) && stripos($content, $term) !== false) { |
|
98 | - if(!isset($hits[$key])) { |
|
95 | + foreach ($this->index as $key => $doc) { |
|
96 | + foreach ($doc->getFields() as $content) { |
|
97 | + if (is_string($content) && stripos($content, $term) !== false) { |
|
98 | + if (!isset($hits[$key])) { |
|
99 | 99 | $hits[$key] = 0; |
100 | 100 | } |
101 | 101 | $hits[$key] = $hits[$key] + 1; |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | } |
105 | 105 | arsort($hits); |
106 | 106 | $results = array(); |
107 | - foreach(array_keys($hits) as $docId) { |
|
107 | + foreach (array_keys($hits) as $docId) { |
|
108 | 108 | $doc = $this->index[$docId]; |
109 | 109 | $facets->addFacetValue(FacetSet::FACET_ENTITY_TYPE, $doc->getEntityClass()); |
110 | 110 | $facets->addFacetValue(FacetSet::FACET_AUTHOR, (string)$doc->getFieldValue(Document::FIELD_AUTHOR)); |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | $id = $document->getEntityClass(); |
132 | 132 | $id .= '#'; |
133 | 133 | $entityId = $document->getEntityId(); |
134 | - if(is_scalar($entityId)) { |
|
134 | + if (is_scalar($entityId)) { |
|
135 | 135 | $id .= $entityId; |
136 | 136 | } else { |
137 | 137 | $id .= md5(serialize($entityId)); |
@@ -52,7 +52,7 @@ |
||
52 | 52 | * @required |
53 | 53 | */ |
54 | 54 | public function setObjectManager(ObjectManager $om): void { |
55 | - if($this->objectManager) |
|
55 | + if ($this->objectManager) |
|
56 | 56 | return; |
57 | 57 | $this->objectManager = $om; |
58 | 58 | } |
@@ -52,8 +52,9 @@ |
||
52 | 52 | * @required |
53 | 53 | */ |
54 | 54 | public function setObjectManager(ObjectManager $om): void { |
55 | - if($this->objectManager) |
|
56 | - return; |
|
55 | + if($this->objectManager) { |
|
56 | + return; |
|
57 | + } |
|
57 | 58 | $this->objectManager = $om; |
58 | 59 | } |
59 | 60 |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function getFieldValue($fieldName) { |
86 | 86 | $value = $this->fields[$fieldName] ?? null; |
87 | - if(\in_array($fieldName, self::$forceSingleValueFields) && \is_array($value)) { |
|
87 | + if (\in_array($fieldName, self::$forceSingleValueFields) && \is_array($value)) { |
|
88 | 88 | return current($value); |
89 | 89 | } |
90 | 90 | return $value; |
@@ -97,15 +97,15 @@ discard block |
||
97 | 97 | * @see \StingerSoft\EntitySearchBundle\Model\Document::addMultiValueField() |
98 | 98 | */ |
99 | 99 | public function addMultiValueField(string $fieldName, $value): void { |
100 | - if(!array_key_exists($fieldName, $this->fields)) { |
|
100 | + if (!array_key_exists($fieldName, $this->fields)) { |
|
101 | 101 | $this->fields[$fieldName] = array( |
102 | 102 | $value |
103 | 103 | ); |
104 | - } else if(!\is_array($this->fields[$fieldName])) { |
|
104 | + } else if (!\is_array($this->fields[$fieldName])) { |
|
105 | 105 | $this->fields[$fieldName] = array( |
106 | 106 | $value, $this->fields[$fieldName] |
107 | 107 | ); |
108 | - } else if(!\in_array($value, $this->fields[$fieldName])) { |
|
108 | + } else if (!\in_array($value, $this->fields[$fieldName])) { |
|
109 | 109 | $this->fields[$fieldName][] = $value; |
110 | 110 | } |
111 | 111 | } |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | */ |
129 | 129 | public function setEntityClass(string $clazz): void { |
130 | 130 | $this->entityClass = $clazz; |
131 | - if(!$this->entityType) { |
|
131 | + if (!$this->entityType) { |
|
132 | 132 | $this->entityType = $clazz; |
133 | 133 | } |
134 | 134 | } |
@@ -33,10 +33,10 @@ discard block |
||
33 | 33 | |
34 | 34 | public function addFacetValue(string $key, string $label, $value = null, int $increaseCounterBy = 1) : void { |
35 | 35 | $value = $value ?? $label; |
36 | - if(!isset($this->facets[$key])) { |
|
36 | + if (!isset($this->facets[$key])) { |
|
37 | 37 | $this->facets[$key] = array(); |
38 | 38 | } |
39 | - if(!isset($this->facets[$key][$label])) { |
|
39 | + if (!isset($this->facets[$key][$label])) { |
|
40 | 40 | $this->facets[$key][$label] = array( |
41 | 41 | 'value' => $value, |
42 | 42 | 'count' => 0, |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * @see \StingerSoft\EntitySearchBundle\Model\Result\FacetSet::getFacet() |
53 | 53 | */ |
54 | 54 | public function getFacet(string $key): ?array { |
55 | - if(isset($this->facets[$key])) { |
|
55 | + if (isset($this->facets[$key])) { |
|
56 | 56 | return $this->facets[$key]; |
57 | 57 | } |
58 | 58 | return null; |