Вход Регистрация
Файл: system/vendor/simba77/johncms-pagination/LengthAwarePaginator.php
Строк: 212
<?php

namespace IlluminatePagination;

use 
ArrayAccess;
use 
Countable;
use 
IlluminateContractsPaginationLengthAwarePaginator as LengthAwarePaginatorContract;
use 
IlluminateContractsSupportArrayable;
use 
IlluminateContractsSupportJsonable;
use 
IlluminateSupportCollection;
use 
IteratorAggregate;
use 
JohncmsSystemViewRender;
use 
JsonSerializable;

class 
LengthAwarePaginator extends AbstractPaginator implements ArrayableArrayAccessCountableIteratorAggregateJsonableJsonSerializableLengthAwarePaginatorContract
{
    
/**
     * The total number of items before slicing.
     *
     * @var int
     */
    
protected $total;

    
/**
     * The last available page.
     *
     * @var int
     */
    
protected $lastPage;

    
/**
     * Create a new paginator instance.
     *
     * @param mixed $items
     * @param int $total
     * @param int $perPage
     * @param int|null $currentPage
     * @param array $options (path, query, fragment, pageName)
     * @return void
     */
    
public function __construct($items$total$perPage$currentPage null, array $options = [])
    {
        
$this->options $options;

        foreach (
$options as $key => $value) {
            
$this->{$key} = $value;
        }

        
$this->total $total;
        
$this->perPage $perPage;
        
$this->lastPage max((int) ceil($total $perPage), 1);
        
$this->currentPage $this->setCurrentPage($currentPage$this->pageName);
        
$this->items $items instanceof Collection $items Collection::make($items);
    }

    
/**
     * Get the current page for the request.
     *
     * @param int $currentPage
     * @param string $pageName
     * @return int
     */
    
protected function setCurrentPage($currentPage$pageName)
    {
        
$currentPage $currentPage ?: static::resolveCurrentPage($pageName);
        if (
$currentPage $this->lastPage()) {
            
header('Location: ' $this->url($this->lastPage()));
            exit;
        }

        return 
$this->isValidPageNumber($currentPage) ? (int) $currentPage 1;
    }

    
/**
     * Render the paginator using the given view.
     *
     * @param string|null $view
     * @param array $data
     * @return IlluminateContractsSupportHtmlable
     */
    
public function links($view null$data = [])
    {
        return 
$this->render($view$data);
    }

    
/**
     * Render the paginator using the given view.
     *
     * @param string|null $view
     * @param array $data
     * @return IlluminateContractsSupportHtmlable
     */
    
public function render($view null$data = [])
    {
        
$view_engine di(Render::class);
        return 
$view_engine->render('system::app/model_paginator', ['elements' => $this->elements(), 'paginator' => $this]);
    }

    
/**
     * Get the array of elements to pass to the view.
     *
     * @return array
     */
    
protected function elements()
    {
        
$window UrlWindow::make($this);

        return 
array_filter(
            [
                
$window['first'],
                
is_array($window['slider']) ? '...' null,
                
$window['slider'],
                
is_array($window['last']) ? '...' null,
                
$window['last'],
            ]
        );
    }

    
/**
     * Get the total number of items being paginated.
     *
     * @return int
     */
    
public function total()
    {
        return 
$this->total;
    }

    
/**
     * Determine if there are more items in the data source.
     *
     * @return bool
     */
    
public function hasMorePages()
    {
        return 
$this->currentPage() < $this->lastPage();
    }

    
/**
     * Get the URL for the next page.
     *
     * @return string|null
     */
    
public function nextPageUrl()
    {
        if (
$this->lastPage() > $this->currentPage()) {
            return 
$this->url($this->currentPage() + 1);
        }
    }

    
/**
     * Get the last page.
     *
     * @return int
     */
    
public function lastPage()
    {
        return 
$this->lastPage;
    }

    
/**
     * Get the instance as an array.
     *
     * @return array
     */
    
public function toArray()
    {
        return [
            
'current_page'   => $this->currentPage(),
            
'data'           => $this->items->toArray(),
            
'first_page_url' => $this->url(1),
            
'from'           => $this->firstItem(),
            
'last_page'      => $this->lastPage(),
            
'last_page_url'  => $this->url($this->lastPage()),
            
'next_page_url'  => $this->nextPageUrl(),
            
'path'           => $this->path(),
            
'per_page'       => $this->perPage(),
            
'prev_page_url'  => $this->previousPageUrl(),
            
'to'             => $this->lastItem(),
            
'total'          => $this->total(),
        ];
    }

    
/**
     * Convert the object into something JSON serializable.
     *
     * @return array
     */
    
public function jsonSerialize()
    {
        return 
$this->toArray();
    }

    
/**
     * Convert the object to its JSON representation.
     *
     * @param int $options
     * @return string
     */
    
public function toJson($options 0)
    {
        return 
json_encode($this->jsonSerialize(), $options);
    }

    
/**
     * Получем коллекцию
     *
     * @return Collection
     */
    
public function getItems()
    {
        return 
$this->items;
    }
}
Онлайн: 1
Реклама