Overview

Namespaces

  • None
  • Yajra
    • Datatables
      • Contracts
      • Engines
      • Facades
      • Generators
      • Html
      • Processors
      • Services
      • Transformers

Classes

  • Yajra\Datatables\Datatables
  • Yajra\Datatables\DatatablesServiceProvider
  • Yajra\Datatables\Engines\BaseEngine
  • Yajra\Datatables\Engines\CollectionEngine
  • Yajra\Datatables\Engines\EloquentEngine
  • Yajra\Datatables\Engines\QueryBuilderEngine
  • Yajra\Datatables\Facades\Datatables
  • Yajra\Datatables\Generators\DataTablesMakeCommand
  • Yajra\Datatables\Generators\DataTablesScopeCommand
  • Yajra\Datatables\Helper
  • Yajra\Datatables\Html\Builder
  • Yajra\Datatables\Html\Column
  • Yajra\Datatables\Html\Parameters
  • Yajra\Datatables\Processors\DataProcessor
  • Yajra\Datatables\Processors\RowProcessor
  • Yajra\Datatables\Request
  • Yajra\Datatables\Services\DataTable
  • Yajra\Datatables\Transformers\DataTransformer

Interfaces

  • Yajra\Datatables\Contracts\DataTableButtonsContract
  • Yajra\Datatables\Contracts\DataTableContract
  • Yajra\Datatables\Contracts\DataTableEngineContract
  • Yajra\Datatables\Contracts\DataTableScopeContract

Functions

  • config_path
  • public_path
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Yajra\Datatables\Processors;
  4: 
  5: use Illuminate\Support\Arr;
  6: use Illuminate\Support\Facades\Config;
  7: use Yajra\Datatables\Helper;
  8: 
  9: /**
 10:  * Class DataProcessor.
 11:  *
 12:  * @package Yajra\Datatables
 13:  * @author  Arjay Angeles <aqangeles@gmail.com>
 14:  */
 15: class DataProcessor
 16: {
 17:     /**
 18:      * @var int
 19:      */
 20:     protected $start;
 21: 
 22:     /**
 23:      * Columns to escape value.
 24:      *
 25:      * @var array
 26:      */
 27:     protected $escapeColumns = [];
 28: 
 29:     /**
 30:      * Processed data output
 31:      *
 32:      * @var array
 33:      */
 34:     protected $output = [];
 35: 
 36:     /**
 37:      * @var array
 38:      */
 39:     protected $appendColumns = [];
 40: 
 41:     /**
 42:      * @var array
 43:      */
 44:     protected $editColumns = [];
 45: 
 46:     /**
 47:      * @var array
 48:      */
 49:     protected $excessColumns = [];
 50: 
 51:     /**
 52:      * @var mixed
 53:      */
 54:     protected $results;
 55: 
 56:     /**
 57:      * @var array
 58:      */
 59:     protected $templates;
 60: 
 61:     /**
 62:      * @var bool
 63:      */
 64:     protected $includeIndex;
 65: 
 66:     /**
 67:      * @param mixed $results
 68:      * @param array $columnDef
 69:      * @param array $templates
 70:      * @param int $start
 71:      */
 72:     public function __construct($results, array $columnDef, array $templates, $start)
 73:     {
 74:         $this->results       = $results;
 75:         $this->appendColumns = $columnDef['append'];
 76:         $this->editColumns   = $columnDef['edit'];
 77:         $this->excessColumns = $columnDef['excess'];
 78:         $this->escapeColumns = $columnDef['escape'];
 79:         $this->includeIndex  = $columnDef['index'];
 80:         $this->templates     = $templates;
 81:         $this->start         = $start;
 82:     }
 83: 
 84:     /**
 85:      * Process data to output on browser
 86:      *
 87:      * @param bool $object
 88:      * @return array
 89:      */
 90:     public function process($object = false)
 91:     {
 92:         $this->output = [];
 93:         $indexColumn  = Config::get('datatables.index_column', 'DT_Row_Index');
 94: 
 95:         foreach ($this->results as $row) {
 96:             $data  = Helper::convertToArray($row);
 97:             $value = $this->addColumns($data, $row);
 98:             $value = $this->editColumns($value, $row);
 99:             $value = $this->setupRowVariables($value, $row);
100:             $value = $this->removeExcessColumns($value);
101: 
102:             if ($this->includeIndex) {
103:                 $value[$indexColumn] = ++$this->start;
104:             }
105: 
106:             $this->output[] = $object ? $value : $this->flatten($value);
107:         }
108: 
109:         return $this->escapeColumns($this->output);
110:     }
111: 
112:     /**
113:      * Process add columns.
114:      *
115:      * @param mixed $data
116:      * @param mixed $row
117:      * @return array
118:      */
119:     protected function addColumns($data, $row)
120:     {
121:         foreach ($this->appendColumns as $key => $value) {
122:             $value['content'] = Helper::compileContent($value['content'], $data, $row);
123:             $data             = Helper::includeInArray($value, $data);
124:         }
125: 
126:         return $data;
127:     }
128: 
129:     /**
130:      * Process edit columns.
131:      *
132:      * @param mixed $data
133:      * @param mixed $row
134:      * @return array
135:      */
136:     protected function editColumns($data, $row)
137:     {
138:         foreach ($this->editColumns as $key => $value) {
139:             $value['content'] = Helper::compileContent($value['content'], $data, $row);
140:             Arr::set($data, $value['name'], $value['content']);
141:         }
142: 
143:         return $data;
144:     }
145: 
146:     /**
147:      * Setup additional DT row variables.
148:      *
149:      * @param mixed $data
150:      * @param mixed $row
151:      * @return array
152:      */
153:     protected function setupRowVariables($data, $row)
154:     {
155:         $processor = new RowProcessor($data, $row);
156: 
157:         return $processor
158:             ->rowValue('DT_RowId', $this->templates['DT_RowId'])
159:             ->rowValue('DT_RowClass', $this->templates['DT_RowClass'])
160:             ->rowData('DT_RowData', $this->templates['DT_RowData'])
161:             ->rowData('DT_RowAttr', $this->templates['DT_RowAttr'])
162:             ->getData();
163:     }
164: 
165:     /**
166:      * Remove declared hidden columns.
167:      *
168:      * @param array $data
169:      * @return array
170:      */
171:     protected function removeExcessColumns(array $data)
172:     {
173:         foreach ($this->excessColumns as $value) {
174:             unset($data[$value]);
175:         }
176: 
177:         return $data;
178:     }
179: 
180:     /**
181:      * Flatten array with exceptions.
182:      *
183:      * @param array $array
184:      * @return array
185:      */
186:     public function flatten(array $array)
187:     {
188:         $return     = [];
189:         $exceptions = ['DT_RowId', 'DT_RowClass', 'DT_RowData', 'DT_RowAttr'];
190: 
191:         foreach ($array as $key => $value) {
192:             if (in_array($key, $exceptions)) {
193:                 $return[$key] = $value;
194:             } else {
195:                 $return[] = $value;
196:             }
197:         }
198: 
199:         return $return;
200:     }
201: 
202:     /**
203:      * Escape column values as declared.
204:      *
205:      * @param array $output
206:      * @return array
207:      */
208:     protected function escapeColumns(array $output)
209:     {
210:         return array_map(function ($row) {
211:             if ($this->escapeColumns == '*') {
212:                 $row = $this->escapeRow($row, $this->escapeColumns);
213:             } else {
214:                 foreach ($this->escapeColumns as $key) {
215:                     if (array_get($row, $key)) {
216:                         array_set($row, $key, e(array_get($row, $key)));
217:                     }
218:                 }
219:             }
220: 
221:             return $row;
222:         }, $output);
223:     }
224: 
225:     /**
226:      * Escape all values of row.
227:      *
228:      * @param array $row
229:      * @param string|array $escapeColumns
230:      * @return array
231:      */
232:     protected function escapeRow(array $row, $escapeColumns)
233:     {
234:         foreach ($row as $key => $value) {
235:             if (is_array($value)) {
236:                 $row[$key] = $this->escapeRow($value, $escapeColumns);
237:             } else {
238:                 $row[$key] = e($value);
239:             }
240:         }
241: 
242:         return $row;
243:     }
244: }
245: 
API documentation generated by ApiGen