22namespace LiveControl \EloquentDataTable ;
33
44use Exception ;
5+ use Illuminate \Database \Eloquent \Model ;
6+ use Illuminate \Database \Eloquent \Builder ;
7+ use Illuminate \Database \Query \Expression as raw ;
58use LiveControl \EloquentDataTable \VersionTransformers \Version110Transformer ;
69use LiveControl \EloquentDataTable \VersionTransformers \VersionTransformerContract ;
710
8- use Illuminate \Database \Query \Expression as raw ;
9- use Illuminate \Database \Eloquent \Builder ;
10- use Illuminate \Database \Eloquent \Model ;
1111
1212class DataTable
1313{
1414 private $ builder ;
1515 private $ columns ;
1616 private $ formatRowFunction ;
1717
18+ /**
19+ * @var VersionTransformerContract
20+ */
1821 protected static $ versionTransformer ;
1922
2023 private $ rawColumns ;
@@ -36,7 +39,7 @@ public function __construct($builder, $columns, $formatRowFunction = null)
3639 $ this ->setBuilder ($ builder );
3740 $ this ->setColumns ($ columns );
3841
39- if ( $ formatRowFunction !== null ) {
42+ if ($ formatRowFunction !== null ) {
4043 $ this ->setFormatRowFunction ($ formatRowFunction );
4144 }
4245 }
@@ -48,7 +51,7 @@ public function __construct($builder, $columns, $formatRowFunction = null)
4851 */
4952 public function setBuilder ($ builder )
5053 {
51- if ( ! ($ builder instanceof Builder || $ builder instanceof Model) ) {
54+ if ( ! ($ builder instanceof Builder || $ builder instanceof Model)) {
5255 throw new Exception ('$builder variable is not an instance of Builder or Model. ' );
5356 }
5457
@@ -87,6 +90,7 @@ public function setVersionTransformer(VersionTransformerContract $versionTransfo
8790 }
8891
8992 /**
93+ * Make the datatable response.
9094 * @return array
9195 * @throws Exception
9296 */
@@ -97,7 +101,7 @@ public function make()
97101 $ this ->rawColumns = $ this ->getRawColumns ($ this ->columns );
98102 $ this ->columnNames = $ this ->getColumnNames ();
99103
100- if ( static ::$ versionTransformer === null ) {
104+ if (static ::$ versionTransformer === null ) {
101105 static ::$ versionTransformer = new Version110Transformer ();
102106 }
103107
@@ -111,7 +115,6 @@ public function make()
111115
112116 $ this ->rows = $ this ->builder ->get ();
113117
114- // format rows
115118 $ rows = [];
116119 foreach ($ this ->rows as $ row ) {
117120 $ rows [] = $ this ->formatRow ($ row );
@@ -127,10 +130,14 @@ public function make()
127130 ];
128131 }
129132
133+ /**
134+ * @param $data
135+ * @return array|mixed
136+ */
130137 private function formatRow ($ data )
131138 {
132139 // if we have a custom format row function we trigger it instead of the default handling.
133- if ( $ this ->formatRowFunction !== null ) {
140+ if ($ this ->formatRowFunction !== null ) {
134141 $ function = $ this ->formatRowFunction ;
135142
136143 return call_user_func ($ function , $ data );
@@ -147,25 +154,31 @@ private function formatRow($data)
147154 return $ data ;
148155 }
149156
157+ /**
158+ * @param $data
159+ * @return mixed
160+ */
150161 private function formatRowIndexes ($ data )
151162 {
152- if ( isset ($ data ['id ' ]) ) {
163+ if (isset ($ data ['id ' ])) {
153164 $ data [static ::$ versionTransformer ->transform ('DT_RowId ' )] = $ data ['id ' ];
154165 }
155166 return $ data ;
156167 }
157168
169+ /**
170+ * @return array
171+ */
158172 private function getColumnNames ()
159173 {
160174 $ names = [];
161175 foreach ($ this ->columns as $ index => $ column ) {
162- if ( $ column instanceof ExpressionWithName ) {
176+ if ($ column instanceof ExpressionWithName) {
163177 $ names [] = $ column ->getName ();
164178 continue ;
165179 }
166180
167- if (is_string ($ column ) && strstr ($ column , '. ' ))
168- {
181+ if (is_string ($ column ) && strstr ($ column , '. ' )) {
169182 $ column = explode ('. ' , $ column );
170183 }
171184
@@ -174,6 +187,10 @@ private function getColumnNames()
174187 return $ names ;
175188 }
176189
190+ /**
191+ * @param $columns
192+ * @return array
193+ */
177194 private function getRawColumns ($ columns )
178195 {
179196 $ rawColumns = [];
@@ -183,14 +200,18 @@ private function getRawColumns($columns)
183200 return $ rawColumns ;
184201 }
185202
203+ /**
204+ * @param $column
205+ * @return raw|string
206+ */
186207 private function getRawColumnQuery ($ column )
187208 {
188- if ( $ column instanceof ExpressionWithName ) {
209+ if ($ column instanceof ExpressionWithName) {
189210 return $ column ->getExpression ();
190211 }
191212
192- if ( is_array ($ column ) ) {
193- if ( $ this ->getDatabaseDriver () == 'sqlite ' ) {
213+ if (is_array ($ column )) {
214+ if ($ this ->getDatabaseDriver () == 'sqlite ' ) {
194215 return '( ' . implode (' || " " || ' , $ this ->getRawColumns ($ column )) . ') ' ;
195216 }
196217 return 'CONCAT( ' . implode (', " ", ' , $ this ->getRawColumns ($ column )) . ') ' ;
@@ -199,27 +220,38 @@ private function getRawColumnQuery($column)
199220 return Model::resolveConnection ()->getQueryGrammar ()->wrap ($ column );
200221 }
201222
223+ /**
224+ * @return string
225+ */
202226 private function getDatabaseDriver ()
203227 {
204228 return Model::resolveConnection ()->getDriverName ();
205229 }
206230
231+ /**
232+ *
233+ */
207234 private function addSelect ()
208235 {
209236 $ rawSelect = [];
210237 foreach ($ this ->columns as $ index => $ column ) {
211- if ( isset ($ this ->rawColumns [$ index ]) ) {
238+ if (isset ($ this ->rawColumns [$ index ])) {
212239 $ rawSelect [] = $ this ->rawColumns [$ index ] . ' as ' . Model::resolveConnection ()->getQueryGrammar ()->wrap ($ this ->columnNames [$ index ]);
213240 }
214241 }
215242 $ this ->builder = $ this ->builder ->select (new raw (implode (', ' , $ rawSelect )));
216243 }
217244
245+ /**
246+ * @param $array
247+ * @param bool|false $inForeach
248+ * @return array|string
249+ */
218250 private function arrayToCamelcase ($ array , $ inForeach = false )
219251 {
220252 $ result = [];
221253 foreach ($ array as $ value ) {
222- if ( is_array ($ value ) ) {
254+ if (is_array ($ value )) {
223255 $ result += $ this ->arrayToCamelcase ($ value , true );
224256 }
225257 $ value = explode ('. ' , $ value );
@@ -230,45 +262,63 @@ private function arrayToCamelcase($array, $inForeach = false)
230262 return (! $ inForeach ? camel_case (implode ('_ ' , $ result )) : $ result );
231263 }
232264
265+ /**
266+ * Add the filters based on the search value given.
267+ * @return $this
268+ */
233269 private function addFilters ()
234270 {
235271 $ search = static ::$ versionTransformer ->getSearchValue ();
236- if ( $ search != '' ) {
272+ if ($ search != '' ) {
237273 $ this ->addAllFilter ($ search );
238274 }
239275 $ this ->addColumnFilters ();
240276 return $ this ;
241277 }
242278
279+ /**
280+ * Searches in all the columns.
281+ * @param $search
282+ */
243283 private function addAllFilter ($ search )
244284 {
245285 $ this ->builder = $ this ->builder ->where (
246286 function ($ query ) use ($ search ) {
247- foreach ($ this ->columnNames as $ column ) {
248- $ query ->orWhere ($ column , 'like ' , '% ' . $ search . '% ' );
287+ foreach ($ this ->columns as $ column ) {
288+ $ query ->orWhere (
289+ new raw ($ this ->getRawColumnQuery ($ column )),
290+ 'like ' ,
291+ '% ' . $ search . '% '
292+ );
249293 }
250294 }
251295 );
252296 }
253297
298+ /**
299+ * Add column specific filters.
300+ */
254301 private function addColumnFilters ()
255302 {
256- foreach ($ this ->columnNames as $ i => $ column ) {
257- if ( static ::$ versionTransformer ->isColumnSearched ($ i ) ) {
303+ foreach ($ this ->columns as $ i => $ column ) {
304+ if (static ::$ versionTransformer ->isColumnSearched ($ i )) {
258305 $ this ->builder ->where (
259- $ column ,
306+ new raw ( $ this -> getRawColumnQuery ( $ column)) ,
260307 'like ' ,
261308 '% ' . static ::$ versionTransformer ->getColumnSearchValue ($ i ) . '% '
262309 );
263310 }
264311 }
265312 }
266313
314+ /**
315+ * Depending on the sorted column this will add orderBy to the builder.
316+ */
267317 protected function addOrderBy ()
268318 {
269- if ( static ::$ versionTransformer ->isOrdered () ) {
319+ if (static ::$ versionTransformer ->isOrdered ()) {
270320 foreach (static ::$ versionTransformer ->getOrderedColumns () as $ index => $ direction ) {
271- if ( isset ($ this ->columnNames [$ index ]) ) {
321+ if (isset ($ this ->columnNames [$ index ])) {
272322 $ this ->builder ->orderBy (
273323 $ this ->columnNames [$ index ],
274324 $ direction
@@ -278,14 +328,17 @@ protected function addOrderBy()
278328 }
279329 }
280330
331+ /**
332+ * Adds the pagination limits to the builder
333+ */
281334 private function addLimits ()
282335 {
283- if ( isset ($ _POST [static ::$ versionTransformer ->transform (
336+ if (isset ($ _POST [static ::$ versionTransformer ->transform (
284337 'start '
285338 )]) && $ _POST [static ::$ versionTransformer ->transform ('length ' )] != '-1 '
286339 ) {
287340 $ this ->builder ->skip ((int )$ _POST [static ::$ versionTransformer ->transform ('start ' )])->take (
288- (int ) $ _POST [static ::$ versionTransformer ->transform ('length ' )]
341+ (int )$ _POST [static ::$ versionTransformer ->transform ('length ' )]
289342 );
290343 }
291344 }
0 commit comments