PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/www/libs/Zend/Paginator/Paginator.php

https://bitbucket.org/Ppito/kawaiviewmodel2
PHP | 970 lines | 474 code | 131 blank | 365 comment | 65 complexity | a0b3f44154b05fe3123261e3c41870e0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Paginator
  9. */
  10. namespace Zend\Paginator;
  11. use ArrayIterator;
  12. use Countable;
  13. use IteratorAggregate;
  14. use Traversable;
  15. use Zend\Cache\Storage\IteratorInterface as CacheIterator;
  16. use Zend\Cache\Storage\StorageInterface as CacheStorage;
  17. use Zend\Db\ResultSet\AbstractResultSet;
  18. use Zend\Filter\FilterInterface;
  19. use Zend\Json\Json;
  20. use Zend\Paginator\Adapter\AdapterInterface;
  21. use Zend\Paginator\ScrollingStyle\ScrollingStyleInterface;
  22. use Zend\Stdlib\ArrayUtils;
  23. use Zend\View;
  24. /**
  25. * @category Zend
  26. * @package Zend_Paginator
  27. */
  28. class Paginator implements Countable, IteratorAggregate
  29. {
  30. /**
  31. * The cache tag prefix used to namespace Paginator results in the cache
  32. *
  33. */
  34. const CACHE_TAG_PREFIX = 'Zend_Paginator_';
  35. /**
  36. * Adapter plugin manager
  37. *
  38. * @var AdapterPluginManager
  39. */
  40. protected static $adapters = null;
  41. /**
  42. * Configuration file
  43. *
  44. * @var array|null
  45. */
  46. protected static $config = null;
  47. /**
  48. * Default scrolling style
  49. *
  50. * @var string
  51. */
  52. protected static $defaultScrollingStyle = 'Sliding';
  53. /**
  54. * Default item count per page
  55. *
  56. * @var int
  57. */
  58. protected static $defaultItemCountPerPage = 10;
  59. /**
  60. * Scrolling style plugin manager
  61. *
  62. * @var ScrollingStylePluginManager
  63. */
  64. protected static $scrollingStyles = null;
  65. /**
  66. * Cache object
  67. *
  68. * @var CacheStorage
  69. */
  70. protected static $cache;
  71. /**
  72. * Enable or disable the cache by Zend\Paginator\Paginator instance
  73. *
  74. * @var bool
  75. */
  76. protected $cacheEnabled = true;
  77. /**
  78. * Adapter
  79. *
  80. * @var AdapterInterface
  81. */
  82. protected $adapter = null;
  83. /**
  84. * Number of items in the current page
  85. *
  86. * @var integer
  87. */
  88. protected $currentItemCount = null;
  89. /**
  90. * Current page items
  91. *
  92. * @var Traversable
  93. */
  94. protected $currentItems = null;
  95. /**
  96. * Current page number (starting from 1)
  97. *
  98. * @var integer
  99. */
  100. protected $currentPageNumber = 1;
  101. /**
  102. * Result filter
  103. *
  104. * @var FilterInterface
  105. */
  106. protected $filter = null;
  107. /**
  108. * Number of items per page
  109. *
  110. * @var integer
  111. */
  112. protected $itemCountPerPage = null;
  113. /**
  114. * Number of pages
  115. *
  116. * @var integer
  117. */
  118. protected $pageCount = null;
  119. /**
  120. * Number of local pages (i.e., the number of discrete page numbers
  121. * that will be displayed, including the current page number)
  122. *
  123. * @var integer
  124. */
  125. protected $pageRange = 10;
  126. /**
  127. * Pages
  128. *
  129. * @var array
  130. */
  131. protected $pages = null;
  132. /**
  133. * View instance used for self rendering
  134. *
  135. * @var \Zend\View\Renderer\RendererInterface
  136. */
  137. protected $view = null;
  138. /**
  139. * Set a global config
  140. *
  141. * @param array|\Traversable $config
  142. * @throws Exception\InvalidArgumentException
  143. */
  144. public static function setGlobalConfig($config)
  145. {
  146. if ($config instanceof Traversable) {
  147. $config = ArrayUtils::iteratorToArray($config);
  148. }
  149. if (!is_array($config)) {
  150. throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable');
  151. }
  152. static::$config = $config;
  153. if (isset($config['scrolling_style_plugins'])
  154. && null !== ($adapters = $config['scrolling_style_plugins'])
  155. ) {
  156. static::setScrollingStylePluginManager($adapters);
  157. }
  158. $scrollingStyle = isset($config['scrolling_style']) ? $config['scrolling_style'] : null;
  159. if ($scrollingStyle != null) {
  160. static::setDefaultScrollingStyle($scrollingStyle);
  161. }
  162. }
  163. /**
  164. * Returns the default scrolling style.
  165. *
  166. * @return string
  167. */
  168. public static function getDefaultScrollingStyle()
  169. {
  170. return static::$defaultScrollingStyle;
  171. }
  172. /**
  173. * Get the default item count per page
  174. *
  175. * @return int
  176. */
  177. public static function getDefaultItemCountPerPage()
  178. {
  179. return static::$defaultItemCountPerPage;
  180. }
  181. /**
  182. * Set the default item count per page
  183. *
  184. * @param int $count
  185. */
  186. public static function setDefaultItemCountPerPage($count)
  187. {
  188. static::$defaultItemCountPerPage = (int) $count;
  189. }
  190. /**
  191. * Sets a cache object
  192. *
  193. * @param CacheStorage $cache
  194. */
  195. public static function setCache(CacheStorage $cache)
  196. {
  197. static::$cache = $cache;
  198. }
  199. /**
  200. * Sets the default scrolling style.
  201. *
  202. * @param string $scrollingStyle
  203. */
  204. public static function setDefaultScrollingStyle($scrollingStyle = 'Sliding')
  205. {
  206. static::$defaultScrollingStyle = $scrollingStyle;
  207. }
  208. public static function setScrollingStylePluginManager($scrollingAdapters)
  209. {
  210. if (is_string($scrollingAdapters)) {
  211. if (!class_exists($scrollingAdapters)) {
  212. throw new Exception\InvalidArgumentException(sprintf(
  213. 'Unable to locate scrolling style plugin manager with class "%s"; class not found',
  214. $scrollingAdapters
  215. ));
  216. }
  217. $scrollingAdapters = new $scrollingAdapters();
  218. }
  219. if (!$scrollingAdapters instanceof ScrollingStylePluginManager) {
  220. throw new Exception\InvalidArgumentException(sprintf(
  221. 'Pagination scrolling-style manager must extend ScrollingStylePluginManager; received "%s"',
  222. (is_object($scrollingAdapters) ? get_class($scrollingAdapters) : gettype($scrollingAdapters))
  223. ));
  224. }
  225. static::$scrollingStyles = $scrollingAdapters;
  226. }
  227. /**
  228. * Returns the scrolling style manager. If it doesn't exist it's
  229. * created.
  230. *
  231. * @return ScrollingStylePluginManager
  232. */
  233. public static function getScrollingStylePluginManager()
  234. {
  235. if (static::$scrollingStyles === null) {
  236. static::$scrollingStyles = new ScrollingStylePluginManager();
  237. }
  238. return static::$scrollingStyles;
  239. }
  240. /**
  241. * Constructor.
  242. *
  243. * @param AdapterInterface|AdapterAggregateInterface $adapter
  244. * @throws Exception\InvalidArgumentException
  245. */
  246. public function __construct($adapter)
  247. {
  248. if ($adapter instanceof AdapterInterface) {
  249. $this->adapter = $adapter;
  250. } elseif ($adapter instanceof AdapterAggregateInterface) {
  251. $this->adapter = $adapter->getPaginatorAdapter();
  252. } else {
  253. throw new Exception\InvalidArgumentException(
  254. 'Zend_Paginator only accepts instances of the type ' .
  255. 'Zend\Paginator\Adapter\AdapterInterface or Zend\Paginator\AdapterAggregateInterface.'
  256. );
  257. }
  258. $config = static::$config;
  259. if (!empty($config)) {
  260. $setupMethods = array('ItemCountPerPage', 'PageRange');
  261. foreach ($setupMethods as $setupMethod) {
  262. $key = strtolower($setupMethod);
  263. $value = isset($config[$key]) ? $config[$key] : null;
  264. if ($value != null) {
  265. $setupMethod = 'set' . $setupMethod;
  266. $this->$setupMethod($value);
  267. }
  268. }
  269. }
  270. }
  271. /**
  272. * Serializes the object as a string. Proxies to {@link render()}.
  273. *
  274. * @return string
  275. */
  276. public function __toString()
  277. {
  278. try {
  279. $return = $this->render();
  280. return $return;
  281. } catch (\Exception $e) {
  282. trigger_error($e->getMessage(), E_USER_WARNING);
  283. }
  284. return '';
  285. }
  286. /**
  287. * Enables/Disables the cache for this instance
  288. *
  289. * @param bool $enable
  290. * @return Paginator
  291. */
  292. public function setCacheEnabled($enable)
  293. {
  294. $this->cacheEnabled = (bool) $enable;
  295. return $this;
  296. }
  297. /**
  298. * Returns the number of pages.
  299. *
  300. * @return integer
  301. */
  302. public function count()
  303. {
  304. if (!$this->pageCount) {
  305. $this->pageCount = $this->_calculatePageCount();
  306. }
  307. return $this->pageCount;
  308. }
  309. /**
  310. * Returns the total number of items available.
  311. *
  312. * @return integer
  313. */
  314. public function getTotalItemCount()
  315. {
  316. return count($this->getAdapter());
  317. }
  318. /**
  319. * Clear the page item cache.
  320. *
  321. * @param int $pageNumber
  322. * @return Paginator
  323. */
  324. public function clearPageItemCache($pageNumber = null)
  325. {
  326. if (!$this->cacheEnabled()) {
  327. return $this;
  328. }
  329. if (null === $pageNumber) {
  330. $prefixLength = strlen(self::CACHE_TAG_PREFIX);
  331. $cacheIterator = static::$cache->getIterator();
  332. $cacheIterator->setMode(CacheIterator::CURRENT_AS_KEY);
  333. foreach ($cacheIterator as $key) {
  334. $tags = static::$cache->getTags($key);
  335. if ($tags && in_array($this->_getCacheInternalId(), $tags)) {
  336. if (substr($key, 0, $prefixLength) == self::CACHE_TAG_PREFIX) {
  337. static::$cache->removeItem($this->_getCacheId((int)substr($key, $prefixLength)));
  338. }
  339. }
  340. }
  341. } else {
  342. $cleanId = $this->_getCacheId($pageNumber);
  343. static::$cache->removeItem($cleanId);
  344. }
  345. return $this;
  346. }
  347. /**
  348. * Returns the absolute item number for the specified item.
  349. *
  350. * @param integer $relativeItemNumber Relative item number
  351. * @param integer $pageNumber Page number
  352. * @return integer
  353. */
  354. public function getAbsoluteItemNumber($relativeItemNumber, $pageNumber = null)
  355. {
  356. $relativeItemNumber = $this->normalizeItemNumber($relativeItemNumber);
  357. if ($pageNumber == null) {
  358. $pageNumber = $this->getCurrentPageNumber();
  359. }
  360. $pageNumber = $this->normalizePageNumber($pageNumber);
  361. return (($pageNumber - 1) * $this->getItemCountPerPage()) + $relativeItemNumber;
  362. }
  363. /**
  364. * Returns the adapter.
  365. *
  366. * @return AdapterInterface
  367. */
  368. public function getAdapter()
  369. {
  370. return $this->adapter;
  371. }
  372. /**
  373. * Returns the number of items for the current page.
  374. *
  375. * @return integer
  376. */
  377. public function getCurrentItemCount()
  378. {
  379. if ($this->currentItemCount === null) {
  380. $this->currentItemCount = $this->getItemCount($this->getCurrentItems());
  381. }
  382. return $this->currentItemCount;
  383. }
  384. /**
  385. * Returns the items for the current page.
  386. *
  387. * @return Traversable
  388. */
  389. public function getCurrentItems()
  390. {
  391. if ($this->currentItems === null) {
  392. $this->currentItems = $this->getItemsByPage($this->getCurrentPageNumber());
  393. }
  394. return $this->currentItems;
  395. }
  396. /**
  397. * Returns the current page number.
  398. *
  399. * @return integer
  400. */
  401. public function getCurrentPageNumber()
  402. {
  403. return $this->normalizePageNumber($this->currentPageNumber);
  404. }
  405. /**
  406. * Sets the current page number.
  407. *
  408. * @param integer $pageNumber Page number
  409. * @return Paginator $this
  410. */
  411. public function setCurrentPageNumber($pageNumber)
  412. {
  413. $this->currentPageNumber = (integer) $pageNumber;
  414. $this->currentItems = null;
  415. $this->currentItemCount = null;
  416. return $this;
  417. }
  418. /**
  419. * Get the filter
  420. *
  421. * @return FilterInterface
  422. */
  423. public function getFilter()
  424. {
  425. return $this->filter;
  426. }
  427. /**
  428. * Set a filter chain
  429. *
  430. * @param FilterInterface $filter
  431. * @return Paginator
  432. */
  433. public function setFilter(FilterInterface $filter)
  434. {
  435. $this->filter = $filter;
  436. return $this;
  437. }
  438. /**
  439. * Returns an item from a page. The current page is used if there's no
  440. * page specified.
  441. *
  442. * @param integer $itemNumber Item number (1 to itemCountPerPage)
  443. * @param integer $pageNumber
  444. * @throws Exception\InvalidArgumentException
  445. * @return mixed
  446. */
  447. public function getItem($itemNumber, $pageNumber = null)
  448. {
  449. if ($pageNumber == null) {
  450. $pageNumber = $this->getCurrentPageNumber();
  451. } elseif ($pageNumber < 0) {
  452. $pageNumber = ($this->count() + 1) + $pageNumber;
  453. }
  454. $page = $this->getItemsByPage($pageNumber);
  455. $itemCount = $this->getItemCount($page);
  456. if ($itemCount == 0) {
  457. throw new Exception\InvalidArgumentException('Page ' . $pageNumber . ' does not exist');
  458. }
  459. if ($itemNumber < 0) {
  460. $itemNumber = ($itemCount + 1) + $itemNumber;
  461. }
  462. $itemNumber = $this->normalizeItemNumber($itemNumber);
  463. if ($itemNumber > $itemCount) {
  464. throw new Exception\InvalidArgumentException('Page ' . $pageNumber . ' does not'
  465. . ' contain item number ' . $itemNumber);
  466. }
  467. return $page[$itemNumber - 1];
  468. }
  469. /**
  470. * Returns the number of items per page.
  471. *
  472. * @return integer
  473. */
  474. public function getItemCountPerPage()
  475. {
  476. if (empty($this->itemCountPerPage)) {
  477. $this->itemCountPerPage = static::getDefaultItemCountPerPage();
  478. }
  479. return $this->itemCountPerPage;
  480. }
  481. /**
  482. * Sets the number of items per page.
  483. *
  484. * @param integer $itemCountPerPage
  485. * @return Paginator $this
  486. */
  487. public function setItemCountPerPage($itemCountPerPage = -1)
  488. {
  489. $this->itemCountPerPage = (integer) $itemCountPerPage;
  490. if ($this->itemCountPerPage < 1) {
  491. $this->itemCountPerPage = $this->getTotalItemCount();
  492. }
  493. $this->pageCount = $this->_calculatePageCount();
  494. $this->currentItems = null;
  495. $this->currentItemCount = null;
  496. return $this;
  497. }
  498. /**
  499. * Returns the number of items in a collection.
  500. *
  501. * @param mixed $items Items
  502. * @return integer
  503. */
  504. public function getItemCount($items)
  505. {
  506. $itemCount = 0;
  507. if (is_array($items) || $items instanceof Countable) {
  508. $itemCount = count($items);
  509. } elseif ($items instanceof Traversable) { // $items is something like LimitIterator
  510. $itemCount = iterator_count($items);
  511. }
  512. return $itemCount;
  513. }
  514. /**
  515. * Returns the items for a given page.
  516. *
  517. * @param integer $pageNumber
  518. * @return mixed
  519. */
  520. public function getItemsByPage($pageNumber)
  521. {
  522. $pageNumber = $this->normalizePageNumber($pageNumber);
  523. if ($this->cacheEnabled()) {
  524. $data = static::$cache->getItem($this->_getCacheId($pageNumber));
  525. if ($data) {
  526. return $data;
  527. }
  528. }
  529. $offset = ($pageNumber - 1) * $this->getItemCountPerPage();
  530. $items = $this->adapter->getItems($offset, $this->getItemCountPerPage());
  531. $filter = $this->getFilter();
  532. if ($filter !== null) {
  533. $items = $filter->filter($items);
  534. }
  535. if (!$items instanceof Traversable) {
  536. $items = new ArrayIterator($items);
  537. }
  538. if ($this->cacheEnabled()) {
  539. $cacheId = $this->_getCacheId($pageNumber);
  540. static::$cache->setItem($cacheId, $items);
  541. static::$cache->setTags($cacheId, array($this->_getCacheInternalId()));
  542. }
  543. return $items;
  544. }
  545. /**
  546. * Returns a foreach-compatible iterator.
  547. *
  548. * @throws Exception\RuntimeException
  549. * @return Traversable
  550. */
  551. public function getIterator()
  552. {
  553. try {
  554. return $this->getCurrentItems();
  555. } catch (\Exception $e) {
  556. throw new Exception\RuntimeException('Error producing an iterator', null, $e);
  557. }
  558. }
  559. /**
  560. * Returns the page range (see property declaration above).
  561. *
  562. * @return integer
  563. */
  564. public function getPageRange()
  565. {
  566. return $this->pageRange;
  567. }
  568. /**
  569. * Sets the page range (see property declaration above).
  570. *
  571. * @param integer $pageRange
  572. * @return Paginator $this
  573. */
  574. public function setPageRange($pageRange)
  575. {
  576. $this->pageRange = (integer) $pageRange;
  577. return $this;
  578. }
  579. /**
  580. * Returns the page collection.
  581. *
  582. * @param string $scrollingStyle Scrolling style
  583. * @return array
  584. */
  585. public function getPages($scrollingStyle = null)
  586. {
  587. if ($this->pages === null) {
  588. $this->pages = $this->_createPages($scrollingStyle);
  589. }
  590. return $this->pages;
  591. }
  592. /**
  593. * Returns a subset of pages within a given range.
  594. *
  595. * @param integer $lowerBound Lower bound of the range
  596. * @param integer $upperBound Upper bound of the range
  597. * @return array
  598. */
  599. public function getPagesInRange($lowerBound, $upperBound)
  600. {
  601. $lowerBound = $this->normalizePageNumber($lowerBound);
  602. $upperBound = $this->normalizePageNumber($upperBound);
  603. $pages = array();
  604. for ($pageNumber = $lowerBound; $pageNumber <= $upperBound; $pageNumber++) {
  605. $pages[$pageNumber] = $pageNumber;
  606. }
  607. return $pages;
  608. }
  609. /**
  610. * Returns the page item cache.
  611. *
  612. * @return array
  613. */
  614. public function getPageItemCache()
  615. {
  616. $data = array();
  617. if ($this->cacheEnabled()) {
  618. $prefixLength = strlen(self::CACHE_TAG_PREFIX);
  619. $cacheIterator = static::$cache->getIterator();
  620. $cacheIterator->setMode(CacheIterator::CURRENT_AS_VALUE);
  621. foreach ($cacheIterator as $key => $value) {
  622. $tags = static::$cache->getTags($key);
  623. if ($tags && in_array($this->_getCacheInternalId(), $tags)) {
  624. if (substr($key, 0, $prefixLength) == self::CACHE_TAG_PREFIX) {
  625. $data[(int) substr($key, $prefixLength)] = $value;
  626. }
  627. }
  628. }
  629. }
  630. return $data;
  631. }
  632. /**
  633. * Retrieves the view instance.
  634. *
  635. * If none registered, instantiates a PhpRenderer instance.
  636. *
  637. * @return \Zend\View\Renderer\RendererInterface|null
  638. */
  639. public function getView()
  640. {
  641. if ($this->view === null) {
  642. $this->setView(new View\Renderer\PhpRenderer());
  643. }
  644. return $this->view;
  645. }
  646. /**
  647. * Sets the view object.
  648. *
  649. * @param \Zend\View\Renderer\RendererInterface $view
  650. * @return Paginator
  651. */
  652. public function setView(View\Renderer\RendererInterface $view = null)
  653. {
  654. $this->view = $view;
  655. return $this;
  656. }
  657. /**
  658. * Brings the item number in range of the page.
  659. *
  660. * @param integer $itemNumber
  661. * @return integer
  662. */
  663. public function normalizeItemNumber($itemNumber)
  664. {
  665. $itemNumber = (integer) $itemNumber;
  666. if ($itemNumber < 1) {
  667. $itemNumber = 1;
  668. }
  669. if ($itemNumber > $this->getItemCountPerPage()) {
  670. $itemNumber = $this->getItemCountPerPage();
  671. }
  672. return $itemNumber;
  673. }
  674. /**
  675. * Brings the page number in range of the paginator.
  676. *
  677. * @param integer $pageNumber
  678. * @return integer
  679. */
  680. public function normalizePageNumber($pageNumber)
  681. {
  682. $pageNumber = (integer) $pageNumber;
  683. if ($pageNumber < 1) {
  684. $pageNumber = 1;
  685. }
  686. $pageCount = $this->count();
  687. if ($pageCount > 0 && $pageNumber > $pageCount) {
  688. $pageNumber = $pageCount;
  689. }
  690. return $pageNumber;
  691. }
  692. /**
  693. * Renders the paginator.
  694. *
  695. * @param \Zend\View\Renderer\RendererInterface $view
  696. * @return string
  697. */
  698. public function render(View\Renderer\RendererInterface $view = null)
  699. {
  700. if (null !== $view) {
  701. $this->setView($view);
  702. }
  703. $view = $this->getView();
  704. return $view->paginationControl($this);
  705. }
  706. /**
  707. * Returns the items of the current page as JSON.
  708. *
  709. * @return string
  710. */
  711. public function toJson()
  712. {
  713. $currentItems = $this->getCurrentItems();
  714. if ($currentItems instanceof AbstractResultSet) {
  715. return Json::encode($currentItems->toArray());
  716. }
  717. return Json::encode($currentItems);
  718. }
  719. /**
  720. * Tells if there is an active cache object
  721. * and if the cache has not been disabled
  722. *
  723. * @return bool
  724. */
  725. protected function cacheEnabled()
  726. {
  727. return ((static::$cache !== null) && $this->cacheEnabled);
  728. }
  729. /**
  730. * Makes an Id for the cache
  731. * Depends on the adapter object and the page number
  732. *
  733. * Used to store item in cache from that Paginator instance
  734. * and that current page
  735. *
  736. * @param int $page
  737. * @return string
  738. */
  739. protected function _getCacheId($page = null)
  740. {
  741. if ($page === null) {
  742. $page = $this->getCurrentPageNumber();
  743. }
  744. return self::CACHE_TAG_PREFIX . $page . '_' . $this->_getCacheInternalId();
  745. }
  746. /**
  747. * Get the internal cache id
  748. * Depends on the adapter and the item count per page
  749. *
  750. * Used to tag that unique Paginator instance in cache
  751. *
  752. * @return string
  753. */
  754. protected function _getCacheInternalId()
  755. {
  756. return md5(serialize(array(
  757. spl_object_hash($this->getAdapter()),
  758. $this->getItemCountPerPage()
  759. )));
  760. }
  761. /**
  762. * Calculates the page count.
  763. *
  764. * @return integer
  765. */
  766. protected function _calculatePageCount()
  767. {
  768. return (integer) ceil($this->getAdapter()->count() / $this->getItemCountPerPage());
  769. }
  770. /**
  771. * Creates the page collection.
  772. *
  773. * @param string $scrollingStyle Scrolling style
  774. * @return \stdClass
  775. */
  776. protected function _createPages($scrollingStyle = null)
  777. {
  778. $pageCount = $this->count();
  779. $currentPageNumber = $this->getCurrentPageNumber();
  780. $pages = new \stdClass();
  781. $pages->pageCount = $pageCount;
  782. $pages->itemCountPerPage = $this->getItemCountPerPage();
  783. $pages->first = 1;
  784. $pages->current = $currentPageNumber;
  785. $pages->last = $pageCount;
  786. // Previous and next
  787. if ($currentPageNumber - 1 > 0) {
  788. $pages->previous = $currentPageNumber - 1;
  789. }
  790. if ($currentPageNumber + 1 <= $pageCount) {
  791. $pages->next = $currentPageNumber + 1;
  792. }
  793. // Pages in range
  794. $scrollingStyle = $this->_loadScrollingStyle($scrollingStyle);
  795. $pages->pagesInRange = $scrollingStyle->getPages($this);
  796. $pages->firstPageInRange = min($pages->pagesInRange);
  797. $pages->lastPageInRange = max($pages->pagesInRange);
  798. // Item numbers
  799. if ($this->getCurrentItems() !== null) {
  800. $pages->currentItemCount = $this->getCurrentItemCount();
  801. $pages->itemCountPerPage = $this->getItemCountPerPage();
  802. $pages->totalItemCount = $this->getTotalItemCount();
  803. $pages->firstItemNumber = (($currentPageNumber - 1) * $this->getItemCountPerPage()) + 1;
  804. $pages->lastItemNumber = $pages->firstItemNumber + $pages->currentItemCount - 1;
  805. }
  806. return $pages;
  807. }
  808. /**
  809. * Loads a scrolling style.
  810. *
  811. * @param string $scrollingStyle
  812. * @return ScrollingStyleInterface
  813. * @throws Exception\InvalidArgumentException
  814. */
  815. protected function _loadScrollingStyle($scrollingStyle = null)
  816. {
  817. if ($scrollingStyle === null) {
  818. $scrollingStyle = static::$defaultScrollingStyle;
  819. }
  820. switch (strtolower(gettype($scrollingStyle))) {
  821. case 'object':
  822. if (!$scrollingStyle instanceof ScrollingStyleInterface) {
  823. throw new Exception\InvalidArgumentException(
  824. 'Scrolling style must implement Zend\Paginator\ScrollingStyle\ScrollingStyleInterface'
  825. );
  826. }
  827. return $scrollingStyle;
  828. case 'string':
  829. return static::getScrollingStylePluginManager()->get($scrollingStyle);
  830. case 'null':
  831. // Fall through to default case
  832. default:
  833. throw new Exception\InvalidArgumentException(
  834. 'Scrolling style must be a class ' .
  835. 'name or object implementing Zend\Paginator\ScrollingStyle\ScrollingStyleInterface'
  836. );
  837. }
  838. }
  839. }