public function paginate($object, array $settings = [])
{
if ($object instanceof QueryInterface) {
$query = $object;
$object = $query->repository();
}
$alias = $object->alias();
$options = $this->mergeOptions($alias, $settings);
$options = $this->validateSort($object, $options);
$options = $this->checkLimit($options);
$options += ['page' => 1, 'scope' => null];
$options['page'] = (int) $options['page'] < 1 ? 1 : (int) $options['page'];
list($finder, $options) = $this->_extractFinder($options);
/* @var \Cake\Datasource\RepositoryInterface $object */
if (empty($query)) {
$query = $object->find($finder, $options);
} else {
$query->applyOptions($options);
}
$results = $query->all();
$numResults = count($results);
$count = $numResults ? $query->count() : 0;
$defaults = $this->getDefaults($alias, $settings);
unset($defaults[0]);
$page = $options['page'];
$limit = $options['limit'];
$pageCount = (int) ceil($count / $limit);
$requestedPage = $page;
$page = max(min($page, $pageCount), 1);
$request = $this->_registry->getController()->request;
$order = (array) $options['order'];
$sortDefault = $directionDefault = false;
if (!empty($defaults['order']) && count($defaults['order']) == 1) {
$sortDefault = key($defaults['order']);
$directionDefault = current($defaults['order']);
}
$paging = ['finder' => $finder, 'page' => $page, 'current' => $numResults, 'count' => $count, 'perPage' => $limit, 'prevPage' => $page > 1, 'nextPage' => $count > $page * $limit, 'pageCount' => $pageCount, 'sort' => key($order), 'direction' => current($order), 'limit' => $defaults['limit'] != $limit ? $limit : null, 'sortDefault' => $sortDefault, 'directionDefault' => $directionDefault, 'scope' => $options['scope']];
if (!isset($request['paging'])) {
$request['paging'] = [];
}
$request['paging'] = [$alias => $paging] + (array) $request['paging'];
if ($requestedPage > $page) {
throw new NotFoundException();
}
return $results;
}