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\Form; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Form\ChoiceList\ArrayChoiceList; |
15
|
|
|
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; |
16
|
|
|
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; |
17
|
|
|
|
18
|
|
|
class FacetChoiceLoader implements ChoiceLoaderInterface { |
19
|
|
|
|
20
|
|
|
/** @var ChoiceListInterface */ |
21
|
|
|
private $choiceList; |
22
|
|
|
|
23
|
|
|
private $facets; |
24
|
|
|
|
25
|
|
|
public function __construct(array $facets) { |
26
|
|
|
$this->facets = $facets; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function addFacets($facets) { |
30
|
|
|
$this->facets = array_merge($this->facets, $facets); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
* |
37
|
|
|
* @see \Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface::loadValuesForChoices() |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function loadValuesForChoices(array $choices, $value = null) { |
|
|
|
|
40
|
|
|
// is called on form creat with $choices containing the preset of the bound entity |
41
|
|
|
$values = array(); |
42
|
|
|
foreach($choices as $key => $choice) { |
43
|
|
|
// we use a DataTransformer, thus only plain values arrive as choices which can be used directly as value |
44
|
|
|
if(is_callable($value)) { |
45
|
|
|
$values[$key] = (string)call_user_func($value, $choice, $key); |
46
|
|
|
} else { |
47
|
|
|
$values[$key] = $choice; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// this has to be done by yourself: array( label => value ) |
52
|
|
|
// $labeledValues = MyLabelService::getLabels($values); |
|
|
|
|
53
|
|
|
|
54
|
|
|
// // create internal choice list from loaded values |
55
|
|
|
// $this->choiceList = new ArrayChoiceList($labeledValues, $value); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return $values; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
* |
64
|
|
|
* @see \Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface::loadChoiceList() |
65
|
|
|
*/ |
66
|
|
|
public function loadChoiceList($value = null) { |
67
|
|
|
// is called on form view create after loadValuesForChoices of form create |
68
|
|
|
// if($this->choiceList instanceof ChoiceListInterface) { |
|
|
|
|
69
|
|
|
// return $this->choiceList; |
|
|
|
|
70
|
|
|
// } |
71
|
|
|
|
72
|
|
|
// if no values preset yet return empty list |
73
|
|
|
$this->choiceList = new ArrayChoiceList($this->facets, $value); |
74
|
|
|
// dump($this->facets); |
|
|
|
|
75
|
|
|
return $this->choiceList; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
* |
82
|
|
|
* @see \Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface::loadChoicesForValues() |
83
|
|
|
* |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function loadChoicesForValues(array $values, $value = null) { |
|
|
|
|
86
|
|
|
// is called on form submit after loadValuesForChoices of form create and loadChoiceList of form view create |
87
|
|
|
$choices = array(); |
88
|
|
|
foreach($values as $key => $val) { |
89
|
|
|
// we use a DataTransformer, thus only plain values arrive as choices which can be used directly as value |
90
|
|
|
if(is_callable($value)) { |
91
|
|
|
$choices[$key] = (string)call_user_func($value, $val, $key); |
92
|
|
|
} else { |
93
|
|
|
$choices[$key] = $val; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// dump($values); |
|
|
|
|
98
|
|
|
// this has to be done by yourself: array( label => value ) |
99
|
|
|
// $labeledValues = MyLabelService::getLabels($values); |
|
|
|
|
100
|
|
|
|
101
|
|
|
// // reset internal choice list |
102
|
|
|
// $this->choiceList = new ArrayChoiceList($labeledValues, $value); |
|
|
|
|
103
|
|
|
|
104
|
|
|
return $choices; |
105
|
|
|
} |
106
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.