Source of file ArraySource.php
Size: 4,997 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Grid/Source/ArraySource.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Grid\Source; use Bluz\Grid; /** * Array Source Adapter for Grid package * * @package Bluz\Grid * @author Anton Shevchuk */ class ArraySource extends AbstractSource { /** * Set array source * * @param array $source * * @return self * @throws Grid\GridException */ public function setSource($source) { if (!is_array($source) && !($source instanceof \ArrayAccess)) { throw new Grid\GridException('Source of `ArraySource` should be array or implement ArrayAccess interface'); } $this->source = $source; return $this; } /** * Process * * @param array[] $settings * * @return \Bluz\Grid\Data */ public function process(array $settings = []) { $data = $this->source; // process filters if (!empty($settings['filters'])) { $data = $this->applyFilters($data, $settings['filters']); } // process orders if (!empty($settings['orders'])) { $data = $this->applyOrders($data, $settings['orders']); } $total = count($data); // process pages $data = array_slice($data, $settings['limit'] * ($settings['page'] - 1), $settings['limit']); $gridData = new Grid\Data($data); $gridData->setTotal($total); return $gridData; } /** * Apply filters to array * * @param array $data * @param array $settings * * @return array */ private function applyFilters(array $data, array $settings): array { return array_filter( $data, function ($row) use ($settings) { foreach ($settings as $column => $filters) { foreach ($filters as $filter => $value) { // switch statement for filter switch ($filter) { case Grid\Grid::FILTER_EQ: if ($row[$column] != $value) { return false; } break; case Grid\Grid::FILTER_NE: if ($row[$column] == $value) { return false; } break; case Grid\Grid::FILTER_GT: if ($row[$column] <= $value) { return false; } break; case Grid\Grid::FILTER_GE: if ($row[$column] < $value) { return false; } break; case Grid\Grid::FILTER_LT: if ($row[$column] >= $value) { return false; } break; case Grid\Grid::FILTER_LE: if ($row[$column] > $value) { return false; } break; case Grid\Grid::FILTER_LIKE: if (!preg_match('/' . $value . '/', $row[$column])) { return false; } break; } } } return true; } ); } /** * Apply order to array * * @param array $data * @param array $settings * * @return array */ private function applyOrders(array $data, array $settings): array { // Create empty column stack $orders = []; foreach ($settings as $column => $order) { $orders[$column] = []; } // Obtain a list of columns foreach ($data as $key => $row) { foreach ($settings as $column => $order) { $orders[$column][$key] = $row[$column]; } } // Prepare array of arguments $funcArgs = []; foreach ($settings as $column => $order) { $funcArgs[] = $orders[$column]; $funcArgs[] = ($order === Grid\Grid::ORDER_ASC) ? SORT_ASC : SORT_DESC; } $funcArgs[] = &$data; // Sort the data with volume descending, edition ascending // Add $data as the last parameter, to sort by the common key array_multisort(...$funcArgs); return $data; } } |