Overview

Namespaces

  • Charcoal
    • Loader
    • Model
      • Service
      • ServiceProvider
    • Source
      • Database
    • Validator

Classes

  • Charcoal\Loader\CollectionLoader
  • Charcoal\Loader\FileLoader
  • Charcoal\Model\AbstractMetadata
  • Charcoal\Model\AbstractModel
  • Charcoal\Model\Collection
  • Charcoal\Model\Model
  • Charcoal\Model\ModelMetadata
  • Charcoal\Model\ModelValidator
  • Charcoal\Model\Service\MetadataLoader
  • Charcoal\Model\Service\ModelBuilder
  • Charcoal\Model\Service\ModelLoader
  • Charcoal\Model\Service\ModelLoaderBuilder
  • Charcoal\Model\ServiceProvider\ModelServiceProvider
  • Charcoal\Source\AbstractSource
  • Charcoal\Source\Database\DatabaseFilter
  • Charcoal\Source\Database\DatabaseOrder
  • Charcoal\Source\Database\DatabasePagination
  • Charcoal\Source\DatabaseSource
  • Charcoal\Source\DatabaseSourceConfig
  • Charcoal\Source\Filter
  • Charcoal\Source\Order
  • Charcoal\Source\Pagination
  • Charcoal\Source\SourceConfig
  • Charcoal\Validator\AbstractValidator
  • Charcoal\Validator\ValidatorResult

Interfaces

  • Charcoal\Model\CollectionInterface
  • Charcoal\Model\DescribableInterface
  • Charcoal\Model\MetadataInterface
  • Charcoal\Model\ModelInterface
  • Charcoal\Source\DatabaseSourceInterface
  • Charcoal\Source\FilterInterface
  • Charcoal\Source\OrderInterface
  • Charcoal\Source\PaginationInterface
  • Charcoal\Source\SourceInterface
  • Charcoal\Source\StorableInterface
  • Charcoal\Validator\ValidatableInterface
  • Charcoal\Validator\ValidatorInterface

Traits

  • Charcoal\Model\DescribableTrait
  • Charcoal\Source\StorableTrait
  • Charcoal\Validator\ValidatableTrait
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Charcoal\Source;
  4: 
  5: use \InvalidArgumentException as InvalidArgumentException;
  6: 
  7: /**
  8:  * Implementation, as concrete class, of the PaginationInterface.
  9:  */
 10: class Pagination implements PaginationInterface
 11: {
 12:     const DEFAULT_PAGE = 0;
 13:     const DEFAULT_NUM_PER_PAGE = 0;
 14: 
 15:     /**
 16:      * @var integer $page
 17:      */
 18:     protected $page = self::DEFAULT_PAGE;
 19:     /**
 20:      * @var integer $numPerPage
 21:      */
 22:     protected $numPerPage = self::DEFAULT_NUM_PER_PAGE;
 23: 
 24:     /**
 25:      * @param array $data The pagination data (page, num_per_page).
 26:      * @return Pagination Chainable
 27:      */
 28:     public function setData(array $data)
 29:     {
 30:         if (isset($data['page'])) {
 31:             $this->setPage($data['page']);
 32:         }
 33:         if (isset($data['num_per_page'])) {
 34:             $this->setNumPerPage($data['num_per_page']);
 35:         }
 36:         return $this;
 37:     }
 38: 
 39:     /**
 40:      * @param integer $page The current page. Start at 0.
 41:      * @throws InvalidArgumentException If the parameter is not numeric or < 0.
 42:      * @return Pagination (Chainable)
 43:      */
 44:     public function setPage($page)
 45:     {
 46:         if (!is_numeric($page)) {
 47:             throw new InvalidArgumentException(
 48:                 'Page number needs to be numeric.'
 49:             );
 50:         }
 51:         $page = (int)$page;
 52:         if ($page < 0) {
 53:             throw new InvalidArgumentException(
 54:                 'Page number needs to be >= 0.'
 55:             );
 56:         }
 57:         $this->page = $page;
 58:         return $this;
 59:     }
 60: 
 61:     /**
 62:      * @return integer
 63:      */
 64:     public function page()
 65:     {
 66:         return $this->page;
 67:     }
 68: 
 69:     /**
 70:      * @param integer $num The number of results to retrieve, per page.
 71:      * @throws InvalidArgumentException If the parameter is not numeric or < 0.
 72:      * @return Pagination (Chainable)
 73:      */
 74:     public function setNumPerPage($num)
 75:     {
 76:         if (!is_numeric($num)) {
 77:             throw new InvalidArgumentException(
 78:                 'Num-per-page needs to be numeric.'
 79:             );
 80:         }
 81:         $num = (int)$num;
 82:         if ($num < 0) {
 83:             throw new InvalidArgumentException(
 84:                 'Num-per-page needs to be >= 0.'
 85:             );
 86:         }
 87: 
 88:         $this->numPerPage = $num;
 89:         return $this;
 90:     }
 91: 
 92:     /**
 93:      * @return integer
 94:      */
 95:     public function numPerPage()
 96:     {
 97:         return $this->numPerPage;
 98:     }
 99: 
100:     /**
101:      * @return integer
102:      */
103:     public function first()
104:     {
105:         $page = $this->page();
106:         $numPerPage = $this->numPerPage();
107:         return max(0, (($page-1)*$numPerPage));
108:     }
109: 
110:     /**
111:      * Can be greater than the actual number of items in Storage
112:      * @return integer
113:      */
114:     public function last()
115:     {
116:         $first = $this->first();
117:         $numPerPage = $this->numPerPage();
118:         return ($first + $numPerPage);
119:     }
120: }
121: 
API documentation generated by ApiGen