1515use Omines \DataTablesBundle \Adapter \AdapterInterface ;
1616use Omines \DataTablesBundle \Adapter \ResultSetInterface ;
1717use Omines \DataTablesBundle \Column \AbstractColumn ;
18- use Symfony \ Component \DependencyInjection \ServiceLocator ;
18+ use Omines \ DataTablesBundle \DependencyInjection \Instantiator ;
1919use Symfony \Component \HttpFoundation \JsonResponse ;
2020use Symfony \Component \HttpFoundation \Request ;
2121use Symfony \Component \OptionsResolver \OptionsResolver ;
2727 */
2828class DataTable
2929{
30- const DEFAULT_SETTINGS = [
31- 'name ' => 'dt ' ,
32- 'class_name ' => 'table table-bordered ' ,
33- 'column_filter ' => null ,
34- 'method ' => Request::METHOD_POST ,
35- 'language_from_cdn ' => true ,
36- 'request_state ' => null ,
37- 'template ' => '@DataTables/datatable_html.html.twig ' ,
38- 'translation_domain ' => 'messages ' ,
39- ];
40-
4130 const DEFAULT_OPTIONS = [
4231 'jQueryUI ' => false ,
4332 'pagingType ' => 'full_numbers ' ,
@@ -53,68 +42,71 @@ class DataTable
5342 'search ' => null ,
5443 'autoWidth ' => false ,
5544 'order ' => [],
56- 'ajax ' => true , //can contain the callback url
5745 'searchDelay ' => 400 ,
5846 'dom ' => 'lftrip ' ,
5947 'orderCellsTop ' => true ,
6048 'stateSave ' => false ,
6149 ];
6250
51+ const DEFAULT_TEMPLATE = '@DataTables/datatable_html.html.twig ' ;
6352 const SORT_ASCENDING = 'asc ' ;
6453 const SORT_DESCENDING = 'desc ' ;
6554
66- /** @var ServiceLocator */
67- private $ adapterLocator ;
55+ /** @var AdapterInterface */
56+ protected $ adapter ;
6857
6958 /** @var AbstractColumn[] */
7059 protected $ columns = [];
7160
7261 /** @var array<string, AbstractColumn> */
7362 protected $ columnsByName = [];
7463
64+ /** @var string */
65+ protected $ method = Request::METHOD_POST ;
66+
7567 /** @var array */
7668 protected $ options ;
7769
70+ /** @var bool */
71+ protected $ languageFromCDN = true ;
72+
73+ /** @var string */
74+ protected $ name = 'dt ' ;
75+
76+ /** @var string */
77+ protected $ template = self ::DEFAULT_TEMPLATE ;
78+
7879 /** @var array */
79- protected $ settings ;
80+ protected $ templateParams = [] ;
8081
8182 /** @var callable */
8283 protected $ transformer ;
8384
84- /** @var AdapterInterface */
85- protected $ adapter ;
85+ /** @var string */
86+ protected $ translationDomain = ' messages ' ;
8687
8788 /** @var DataTableRendererInterface */
8889 private $ renderer ;
8990
9091 /** @var DataTableState */
9192 private $ state ;
9293
94+ /** @var null|Instantiator */
95+ private $ instantiator ;
96+
9397 /**
9498 * DataTable constructor.
9599 *
96- * @param array $settings
97100 * @param array $options
98- * @param DataTableState $state
99- * @param ServiceLocator $adapterLocator
101+ * @param Instantiator|null $instantiator
100102 */
101- public function __construct (array $ settings = [], array $ options = [], DataTableState $ state = null , ServiceLocator $ adapterLocator = null )
103+ public function __construct (array $ options = [], Instantiator $ instantiator = null )
102104 {
103- $ this ->state = $ state ;
104- $ this ->adapterLocator = $ adapterLocator ;
105+ $ this ->instantiator = $ instantiator ;
105106
106107 $ resolver = new OptionsResolver ();
107108 $ this ->configureOptions ($ resolver );
108109 $ this ->options = $ resolver ->resolve ($ options );
109-
110- $ resolver = new OptionsResolver ();
111- $ this ->configureSettings ($ resolver );
112- $ this ->settings = $ resolver ->resolve ($ settings );
113-
114- // Temporarily disable column filters until their functionality has been restored
115- if (null !== $ this ->settings ['column_filter ' ]) {
116- throw new \LogicException ("The 'column_filter' setting is currently not supported and must be null " );
117- }
118110 }
119111
120112 /**
@@ -138,6 +130,44 @@ public function add(string $name, string $type, array $options = [])
138130 return $ this ;
139131 }
140132
133+ /**
134+ * @param int|string|AbstractColumn $column
135+ * @param string $direction
136+ * @return self
137+ */
138+ public function addOrderBy ($ column , string $ direction = self ::SORT_ASCENDING )
139+ {
140+ if (!$ column instanceof AbstractColumn) {
141+ $ column = is_int ($ column ) ? $ this ->getColumn ($ column ) : $ this ->getColumnByName ((string ) $ column );
142+ }
143+ $ this ->options ['order ' ][] = [$ column ->getIndex (), $ direction ];
144+
145+ return $ this ;
146+ }
147+
148+ /**
149+ * @param string $adapter
150+ * @return DataTable
151+ */
152+ public function createAdapter (string $ adapter , array $ options = []): self
153+ {
154+ if (null !== $ this ->instantiator && $ instance = $ this ->instantiator ->getAdapter ($ adapter )) {
155+ return $ this ->setAdapter ($ instance , $ options );
156+ } elseif (class_exists ($ adapter ) && in_array (AdapterInterface::class, class_implements ($ adapter ), true )) {
157+ return $ this ->setAdapter (new $ adapter (), $ options );
158+ } else {
159+ throw new \InvalidArgumentException (sprintf ('Could not resolve adapter type "%s" to a service or class implementing AdapterInterface ' , $ adapter ));
160+ }
161+ }
162+
163+ /**
164+ * @return AdapterInterface
165+ */
166+ public function getAdapter (): AdapterInterface
167+ {
168+ return $ this ->adapter ;
169+ }
170+
141171 /**
142172 * @param int $index
143173 * @return AbstractColumn
@@ -173,57 +203,35 @@ public function getColumns(): array
173203 }
174204
175205 /**
176- * @return string
206+ * @return bool
177207 */
178- public function getMethod (): string
208+ public function isLanguageFromCDN (): bool
179209 {
180- return $ this ->settings [ ' method ' ] ;
210+ return $ this ->languageFromCDN ;
181211 }
182212
183213 /**
184214 * @return string
185215 */
186- public function getName (): string
216+ public function getMethod (): string
187217 {
188- return $ this ->settings [ ' name ' ] ;
218+ return $ this ->method ;
189219 }
190220
191221 /**
192- * @return AdapterInterface
222+ * @return string
193223 */
194- public function getAdapter (): AdapterInterface
224+ public function getName (): string
195225 {
196- return $ this ->adapter ;
226+ return $ this ->name ;
197227 }
198228
199229 /**
200- * @param string $adapter
201- * @return DataTable
202- */
203- public function createAdapter (string $ adapter , array $ options = []): self
204- {
205- if (null !== $ this ->adapterLocator && $ this ->adapterLocator ->has ($ adapter )) {
206- return $ this ->setAdapter ($ this ->adapterLocator ->get ($ adapter ), $ options );
207- } elseif (class_exists ($ adapter ) && in_array (AdapterInterface::class, class_implements ($ adapter ), true )) {
208- return $ this ->setAdapter (new $ adapter (), $ options );
209- } else {
210- throw new \InvalidArgumentException (sprintf ('Could not resolve adapter type "%s" to a service or class implementing AdapterInterface ' , $ adapter ));
211- }
212- }
213-
214- /**
215- * @param AdapterInterface $adapter
216- * @param array|null $options
217- * @return DataTable
230+ * @return string
218231 */
219- public function setAdapter ( AdapterInterface $ adapter , array $ options = null ): self
232+ public function getTranslationDomain ( ): string
220233 {
221- if (null !== $ options ) {
222- $ adapter ->configure ($ options );
223- }
224- $ this ->adapter = $ adapter ;
225-
226- return $ this ;
234+ return $ this ->translationDomain ;
227235 }
228236
229237 /**
@@ -321,23 +329,6 @@ protected function getResultSet(): ResultSetInterface
321329 return $ this ->adapter ->getData ($ this ->state );
322330 }
323331
324- /**
325- * @return array
326- */
327- public function getSettings (): array
328- {
329- return $ this ->settings ;
330- }
331-
332- /**
333- * @param $name
334- * @return mixed|null
335- */
336- public function getSetting ($ name )
337- {
338- return $ this ->settings [$ name ] ?? null ;
339- }
340-
341332 /**
342333 * @return callable|null
343334 */
@@ -364,27 +355,38 @@ public function getOption($name)
364355 }
365356
366357 /**
367- * @param int|string|AbstractColumn $column
368- * @param string $direction
369- * @return self
358+ * @param AdapterInterface $adapter
359+ * @param array|null $options
360+ * @return DataTable
370361 */
371- public function addOrderBy ( $ column , string $ direction = self :: SORT_ASCENDING )
362+ public function setAdapter ( AdapterInterface $ adapter , array $ options = null ): self
372363 {
373- if (! $ column instanceof AbstractColumn ) {
374- $ column = is_int ( $ column ) ? $ this -> getColumn ( $ column ) : $ this -> getColumnByName (( string ) $ column );
364+ if (null !== $ options ) {
365+ $ adapter -> configure ( $ options );
375366 }
376- $ this ->options ['order ' ][] = [$ column ->getIndex (), $ direction ];
367+ $ this ->adapter = $ adapter ;
368+
369+ return $ this ;
370+ }
371+
372+ /**
373+ * @param bool $languageFromCDN
374+ * @return self
375+ */
376+ public function setLanguageFromCDN (bool $ languageFromCDN ): self
377+ {
378+ $ this ->languageFromCDN = $ languageFromCDN ;
377379
378380 return $ this ;
379381 }
380382
381383 /**
382- * @param ServiceLocator $adapterLocator
384+ * @param string $method
383385 * @return self
384386 */
385- public function setAdapterLocator ( ServiceLocator $ adapterLocator ): self
387+ public function setMethod ( string $ method ): self
386388 {
387- $ this ->adapterLocator = $ adapterLocator ;
389+ $ this ->method = $ method ;
388390
389391 return $ this ;
390392 }
@@ -409,37 +411,41 @@ public function setName(string $name): self
409411 if (empty ($ name )) {
410412 throw new \InvalidArgumentException ('DataTable name cannot be empty ' );
411413 }
412- $ this ->settings [ ' name ' ] = $ name ;
414+ $ this ->name = $ name ;
413415
414416 return $ this ;
415417 }
416418
417419 /**
418- * @param callable $formatter
419- * @return $this
420+ * @param string $template
421+ * @return self
420422 */
421- public function setTransformer ( callable $ formatter )
423+ public function setTemplate ( string $ template , array $ parameters = []): self
422424 {
423- $ this ->transformer = $ formatter ;
425+ $ this ->template = $ template ;
426+ $ this ->templateParams = $ parameters ;
424427
425428 return $ this ;
426429 }
427430
428431 /**
429- * @param OptionsResolver $resolver
432+ * @param string $translationDomain
433+ * @return self
434+ */
435+ public function setTranslationDomain (string $ translationDomain ): self
436+ {
437+ $ this ->translationDomain = $ translationDomain ;
438+
439+ return $ this ;
440+ }
441+
442+ /**
443+ * @param callable $formatter
430444 * @return $this
431445 */
432- protected function configureSettings ( OptionsResolver $ resolver )
446+ public function setTransformer ( callable $ formatter )
433447 {
434- $ resolver ->setDefaults (self ::DEFAULT_SETTINGS )
435- ->setAllowedTypes ('name ' , 'string ' )
436- ->setAllowedTypes ('method ' , 'string ' )
437- ->setAllowedTypes ('class_name ' , 'string ' )
438- ->setAllowedTypes ('column_filter ' , ['null ' , 'string ' ])
439- ->setAllowedTypes ('language_from_cdn ' , 'bool ' )
440- ->setAllowedTypes ('translation_domain ' , 'string ' )
441- ->setAllowedValues ('method ' , [Request::METHOD_GET , Request::METHOD_POST ])
442- ;
448+ $ this ->transformer = $ formatter ;
443449
444450 return $ this ;
445451 }
0 commit comments