queryParameters->filter(); // collect sort options $sort = $this->queryParameters->sortFields(); // collect pagination based on the page $pagination = $this->filtersPagination($this->queryParameters->page()); // check if we need all accounts, regardless of pagination // This is necessary when the user wants to sort on specific params. $needsAll = $this->needsFullDataset('account', $sort); // params that were not recognised, may be my own custom stuff. $otherParams = $this->getOtherParams($this->queryParameters->unrecognisedParameters()); // start the query $query = $this->userGroup->accounts(); // add pagination to the query, limiting the results. if (!$needsAll) { $query = $this->addPagination($query, $pagination); } // add sort and filter parameters to the query. $query = $this->addSortParams($query, $sort); $query = $this->addFilterParams(Account::class, $query, $filters); // collect the result. $collection = $query->get(['accounts.*']); // enrich the collected data $enrichment = new AccountEnrichment(); $enrichment->setStart($otherParams['start'] ?? null); $enrichment->setEnd($otherParams['end'] ?? null); $collection = $enrichment->enrich($collection); // TODO add filters after the query, if there are filters that cannot be applied to the database but only // to the enriched results. // sort the data after the query, and return it right away. return $this->sortCollection($collection, $sort); } }