@@ -17,6 +17,9 @@ |
||
17 | 17 | */ |
18 | 18 | class StingerSoftEntitySearchBundle extends Bundle { |
19 | 19 | |
20 | + /** |
|
21 | + * @param string $env |
|
22 | + */ |
|
20 | 23 | public static function getRequiredBundles($env) { |
21 | 24 | $bundles = array(); |
22 | 25 | $bundles['StingerSoftEntitySearchBundle'] = '\StingerSoft\EntitySearchBundle\StingerSoftEntitySearchBundle'; |
@@ -30,10 +30,10 @@ discard block |
||
30 | 30 | } |
31 | 31 | |
32 | 32 | public function addFacetValue($key, $value, $increaseCounterBy = 1) { |
33 | - if(!isset($this->facets[$key])){ |
|
33 | + if (!isset($this->facets[$key])) { |
|
34 | 34 | $this->facets[$key] = array(); |
35 | 35 | } |
36 | - if(!isset($this->facets[$key][$value])){ |
|
36 | + if (!isset($this->facets[$key][$value])) { |
|
37 | 37 | $this->facets[$key][$value] = 0; |
38 | 38 | } |
39 | 39 | $this->facets[$key][$value] = $this->facets[$key][$value] + $increaseCounterBy; |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | * @see \StingerSoft\EntitySearchBundle\Model\Result\FacetSet::getFacet() |
47 | 47 | */ |
48 | 48 | public function getFacet($key) { |
49 | - if(isset($this->facets[$key])) { |
|
49 | + if (isset($this->facets[$key])) { |
|
50 | 50 | return $this->facets[$key]; |
51 | 51 | } else { |
52 | 52 | return; |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | $id = $document->getEntityClass(); |
30 | 30 | $id .= '#'; |
31 | 31 | $entityId = $document->getEntityId(); |
32 | - if(is_scalar($entityId)) { |
|
32 | + if (is_scalar($entityId)) { |
|
33 | 33 | $id .= $entityId; |
34 | 34 | } else { |
35 | 35 | $id .= md5(serialize($entityId)); |
@@ -75,15 +75,15 @@ discard block |
||
75 | 75 | */ |
76 | 76 | public function autocomplete($search, $maxResults = 10) { |
77 | 77 | $words = array(); |
78 | - foreach($this->index as $doc) { |
|
79 | - foreach($doc->getFields() as $content) { |
|
80 | - if(is_string($content)) { |
|
78 | + foreach ($this->index as $doc) { |
|
79 | + foreach ($doc->getFields() as $content) { |
|
80 | + if (is_string($content)) { |
|
81 | 81 | $words = array_merge($words, explode(' ', $content)); |
82 | 82 | } |
83 | 83 | } |
84 | 84 | } |
85 | 85 | |
86 | - return array_filter($words, function ($word) use ($search) { |
|
86 | + return array_filter($words, function($word) use ($search) { |
|
87 | 87 | return stripos($word, $search) === 0; |
88 | 88 | }); |
89 | 89 | } |
@@ -101,10 +101,10 @@ discard block |
||
101 | 101 | $facets = new FacetSetAdapter(); |
102 | 102 | |
103 | 103 | $hits = array(); |
104 | - foreach($this->index as $key => $doc) { |
|
105 | - foreach($doc->getFields() as $content) { |
|
106 | - if(is_string($content) && stripos($content, $term) !== false) { |
|
107 | - if(!isset($hits[$key])) { |
|
104 | + foreach ($this->index as $key => $doc) { |
|
105 | + foreach ($doc->getFields() as $content) { |
|
106 | + if (is_string($content) && stripos($content, $term) !== false) { |
|
107 | + if (!isset($hits[$key])) { |
|
108 | 108 | $hits[$key] = 0; |
109 | 109 | } |
110 | 110 | $hits[$key] = $hits[$key] + 1; |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | } |
114 | 114 | arsort($hits); |
115 | 115 | $results = array(); |
116 | - foreach(array_keys($hits) as $docId) { |
|
116 | + foreach (array_keys($hits) as $docId) { |
|
117 | 117 | $doc = $this->index[$docId]; |
118 | 118 | $facets->addFacetValue(FacetSet::FACET_ENTITY_TYPE, $doc->getEntityClass()); |
119 | 119 | $facets->addFacetValue(FacetSet::FACET_AUTHOR, $doc->getFieldValue(Document::FIELD_AUTHOR)); |
@@ -39,9 +39,9 @@ discard block |
||
39 | 39 | public function loadValuesForChoices(array $choices, $value = null) { |
40 | 40 | // is called on form creat with $choices containing the preset of the bound entity |
41 | 41 | $values = array(); |
42 | - foreach($choices as $key => $choice) { |
|
42 | + foreach ($choices as $key => $choice) { |
|
43 | 43 | // we use a DataTransformer, thus only plain values arrive as choices which can be used directly as value |
44 | - if(is_callable($value)) { |
|
44 | + if (is_callable($value)) { |
|
45 | 45 | $values[$key] = (string)call_user_func($value, $choice, $key); |
46 | 46 | } else { |
47 | 47 | $values[$key] = $choice; |
@@ -85,9 +85,9 @@ discard block |
||
85 | 85 | public function loadChoicesForValues(array $values, $value = null) { |
86 | 86 | // is called on form submit after loadValuesForChoices of form create and loadChoiceList of form view create |
87 | 87 | $choices = array(); |
88 | - foreach($values as $key => $val) { |
|
88 | + foreach ($values as $key => $val) { |
|
89 | 89 | // we use a DataTransformer, thus only plain values arrive as choices which can be used directly as value |
90 | - if(is_callable($value)) { |
|
90 | + if (is_callable($value)) { |
|
91 | 91 | $choices[$key] = (string)call_user_func($value, $val, $key); |
92 | 92 | } else { |
93 | 93 | $choices[$key] = $val; |
@@ -116,7 +116,7 @@ |
||
116 | 116 | $view = $form->createView(); |
117 | 117 | $children = $view->children; |
118 | 118 | |
119 | - foreach(array_keys($formData) as $key) { |
|
119 | + foreach (array_keys($formData) as $key) { |
|
120 | 120 | $this->assertArrayHasKey($key, $children); |
121 | 121 | } |
122 | 122 |
@@ -140,6 +140,9 @@ |
||
140 | 140 | // $this->assertCount(2, $typeForm->vars['choices']); |
141 | 141 | } |
142 | 142 | |
143 | + /** |
|
144 | + * @return ValidatorInterface |
|
145 | + */ |
|
143 | 146 | protected function mockValidator() { |
144 | 147 | $validator = $this->getMockBuilder(ValidatorInterface::class)->setMethods(array('validate', 'getErrors'))->getMockForAbstractClass(); |
145 | 148 | $validator->method('validate')->willReturn(array()); |
@@ -104,7 +104,7 @@ |
||
104 | 104 | * |
105 | 105 | * @param string $name |
106 | 106 | * Name of the property to set |
107 | - * @param mixed $value |
|
107 | + * @param string[] $value |
|
108 | 108 | * The value of the property |
109 | 109 | */ |
110 | 110 | public function __set($name, $value) { |
@@ -90,10 +90,10 @@ discard block |
||
90 | 90 | * @return \Pec\Bundle\MtuDvpBundle\Entity\TestRiskValue |
91 | 91 | */ |
92 | 92 | public function __get($name) { |
93 | - if(strrpos($name, 'facet_', -strlen($name)) !== false) { |
|
93 | + if (strrpos($name, 'facet_', -strlen($name)) !== false) { |
|
94 | 94 | $facetname = substr($name, 6); |
95 | 95 | |
96 | - if(isset($this->facets[$facetname])) { |
|
96 | + if (isset($this->facets[$facetname])) { |
|
97 | 97 | return $this->facets[$facetname]; |
98 | 98 | } |
99 | 99 | return array(); |
@@ -108,10 +108,10 @@ discard block |
||
108 | 108 | * The value of the property |
109 | 109 | */ |
110 | 110 | public function __set($name, $value) { |
111 | - if(strrpos($name, 'facet_', -strlen($name)) !== false) { |
|
111 | + if (strrpos($name, 'facet_', -strlen($name)) !== false) { |
|
112 | 112 | $facetname = substr($name, 6); |
113 | 113 | |
114 | - if(!isset($this->facets[$facetname])) { |
|
114 | + if (!isset($this->facets[$facetname])) { |
|
115 | 115 | $this->facets[$facetname] = array(); |
116 | 116 | } |
117 | 117 | $this->facets[$facetname] = $value; |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | * @return boolean |
126 | 126 | */ |
127 | 127 | public function __isset($name) { |
128 | - if(strrpos($name, 'facet_', -strlen($name)) !== false) { |
|
128 | + if (strrpos($name, 'facet_', -strlen($name)) !== false) { |
|
129 | 129 | return true; |
130 | 130 | } |
131 | 131 |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | |
17 | 17 | class FacetSetAdapterTest extends \PHPUnit_Framework_TestCase { |
18 | 18 | |
19 | - public function testAddFacetValue(){ |
|
19 | + public function testAddFacetValue() { |
|
20 | 20 | $facets = new FacetSetAdapter(array()); |
21 | 21 | $this->assertCount(0, $facets); |
22 | 22 | $facets->addFacetValue(Document::FIELD_AUTHOR, 'Oliver Kotte'); |
@@ -26,13 +26,13 @@ discard block |
||
26 | 26 | $this->assertCount(1, $facets); |
27 | 27 | $this->assertCount(2, $facets->getFacet(Document::FIELD_AUTHOR)); |
28 | 28 | |
29 | - foreach($facets as $facetKey => $facetValues){ |
|
29 | + foreach ($facets as $facetKey => $facetValues) { |
|
30 | 30 | $this->assertEquals(Document::FIELD_AUTHOR, $facetKey); |
31 | 31 | $this->assertCount(2, $facetValues); |
32 | 32 | } |
33 | 33 | } |
34 | 34 | |
35 | - public function testGetFacet(){ |
|
35 | + public function testGetFacet() { |
|
36 | 36 | $facets = new FacetSetAdapter(array()); |
37 | 37 | $this->assertCount(0, $facets); |
38 | 38 | $this->assertNull($facets->getFacet('NotExisting')); |
@@ -73,12 +73,12 @@ discard block |
||
73 | 73 | * @param LifecycleEventArgs $args |
74 | 74 | */ |
75 | 75 | public function postPersist(LifecycleEventArgs $args) { |
76 | - if(!$this->enableIndexing) return; |
|
76 | + if (!$this->enableIndexing) return; |
|
77 | 77 | $this->updateEntity($args->getObject(), $args->getObjectManager()); |
78 | 78 | } |
79 | 79 | |
80 | 80 | public function postFlush(PostFlushEventArgs $eventArgs) { |
81 | - if($this->needsFlush) { |
|
81 | + if ($this->needsFlush) { |
|
82 | 82 | $this->needsFlush = false; |
83 | 83 | $eventArgs->getEntityManager()->flush(); |
84 | 84 | } |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | * @param LifecycleEventArgs $args |
91 | 91 | */ |
92 | 92 | public function preRemove(LifecycleEventArgs $args) { |
93 | - if(!$this->enableIndexing) return; |
|
93 | + if (!$this->enableIndexing) return; |
|
94 | 94 | $this->removeEntity($args->getObject(), $args->getObjectManager()); |
95 | 95 | } |
96 | 96 | |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | * @param LifecycleEventArgs $args |
101 | 101 | */ |
102 | 102 | public function postUpdate(LifecycleEventArgs $args) { |
103 | - if(!$this->enableIndexing) return; |
|
103 | + if (!$this->enableIndexing) return; |
|
104 | 104 | $this->updateEntity($args->getObject(), $args->getObjectManager()); |
105 | 105 | } |
106 | 106 | |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | */ |
128 | 128 | protected function updateEntity($entity, ObjectManager $manager) { |
129 | 129 | $document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity); |
130 | - if($document !== false) { |
|
130 | + if ($document !== false) { |
|
131 | 131 | $this->getSearchService($manager)->saveDocument($document); |
132 | 132 | } |
133 | 133 | } |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | */ |
139 | 139 | protected function removeEntity($entity, ObjectManager $manager) { |
140 | 140 | $document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity); |
141 | - if($document !== false) { |
|
141 | + if ($document !== false) { |
|
142 | 142 | $this->getSearchService($manager)->removeDocument($document); |
143 | 143 | } |
144 | 144 | } |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | |
101 | 101 | /** |
102 | 102 | * |
103 | - * @param FormBuilderInterface|Form $builder |
|
103 | + * @param FormInterface $builder |
|
104 | 104 | * @param FacetSet $facets |
105 | 105 | */ |
106 | 106 | protected function createFacets($builder, FacetSet $facets, array $options, $data) { |
@@ -143,6 +143,9 @@ discard block |
||
143 | 143 | return $choices; |
144 | 144 | } |
145 | 145 | |
146 | + /** |
|
147 | + * @param string $facetType |
|
148 | + */ |
|
146 | 149 | protected function formatFacet($formatter, $facetType, $facet, $count) { |
147 | 150 | $default = $facet . ' (' . $count . ')'; |
148 | 151 | if(!$formatter) { |
@@ -53,26 +53,26 @@ discard block |
||
53 | 53 | $maxChoiceGroupCount = $options['max_choice_group_count']; |
54 | 54 | $data = array(); |
55 | 55 | |
56 | - if($usedFacets && !$result) { |
|
56 | + if ($usedFacets && !$result) { |
|
57 | 57 | $data = array(); |
58 | - foreach($usedFacets as $facetType => $facetTypeOptions) { |
|
58 | + foreach ($usedFacets as $facetType => $facetTypeOptions) { |
|
59 | 59 | $facetTypeOptions = is_array($facetTypeOptions) ? $facetTypeOptions : []; |
60 | 60 | $preferredChoices = $preferredFilterChoices[$facetType] ?? []; |
61 | 61 | $i = 0; |
62 | - $builder->add('facet_' . $facetType, FacetType::class, array_merge(array( |
|
63 | - 'label' => 'stinger_soft_entity_search.forms.query.' . $facetType . '.label', |
|
62 | + $builder->add('facet_'.$facetType, FacetType::class, array_merge(array( |
|
63 | + 'label' => 'stinger_soft_entity_search.forms.query.'.$facetType.'.label', |
|
64 | 64 | 'multiple' => true, |
65 | 65 | 'expanded' => true, |
66 | 66 | 'allow_extra_fields' => true, |
67 | - 'preferred_choices' => function ($val) use ($preferredChoices, $data, $facetType, $maxChoiceGroupCount, &$i) { |
|
68 | - return $i++ < $maxChoiceGroupCount || $maxChoiceGroupCount == 0 || in_array($val, $preferredChoices) || (isset($data['facet_' . $facetType]) && in_array($val, $data['facet_' . $facetType])); |
|
67 | + 'preferred_choices' => function($val) use ($preferredChoices, $data, $facetType, $maxChoiceGroupCount, &$i) { |
|
68 | + return $i++ < $maxChoiceGroupCount || $maxChoiceGroupCount == 0 || in_array($val, $preferredChoices) || (isset($data['facet_'.$facetType]) && in_array($val, $data['facet_'.$facetType])); |
|
69 | 69 | } |
70 | 70 | ), $facetTypeOptions)); |
71 | 71 | unset($i); |
72 | 72 | } |
73 | 73 | } |
74 | - if($result) { |
|
75 | - $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options, $result) { |
|
74 | + if ($result) { |
|
75 | + $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($options, $result) { |
|
76 | 76 | $this->createFacets($event->getForm(), $result->getFacets(), $options, $event->getData()); |
77 | 77 | }); |
78 | 78 | } |
@@ -111,19 +111,19 @@ discard block |
||
111 | 111 | $selectedFacets = $data->getFacets(); |
112 | 112 | $usedFacets = $options['used_facets']; |
113 | 113 | |
114 | - foreach($facets->getFacets() as $facetType => $facetValues) { |
|
114 | + foreach ($facets->getFacets() as $facetType => $facetValues) { |
|
115 | 115 | $preferredChoices = isset($preferredFilterChoices[$facetType]) ? $preferredFilterChoices[$facetType] : array(); |
116 | 116 | |
117 | 117 | $i = 0; |
118 | 118 | $facetTypeOptions = isset($usedFacets[$facetType]) ? $usedFacets[$facetType] : []; |
119 | 119 | $formatter = isset($options['facet_formatter'][$facetType]) ? $options['facet_formatter'][$facetType] : null; |
120 | - $builder->add('facet_' . $facetType, FacetType::class, array_merge(array( |
|
121 | - 'label' => 'stinger_soft_entity_search.forms.query.' . $facetType . '.label', |
|
120 | + $builder->add('facet_'.$facetType, FacetType::class, array_merge(array( |
|
121 | + 'label' => 'stinger_soft_entity_search.forms.query.'.$facetType.'.label', |
|
122 | 122 | 'multiple' => true, |
123 | 123 | 'expanded' => true, |
124 | 124 | 'allow_extra_fields' => true, |
125 | 125 | 'choices' => $this->generateFacetChoices($facetType, $facetValues, isset($selectedFacets[$facetType]) ? $selectedFacets[$facetType] : array(), $formatter), |
126 | - 'preferred_choices' => function ($val) use ($preferredChoices, $selectedFacets, $facetType, $maxChoiceGroupCount, &$i) { |
|
126 | + 'preferred_choices' => function($val) use ($preferredChoices, $selectedFacets, $facetType, $maxChoiceGroupCount, &$i) { |
|
127 | 127 | return $i++ < $maxChoiceGroupCount || $maxChoiceGroupCount == 0 || in_array($val, $preferredChoices) || (isset($selectedFacets[$facetType]) && in_array($val, $selectedFacets[$facetType])); |
128 | 128 | } |
129 | 129 | ), $facetTypeOptions)); |
@@ -138,13 +138,13 @@ discard block |
||
138 | 138 | */ |
139 | 139 | protected function generateFacetChoices($facetType, array $facets, array $selectedFacets = array(), $formatter) { |
140 | 140 | $choices = array(); |
141 | - foreach($facets as $facet => $count) { |
|
142 | - if($count == 0 && !in_array($facet, $selectedFacets)) |
|
141 | + foreach ($facets as $facet => $count) { |
|
142 | + if ($count == 0 && !in_array($facet, $selectedFacets)) |
|
143 | 143 | continue; |
144 | 144 | $choices[$this->formatFacet($formatter, $facetType, $facet, $count)] = $facet; |
145 | 145 | } |
146 | - foreach($selectedFacets as $facet) { |
|
147 | - if(isset($facets[$facet])) continue; |
|
146 | + foreach ($selectedFacets as $facet) { |
|
147 | + if (isset($facets[$facet])) continue; |
|
148 | 148 | $count = 0; |
149 | 149 | $choices[$this->formatFacet($formatter, $facetType, $facet, $count)] = $facet; |
150 | 150 | } |
@@ -152,8 +152,8 @@ discard block |
||
152 | 152 | } |
153 | 153 | |
154 | 154 | protected function formatFacet($formatter, $facetType, $facet, $count) { |
155 | - $default = $facet . ' (' . $count . ')'; |
|
156 | - if(!$formatter) { |
|
155 | + $default = $facet.' ('.$count.')'; |
|
156 | + if (!$formatter) { |
|
157 | 157 | return $default; |
158 | 158 | } |
159 | 159 | return call_user_func($formatter, $facetType, $facet, $count, $default); |