1414
1515use Omines \DataTablesBundle \DataTableState ;
1616use Symfony \Component \PropertyAccess \PropertyAccess ;
17+ use Symfony \Component \PropertyAccess \PropertyAccessor ;
1718
1819/**
1920 * ArrayAdapter.
@@ -25,12 +26,16 @@ class ArrayAdapter implements AdapterInterface
2526 /** @var array */
2627 private $ data = [];
2728
29+ /** @var PropertyAccessor */
30+ private $ accessor ;
31+
2832 /**
2933 * {@inheritdoc}
3034 */
3135 public function configure (array $ options )
3236 {
3337 $ this ->data = $ options ;
38+ $ this ->accessor = PropertyAccess::createPropertyAccessor ();
3439 }
3540
3641 /**
@@ -65,26 +70,37 @@ public function getData(DataTableState $state): ResultSetInterface
6570 protected function processData (DataTableState $ state , array $ data , array $ map )
6671 {
6772 $ transformer = $ state ->getDataTable ()->getTransformer ();
68- $ search = $ state ->getGlobalSearch () ?: null ;
69- $ accessor = PropertyAccess::createPropertyAccessor ();
73+ $ search = $ state ->getGlobalSearch () ?: '' ;
7074 foreach ($ data as $ result ) {
71- $ row = [];
72- $ match = (null === $ search );
73- foreach ($ state ->getDataTable ()->getColumns () as $ column ) {
74- $ value = (!empty ($ propertyPath = $ map [$ column ->getName ()]) && $ accessor ->isReadable ($ result , $ propertyPath )) ? $ accessor ->getValue ($ result , $ propertyPath ) : null ;
75- $ value = $ column ->transform ($ value , $ result );
76- if (!$ match ) {
77- $ match = (false !== mb_stripos ($ value , $ search ));
75+ if ($ row = $ this ->processRow ($ state , $ result , $ map , $ search )) {
76+ if ($ transformer ) {
77+ $ row = call_user_func ($ transformer , $ row , $ result );
7878 }
79- $ row [ $ column -> getName ()] = $ column -> transform ( $ value , $ result ) ;
79+ yield $ row ;
8080 }
81+ }
82+ }
83+
84+ /**
85+ * @param DataTableState $state
86+ * @param array $result
87+ * @param array $map
88+ * @param string $search
89+ * @return array|null
90+ */
91+ protected function processRow (DataTableState $ state , array $ result , array $ map , string $ search )
92+ {
93+ $ row = [];
94+ $ match = empty ($ search );
95+ foreach ($ state ->getDataTable ()->getColumns () as $ column ) {
96+ $ value = (!empty ($ propertyPath = $ map [$ column ->getName ()]) && $ this ->accessor ->isReadable ($ result , $ propertyPath )) ? $ this ->accessor ->getValue ($ result , $ propertyPath ) : null ;
97+ $ value = $ column ->transform ($ value , $ result );
8198 if (!$ match ) {
82- continue ;
83- }
84- if ($ transformer ) {
85- $ row = call_user_func ($ transformer , $ row , $ result );
99+ $ match = (false !== mb_stripos ($ value , $ search ));
86100 }
87- yield $ row ;
101+ $ row[ $ column -> getName ()] = $ column -> transform ( $ value , $ result ) ;
88102 }
103+
104+ return $ match ? $ row : null ;
89105 }
90106}
0 commit comments