|
1
|
|
|
<?php |
|
2
|
|
|
namespace Netdudes\DataSourceryBundle\DataSource; |
|
3
|
|
|
|
|
4
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Configuration\Field; |
|
5
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Exception\InvalidDataTypeException; |
|
6
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Util\ChoicesBuilder; |
|
7
|
|
|
use Netdudes\DataSourceryBundle\DataType\BooleanDataType; |
|
8
|
|
|
use Netdudes\DataSourceryBundle\DataType\DateDataType; |
|
9
|
|
|
use Netdudes\DataSourceryBundle\DataType\EntityDataType; |
|
10
|
|
|
use Netdudes\DataSourceryBundle\DataType\NumberDataType; |
|
11
|
|
|
use Netdudes\DataSourceryBundle\DataType\PercentDataType; |
|
12
|
|
|
use Netdudes\DataSourceryBundle\DataType\SearchTextDataType; |
|
13
|
|
|
use Netdudes\DataSourceryBundle\DataType\StringDataType; |
|
14
|
|
|
use Netdudes\DataSourceryBundle\Transformers\TransformerInterface; |
|
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
17
|
|
|
|
|
18
|
|
|
class DataSourceBuilder implements DataSourceBuilderInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var EventDispatcherInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $eventDispatcher; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Field[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $fields = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var TransformerInterface[] |
|
32
|
|
|
*/ |
|
33
|
|
|
private $transformers = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Field[] |
|
37
|
|
|
*/ |
|
38
|
|
|
private $nativeFields = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $entityClass; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var ChoicesBuilder |
|
47
|
|
|
*/ |
|
48
|
|
|
private $choicesBuilder; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var DataSourceFactoryInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
private $dataSourceFactory; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $entityClass |
|
57
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
58
|
|
|
* @param DataSourceFactoryInterface $dataSourceFactory |
|
59
|
|
|
* @param ChoicesBuilder $choicesBuilder |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct($entityClass, EventDispatcherInterface $eventDispatcher, DataSourceFactoryInterface $dataSourceFactory, ChoicesBuilder $choicesBuilder) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->entityClass = $entityClass; |
|
64
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
65
|
|
|
$this->choicesBuilder = $choicesBuilder; |
|
66
|
|
|
$this->dataSourceFactory = $dataSourceFactory; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function addField($name, $type, $field, array $options = []) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->fields[] = $this->newField($name, $type, $field, $options); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function addNativeField($name, $type, $alias, array $options = []) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->nativeFields[] = $this->newNativeField($name, $type, $alias, $options); |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function addVectorField($name, $type, $filteringField, array $aliasedFields, array $options = []) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->fields[] = $this->newRawField($name, $type, $filteringField, $aliasedFields, $options); |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function addSearchField($name, $type) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->fields[] = $this->newRawField($name, $type, null, null, []); |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function removeField($name) |
|
98
|
|
|
{ |
|
99
|
|
|
foreach ($this->fields as $k => $field) { |
|
100
|
|
|
if ($field->getUniqueName() == $name) { |
|
101
|
|
|
unset($this->fields[$k]); |
|
102
|
|
|
break; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function addTransformer(TransformerInterface $transformer) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->transformers[] = $transformer; |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function addEventListener($eventName, $listener) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->eventDispatcher->addListener($eventName, $listener); |
|
119
|
|
|
|
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function addEventSubscriber(EventSubscriberInterface $eventSubscriber) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->eventDispatcher->addSubscriber($eventSubscriber); |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function build() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->dataSourceFactory->create( |
|
133
|
|
|
$this->entityClass, |
|
134
|
|
|
array_merge($this->fields, $this->nativeFields), |
|
135
|
|
|
$this->transformers, |
|
136
|
|
|
$this->eventDispatcher |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
private function newField($name, $type, $field, $options) |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->newRawField($name, $type, $field, null, $options); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
private function getOption($options, $key, $default) |
|
146
|
|
|
{ |
|
147
|
|
|
if (array_key_exists($key, $options)) { |
|
148
|
|
|
return $options[$key]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $default; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Helper function: matches a data type name given in the configuration to the needed |
|
156
|
|
|
* DataType instance. |
|
157
|
|
|
* |
|
158
|
|
|
* @param $type |
|
159
|
|
|
* |
|
160
|
|
|
* @throws InvalidDataTypeException |
|
161
|
|
|
* |
|
162
|
|
|
* @return mixed |
|
163
|
|
|
*/ |
|
164
|
|
|
private function getDataTypeByName($type) |
|
165
|
|
|
{ |
|
166
|
|
|
switch ($type) { |
|
167
|
|
|
case 'date': |
|
168
|
|
|
return new DateDataType(); |
|
169
|
|
|
case 'string': |
|
170
|
|
|
return new StringDataType(); |
|
171
|
|
|
case 'number': |
|
172
|
|
|
return new NumberDataType(); |
|
173
|
|
|
case 'boolean': |
|
174
|
|
|
return new BooleanDataType(); |
|
175
|
|
|
case 'entity': |
|
176
|
|
|
return new EntityDataType(); |
|
177
|
|
|
case 'percent': |
|
178
|
|
|
return new PercentDataType(); |
|
179
|
|
|
case 'search_text': |
|
180
|
|
|
return new SearchTextDataType(); |
|
181
|
|
|
default: |
|
182
|
|
|
throw new InvalidDataTypeException(); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
private function newNativeField($name, $type, $alias, $options) |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->newRawField($name, $type, null, $alias, $options); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
private function newRawField($name, $type, $field, $alias, $options) |
|
192
|
|
|
{ |
|
193
|
|
|
$readable = $this->getOption($options, 'readable', $name); |
|
194
|
|
|
$description = $this->getOption($options, 'description', ""); |
|
195
|
|
|
$choices = $this->getOption($options, 'choices', null); |
|
196
|
|
|
if ($choices) { |
|
197
|
|
|
$choices = $this->parseChoices($choices); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$type = $this->getDataTypeByName($type); |
|
201
|
|
|
|
|
202
|
|
|
return new Field($name, $readable, $description, $type, $field, $alias, $choices); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
private function parseChoices($choices) |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->choicesBuilder->build($choices); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|