/** * Class construction. * * @param \Docolight\Support\Collection $collection * @param array $freeTextField */ public function __construct(Collection $collection, array $freeTextField = array()) { // This contains a list of field that can perform such a free text search $this->freeTextField = $freeTextField; $first = $collection->first(); if (!$first instanceof Fluent) { throw new Exception("The items in Collection must be an instance of \\Docolight\\Support\\Fluent."); } // Let's create a bare sort $sort = $this->getSort(); // Set a list of attributes that available to be sorted $sort->attributes = array_keys($first->attributes()); // Set the sort object $this->setSort($sort); $collectionClassName = get_class($collection); // Is there any filter from request? $filter = request($collectionClassName, array()); // In Firefox, backslash (\) from Namespace is converted to underscore (_) $additionalFilter = request(str_replace('\\', '_', $collectionClassName), array()); // Let's merge them together $filter = array_filter(array_merge($filter, $additionalFilter)); // Make a collection, filter it first if we got a filter request $this->collection = !empty($filter) ? $this->filterCollection($collection, $filter) : $collection; $sortRequest = request('sort'); // This is it, a sort request if ($sortRequest !== null) { // Get the field name we want to sort $fieldToSort = head(explode('.', $sortRequest)); // Determine whether request is a ascending sort or descending one $isDescending = trimtolower(last(explode('.', $sortRequest))) === 'desc'; // Assign new collection instance $this->collection = $this->collection->sortBy($fieldToSort, SORT_REGULAR, $isDescending); } }