From 0d91c05f02cec7c2c6f0732cee44192ee1aab63b Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sun, 27 Dec 2015 17:41:43 +0900 Subject: [PATCH 01/15] Move toJson() method To JsonSerializeTrait --- src/JsonSerializeTrait.php | 38 ++++++++++++++++++++++++++++++++++++++ src/Paginator.php | 19 ++----------------- 2 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 src/JsonSerializeTrait.php diff --git a/src/JsonSerializeTrait.php b/src/JsonSerializeTrait.php new file mode 100644 index 0000000..ca55622 --- /dev/null +++ b/src/JsonSerializeTrait.php @@ -0,0 +1,38 @@ +toJson(); + } + + /** + * Returns the items of the current page as JSON. + * + * @return string + */ + public function toJson() + { + $currentItems = $this->getCurrentItems(); + + if ($currentItems instanceof AbstractResultSet) { + return Json::encode($currentItems->toArray()); + } + return Json::encode($currentItems); + } +} \ No newline at end of file diff --git a/src/Paginator.php b/src/Paginator.php index bec3691..20d8bfc 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -15,9 +15,7 @@ use Traversable; use Zend\Cache\Storage\IteratorInterface as CacheIterator; use Zend\Cache\Storage\StorageInterface as CacheStorage; -use Zend\Db\ResultSet\AbstractResultSet; use Zend\Filter\FilterInterface; -use Zend\Json\Json; use Zend\Paginator\Adapter\AdapterInterface; use Zend\Paginator\ScrollingStyle\ScrollingStyleInterface; use Zend\Stdlib\ArrayUtils; @@ -26,6 +24,8 @@ class Paginator implements Countable, IteratorAggregate { + use JsonSerializeTrait; + /** * The cache tag prefix used to namespace Paginator results in the cache * @@ -805,21 +805,6 @@ public function render(View\Renderer\RendererInterface $view = null) return $view->paginationControl($this); } - /** - * Returns the items of the current page as JSON. - * - * @return string - */ - public function toJson() - { - $currentItems = $this->getCurrentItems(); - - if ($currentItems instanceof AbstractResultSet) { - return Json::encode($currentItems->toArray()); - } - return Json::encode($currentItems); - } - /** * Tells if there is an active cache object * and if the cache has not been disabled From 9615fcdcdb705b8076d9b89adfe841becc148d30 Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sun, 27 Dec 2015 17:50:12 +0900 Subject: [PATCH 02/15] Move render() & __toString() requires To RenderTrait --- src/Paginator.php | 72 ++----------------------------------- src/RenderTrait.php | 86 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 70 deletions(-) create mode 100644 src/RenderTrait.php diff --git a/src/Paginator.php b/src/Paginator.php index 20d8bfc..3111aa3 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -19,12 +19,12 @@ use Zend\Paginator\Adapter\AdapterInterface; use Zend\Paginator\ScrollingStyle\ScrollingStyleInterface; use Zend\Stdlib\ArrayUtils; -use Zend\View; use Zend\ServiceManager\ServiceManager; class Paginator implements Countable, IteratorAggregate { - use JsonSerializeTrait; + use JsonSerializeTrait, + RenderTrait; /** * The cache tag prefix used to namespace Paginator results in the cache @@ -145,13 +145,6 @@ class Paginator implements Countable, IteratorAggregate */ protected $pages = null; - /** - * View instance used for self rendering - * - * @var \Zend\View\Renderer\RendererInterface - */ - protected $view = null; - /** * Set a global config * @@ -303,23 +296,6 @@ public function __construct($adapter) } } - /** - * Serializes the object as a string. Proxies to {@link render()}. - * - * @return string - */ - public function __toString() - { - try { - $return = $this->render(); - return $return; - } catch (\Exception $e) { - trigger_error($e->getMessage(), E_USER_WARNING); - } - - return ''; - } - /** * Enables/Disables the cache for this instance * @@ -715,34 +691,7 @@ public function getPageItemCache() return $data; } - /** - * Retrieves the view instance. - * - * If none registered, instantiates a PhpRenderer instance. - * - * @return \Zend\View\Renderer\RendererInterface|null - */ - public function getView() - { - if ($this->view === null) { - $this->setView(new View\Renderer\PhpRenderer()); - } - return $this->view; - } - - /** - * Sets the view object. - * - * @param \Zend\View\Renderer\RendererInterface $view - * @return Paginator - */ - public function setView(View\Renderer\RendererInterface $view = null) - { - $this->view = $view; - - return $this; - } /** * Brings the item number in range of the page. @@ -788,23 +737,6 @@ public function normalizePageNumber($pageNumber) return $pageNumber; } - /** - * Renders the paginator. - * - * @param \Zend\View\Renderer\RendererInterface $view - * @return string - */ - public function render(View\Renderer\RendererInterface $view = null) - { - if (null !== $view) { - $this->setView($view); - } - - $view = $this->getView(); - - return $view->paginationControl($this); - } - /** * Tells if there is an active cache object * and if the cache has not been disabled diff --git a/src/RenderTrait.php b/src/RenderTrait.php new file mode 100644 index 0000000..39c8400 --- /dev/null +++ b/src/RenderTrait.php @@ -0,0 +1,86 @@ +view === null) { + $this->setView(new View\Renderer\PhpRenderer()); + } + + return $this->view; + } + + /** + * Sets the view object. + * + * @param \Zend\View\Renderer\RendererInterface $view + * @return Paginator + */ + public function setView(View\Renderer\RendererInterface $view = null) + { + $this->view = $view; + + return $this; + } + + /** + * Renders the paginator. + * + * @param \Zend\View\Renderer\RendererInterface $view + * @return string + */ + public function render(View\Renderer\RendererInterface $view = null) + { + if (null !== $view) { + $this->setView($view); + } + + $view = $this->getView(); + + return $view->paginationControl($this); + } + + /** + * Serializes the object as a string. Proxies to {@link render()}. + * + * @return string + */ + public function __toString() + { + try { + $return = $this->render(); + return $return; + } catch (\Exception $e) { + trigger_error($e->getMessage(), E_USER_WARNING); + } + + return ''; + } + +} \ No newline at end of file From f91f696ff509789db16df2beaa0fe967c88dae00 Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sun, 27 Dec 2015 18:06:05 +0900 Subject: [PATCH 03/15] Move caching To CachedTrait todo getItemsByPage() must override by caching --- src/CachedTrait.php | 187 ++++++++++++++++++++++++++++++++++++++++++++ src/Paginator.php | 146 ---------------------------------- 2 files changed, 187 insertions(+), 146 deletions(-) create mode 100644 src/CachedTrait.php diff --git a/src/CachedTrait.php b/src/CachedTrait.php new file mode 100644 index 0000000..b0d9afd --- /dev/null +++ b/src/CachedTrait.php @@ -0,0 +1,187 @@ +cacheEnabled = (bool) $enable; + return $this; + } + + /** + * Returns the items for a given page. + * + * @param int $pageNumber + * @return mixed + */ + public function getItemsByPage($pageNumber) + { + $pageNumber = $this->normalizePageNumber($pageNumber); + + if ($this->cacheEnabled()) { + $data = static::$cache->getItem($this->_getCacheId($pageNumber)); + if ($data) { + return $data; + } + } + + $offset = ($pageNumber - 1) * $this->getItemCountPerPage(); + + $items = $this->adapter->getItems($offset, $this->getItemCountPerPage()); + + $filter = $this->getFilter(); + + if ($filter !== null) { + $items = $filter->filter($items); + } + + if (!$items instanceof Traversable) { + $items = new ArrayIterator($items); + } + + if ($this->cacheEnabled()) { + $cacheId = $this->_getCacheId($pageNumber); + static::$cache->setItem($cacheId, $items); + } + + return $items; + } + + /** + * Returns the page item cache. + * + * @return array + */ + public function getPageItemCache() + { + $data = []; + if ($this->cacheEnabled()) { + $prefixLength = strlen(self::CACHE_TAG_PREFIX); + $cacheIterator = static::$cache->getIterator(); + $cacheIterator->setMode(CacheIterator::CURRENT_AS_VALUE); + foreach ($cacheIterator as $key => $value) { + if (substr($key, 0, $prefixLength) == self::CACHE_TAG_PREFIX) { + $data[(int) substr($key, $prefixLength)] = $value; + } + } + } + return $data; + } + + /** + * Clear the page item cache. + * + * @param int $pageNumber + * @return Paginator + */ + public function clearPageItemCache($pageNumber = null) + { + if (!$this->cacheEnabled()) { + return $this; + } + + if (null === $pageNumber) { + $prefixLength = strlen(self::CACHE_TAG_PREFIX); + $cacheIterator = static::$cache->getIterator(); + $cacheIterator->setMode(CacheIterator::CURRENT_AS_KEY); + foreach ($cacheIterator as $key) { + if (substr($key, 0, $prefixLength) == self::CACHE_TAG_PREFIX) { + static::$cache->removeItem($this->_getCacheId((int)substr($key, $prefixLength))); + } + } + } else { + $cleanId = $this->_getCacheId($pageNumber); + static::$cache->removeItem($cleanId); + } + return $this; + } + + /** + * Tells if there is an active cache object + * and if the cache has not been disabled + * + * @return bool + */ + protected function cacheEnabled() + { + return ((static::$cache !== null) && $this->cacheEnabled); + } + + /** + * Makes an Id for the cache + * Depends on the adapter object and the page number + * + * Used to store item in cache from that Paginator instance + * and that current page + * + * @param int $page + * @return string + */ + protected function _getCacheId($page = null) + { + if ($page === null) { + $page = $this->getCurrentPageNumber(); + } + return self::CACHE_TAG_PREFIX . $page . '_' . $this->_getCacheInternalId(); + } + + /** + * Get the internal cache id + * Depends on the adapter and the item count per page + * + * Used to tag that unique Paginator instance in cache + * + * @return string + */ + protected function _getCacheInternalId() + { + return md5(serialize([ + spl_object_hash($this->getAdapter()), + $this->getItemCountPerPage() + ])); + } + +} \ No newline at end of file diff --git a/src/Paginator.php b/src/Paginator.php index 3111aa3..ab7c994 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -13,8 +13,6 @@ use Countable; use IteratorAggregate; use Traversable; -use Zend\Cache\Storage\IteratorInterface as CacheIterator; -use Zend\Cache\Storage\StorageInterface as CacheStorage; use Zend\Filter\FilterInterface; use Zend\Paginator\Adapter\AdapterInterface; use Zend\Paginator\ScrollingStyle\ScrollingStyleInterface; @@ -67,20 +65,6 @@ class Paginator implements Countable, IteratorAggregate */ protected static $scrollingStyles = null; - /** - * Cache object - * - * @var CacheStorage - */ - protected static $cache; - - /** - * Enable or disable the cache by Zend\Paginator\Paginator instance - * - * @var bool - */ - protected $cacheEnabled = true; - /** * Adapter * @@ -205,16 +189,6 @@ public static function setDefaultItemCountPerPage($count) static::$defaultItemCountPerPage = (int) $count; } - /** - * Sets a cache object - * - * @param CacheStorage $cache - */ - public static function setCache(CacheStorage $cache) - { - static::$cache = $cache; - } - /** * Sets the default scrolling style. * @@ -296,18 +270,6 @@ public function __construct($adapter) } } - /** - * Enables/Disables the cache for this instance - * - * @param bool $enable - * @return Paginator - */ - public function setCacheEnabled($enable) - { - $this->cacheEnabled = (bool) $enable; - return $this; - } - /** * Returns the number of pages. * @@ -332,34 +294,6 @@ public function getTotalItemCount() return count($this->getAdapter()); } - /** - * Clear the page item cache. - * - * @param int $pageNumber - * @return Paginator - */ - public function clearPageItemCache($pageNumber = null) - { - if (!$this->cacheEnabled()) { - return $this; - } - - if (null === $pageNumber) { - $prefixLength = strlen(self::CACHE_TAG_PREFIX); - $cacheIterator = static::$cache->getIterator(); - $cacheIterator->setMode(CacheIterator::CURRENT_AS_KEY); - foreach ($cacheIterator as $key) { - if (substr($key, 0, $prefixLength) == self::CACHE_TAG_PREFIX) { - static::$cache->removeItem($this->_getCacheId((int)substr($key, $prefixLength))); - } - } - } else { - $cleanId = $this->_getCacheId($pageNumber); - static::$cache->removeItem($cleanId); - } - return $this; - } - /** * Returns the absolute item number for the specified item. * @@ -567,13 +501,6 @@ public function getItemsByPage($pageNumber) { $pageNumber = $this->normalizePageNumber($pageNumber); - if ($this->cacheEnabled()) { - $data = static::$cache->getItem($this->_getCacheId($pageNumber)); - if ($data) { - return $data; - } - } - $offset = ($pageNumber - 1) * $this->getItemCountPerPage(); $items = $this->adapter->getItems($offset, $this->getItemCountPerPage()); @@ -588,11 +515,6 @@ public function getItemsByPage($pageNumber) $items = new ArrayIterator($items); } - if ($this->cacheEnabled()) { - $cacheId = $this->_getCacheId($pageNumber); - static::$cache->setItem($cacheId, $items); - } - return $items; } @@ -670,29 +592,6 @@ public function getPagesInRange($lowerBound, $upperBound) return $pages; } - /** - * Returns the page item cache. - * - * @return array - */ - public function getPageItemCache() - { - $data = []; - if ($this->cacheEnabled()) { - $prefixLength = strlen(self::CACHE_TAG_PREFIX); - $cacheIterator = static::$cache->getIterator(); - $cacheIterator->setMode(CacheIterator::CURRENT_AS_VALUE); - foreach ($cacheIterator as $key => $value) { - if (substr($key, 0, $prefixLength) == self::CACHE_TAG_PREFIX) { - $data[(int) substr($key, $prefixLength)] = $value; - } - } - } - return $data; - } - - - /** * Brings the item number in range of the page. * @@ -737,51 +636,6 @@ public function normalizePageNumber($pageNumber) return $pageNumber; } - /** - * Tells if there is an active cache object - * and if the cache has not been disabled - * - * @return bool - */ - protected function cacheEnabled() - { - return ((static::$cache !== null) && $this->cacheEnabled); - } - - /** - * Makes an Id for the cache - * Depends on the adapter object and the page number - * - * Used to store item in cache from that Paginator instance - * and that current page - * - * @param int $page - * @return string - */ - protected function _getCacheId($page = null) - { - if ($page === null) { - $page = $this->getCurrentPageNumber(); - } - return self::CACHE_TAG_PREFIX . $page . '_' . $this->_getCacheInternalId(); - } - - /** - * Get the internal cache id - * Depends on the adapter and the item count per page - * - * Used to tag that unique Paginator instance in cache - * - * @return string - */ - protected function _getCacheInternalId() - { - return md5(serialize([ - spl_object_hash($this->getAdapter()), - $this->getItemCountPerPage() - ])); - } - /** * Calculates the page count. * From fb6a8a74b73e6abafb4374410b8e43ae3fae5396 Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sun, 27 Dec 2015 18:29:29 +0900 Subject: [PATCH 04/15] Introduce PaginatorInterface Introduced SimplePaginator, too. Cache & Filter is required getItemsByPage --- src/CachedTrait.php | 39 --- src/FilterTrait.php | 46 +++ src/Paginator.php | 698 +------------------------------------ src/PaginatorInterface.php | 219 ++++++++++++ src/SimplePaginator.php | 687 ++++++++++++++++++++++++++++++++++++ 5 files changed, 967 insertions(+), 722 deletions(-) create mode 100644 src/FilterTrait.php create mode 100644 src/PaginatorInterface.php create mode 100644 src/SimplePaginator.php diff --git a/src/CachedTrait.php b/src/CachedTrait.php index b0d9afd..be92b77 100644 --- a/src/CachedTrait.php +++ b/src/CachedTrait.php @@ -51,45 +51,6 @@ public function setCacheEnabled($enable) return $this; } - /** - * Returns the items for a given page. - * - * @param int $pageNumber - * @return mixed - */ - public function getItemsByPage($pageNumber) - { - $pageNumber = $this->normalizePageNumber($pageNumber); - - if ($this->cacheEnabled()) { - $data = static::$cache->getItem($this->_getCacheId($pageNumber)); - if ($data) { - return $data; - } - } - - $offset = ($pageNumber - 1) * $this->getItemCountPerPage(); - - $items = $this->adapter->getItems($offset, $this->getItemCountPerPage()); - - $filter = $this->getFilter(); - - if ($filter !== null) { - $items = $filter->filter($items); - } - - if (!$items instanceof Traversable) { - $items = new ArrayIterator($items); - } - - if ($this->cacheEnabled()) { - $cacheId = $this->_getCacheId($pageNumber); - static::$cache->setItem($cacheId, $items); - } - - return $items; - } - /** * Returns the page item cache. * diff --git a/src/FilterTrait.php b/src/FilterTrait.php new file mode 100644 index 0000000..7340b04 --- /dev/null +++ b/src/FilterTrait.php @@ -0,0 +1,46 @@ +filter; + } + + /** + * Set a filter chain + * + * @param FilterInterface $filter + * @return Paginator + */ + public function setFilter(FilterInterface $filter) + { + $this->filter = $filter; + + return $this; + } +} \ No newline at end of file diff --git a/src/Paginator.php b/src/Paginator.php index ab7c994..f28a275 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -10,19 +10,14 @@ namespace Zend\Paginator; use ArrayIterator; -use Countable; -use IteratorAggregate; use Traversable; -use Zend\Filter\FilterInterface; -use Zend\Paginator\Adapter\AdapterInterface; -use Zend\Paginator\ScrollingStyle\ScrollingStyleInterface; use Zend\Stdlib\ArrayUtils; -use Zend\ServiceManager\ServiceManager; -class Paginator implements Countable, IteratorAggregate +class Paginator extends SimplePaginator { use JsonSerializeTrait, - RenderTrait; + RenderTrait, + FilterTrait; /** * The cache tag prefix used to namespace Paginator results in the cache @@ -30,477 +25,21 @@ class Paginator implements Countable, IteratorAggregate */ const CACHE_TAG_PREFIX = 'Zend_Paginator_'; - /** - * Adapter plugin manager - * - * @var AdapterPluginManager - */ - protected static $adapters = null; - - /** - * Configuration file - * - * @var array|null - */ - protected static $config = null; - - /** - * Default scrolling style - * - * @var string - */ - protected static $defaultScrollingStyle = 'Sliding'; - - /** - * Default item count per page - * - * @var int - */ - protected static $defaultItemCountPerPage = 10; - - /** - * Scrolling style plugin manager - * - * @var ScrollingStylePluginManager - */ - protected static $scrollingStyles = null; - - /** - * Adapter - * - * @var AdapterInterface - */ - protected $adapter = null; - - /** - * Number of items in the current page - * - * @var int - */ - protected $currentItemCount = null; /** - * Current page items - * - * @var Traversable - */ - protected $currentItems = null; - - /** - * Current page number (starting from 1) - * - * @var int + * @inheritdoc */ - protected $currentPageNumber = 1; - - /** - * Result filter - * - * @var FilterInterface - */ - protected $filter = null; - - /** - * Number of items per page - * - * @var int - */ - protected $itemCountPerPage = null; - - /** - * Number of pages - * - * @var int - */ - protected $pageCount = null; - - /** - * Number of local pages (i.e., the number of discrete page numbers - * that will be displayed, including the current page number) - * - * @var int - */ - protected $pageRange = 10; - - /** - * Pages - * - * @var \stdClass - */ - protected $pages = null; - - /** - * Set a global config - * - * @param array|Traversable $config - * @throws Exception\InvalidArgumentException - */ - public static function setGlobalConfig($config) - { - if ($config instanceof Traversable) { - $config = ArrayUtils::iteratorToArray($config); - } - if (!is_array($config)) { - throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable'); - } - - static::$config = $config; - - if (isset($config['scrolling_style_plugins']) - && null !== ($adapters = $config['scrolling_style_plugins']) - ) { - static::setScrollingStylePluginManager($adapters); - } - - $scrollingStyle = isset($config['scrolling_style']) ? $config['scrolling_style'] : null; - - if ($scrollingStyle !== null) { - static::setDefaultScrollingStyle($scrollingStyle); - } - } - - /** - * Returns the default scrolling style. - * - * @return string - */ - public static function getDefaultScrollingStyle() - { - return static::$defaultScrollingStyle; - } - - /** - * Get the default item count per page - * - * @return int - */ - public static function getDefaultItemCountPerPage() - { - return static::$defaultItemCountPerPage; - } - - /** - * Set the default item count per page - * - * @param int $count - */ - public static function setDefaultItemCountPerPage($count) - { - static::$defaultItemCountPerPage = (int) $count; - } - - /** - * Sets the default scrolling style. - * - * @param string $scrollingStyle - */ - public static function setDefaultScrollingStyle($scrollingStyle = 'Sliding') - { - static::$defaultScrollingStyle = $scrollingStyle; - } - - public static function setScrollingStylePluginManager($scrollingAdapters) - { - if (is_string($scrollingAdapters)) { - if (!class_exists($scrollingAdapters)) { - throw new Exception\InvalidArgumentException(sprintf( - 'Unable to locate scrolling style plugin manager with class "%s"; class not found', - $scrollingAdapters - )); - } - $scrollingAdapters = new $scrollingAdapters(new ServiceManager); - } - if (!$scrollingAdapters instanceof ScrollingStylePluginManager) { - throw new Exception\InvalidArgumentException(sprintf( - 'Pagination scrolling-style manager must extend ScrollingStylePluginManager; received "%s"', - (is_object($scrollingAdapters) ? get_class($scrollingAdapters) : gettype($scrollingAdapters)) - )); - } - static::$scrollingStyles = $scrollingAdapters; - } - - /** - * Returns the scrolling style manager. If it doesn't exist it's - * created. - * - * @return ScrollingStylePluginManager - */ - public static function getScrollingStylePluginManager() - { - if (static::$scrollingStyles === null) { - static::$scrollingStyles = new ScrollingStylePluginManager(new ServiceManager); - } - - return static::$scrollingStyles; - } - - /** - * Constructor. - * - * @param AdapterInterface|AdapterAggregateInterface $adapter - * @throws Exception\InvalidArgumentException - */ - public function __construct($adapter) - { - if ($adapter instanceof AdapterInterface) { - $this->adapter = $adapter; - } elseif ($adapter instanceof AdapterAggregateInterface) { - $this->adapter = $adapter->getPaginatorAdapter(); - } else { - throw new Exception\InvalidArgumentException( - 'Zend\Paginator only accepts instances of the type ' . - 'Zend\Paginator\Adapter\AdapterInterface or Zend\Paginator\AdapterAggregateInterface.' - ); - } - - $config = static::$config; - - if (!empty($config)) { - $setupMethods = ['ItemCountPerPage', 'PageRange']; - - foreach ($setupMethods as $setupMethod) { - $key = strtolower($setupMethod); - $value = isset($config[$key]) ? $config[$key] : null; - - if ($value !== null) { - $setupMethod = 'set' . $setupMethod; - $this->$setupMethod($value); - } - } - } - } - - /** - * Returns the number of pages. - * - * @return int - */ - public function count() - { - if (!$this->pageCount) { - $this->pageCount = $this->_calculatePageCount(); - } - - return $this->pageCount; - } - - /** - * Returns the total number of items available. - * - * @return int - */ - public function getTotalItemCount() - { - return count($this->getAdapter()); - } - - /** - * Returns the absolute item number for the specified item. - * - * @param int $relativeItemNumber Relative item number - * @param int $pageNumber Page number - * @return int - */ - public function getAbsoluteItemNumber($relativeItemNumber, $pageNumber = null) + public function getItemsByPage($pageNumber) { - $relativeItemNumber = $this->normalizeItemNumber($relativeItemNumber); - - if ($pageNumber === null) { - $pageNumber = $this->getCurrentPageNumber(); - } - $pageNumber = $this->normalizePageNumber($pageNumber); - return (($pageNumber - 1) * $this->getItemCountPerPage()) + $relativeItemNumber; - } - - /** - * Returns the adapter. - * - * @return AdapterInterface - */ - public function getAdapter() - { - return $this->adapter; - } - - /** - * Returns the number of items for the current page. - * - * @return int - */ - public function getCurrentItemCount() - { - if ($this->currentItemCount === null) { - $this->currentItemCount = $this->getItemCount($this->getCurrentItems()); - } - - return $this->currentItemCount; - } - - /** - * Returns the items for the current page. - * - * @return Traversable - */ - public function getCurrentItems() - { - if ($this->currentItems === null) { - $this->currentItems = $this->getItemsByPage($this->getCurrentPageNumber()); - } - - return $this->currentItems; - } - - /** - * Returns the current page number. - * - * @return int - */ - public function getCurrentPageNumber() - { - return $this->normalizePageNumber($this->currentPageNumber); - } - - /** - * Sets the current page number. - * - * @param int $pageNumber Page number - * @return Paginator $this - */ - public function setCurrentPageNumber($pageNumber) - { - $this->currentPageNumber = (int) $pageNumber; - $this->currentItems = null; - $this->currentItemCount = null; - - return $this; - } - - /** - * Get the filter - * - * @return FilterInterface - */ - public function getFilter() - { - return $this->filter; - } - - /** - * Set a filter chain - * - * @param FilterInterface $filter - * @return Paginator - */ - public function setFilter(FilterInterface $filter) - { - $this->filter = $filter; - - return $this; - } - - /** - * Returns an item from a page. The current page is used if there's no - * page specified. - * - * @param int $itemNumber Item number (1 to itemCountPerPage) - * @param int $pageNumber - * @throws Exception\InvalidArgumentException - * @return mixed - */ - public function getItem($itemNumber, $pageNumber = null) - { - if ($pageNumber === null) { - $pageNumber = $this->getCurrentPageNumber(); - } elseif ($pageNumber < 0) { - $pageNumber = ($this->count() + 1) + $pageNumber; - } - - $page = $this->getItemsByPage($pageNumber); - $itemCount = $this->getItemCount($page); - - if ($itemCount == 0) { - throw new Exception\InvalidArgumentException('Page ' . $pageNumber . ' does not exist'); - } - - if ($itemNumber < 0) { - $itemNumber = ($itemCount + 1) + $itemNumber; - } - - $itemNumber = $this->normalizeItemNumber($itemNumber); - - if ($itemNumber > $itemCount) { - throw new Exception\InvalidArgumentException( - "Page {$pageNumber} does not contain item number {$itemNumber}" - ); - } - - return $page[$itemNumber - 1]; - } - - /** - * Returns the number of items per page. - * - * @return int - */ - public function getItemCountPerPage() - { - if (empty($this->itemCountPerPage)) { - $this->itemCountPerPage = static::getDefaultItemCountPerPage(); - } - - return $this->itemCountPerPage; - } - - /** - * Sets the number of items per page. - * - * @param int $itemCountPerPage - * @return Paginator $this - */ - public function setItemCountPerPage($itemCountPerPage = -1) - { - $this->itemCountPerPage = (int) $itemCountPerPage; - if ($this->itemCountPerPage < 1) { - $this->itemCountPerPage = $this->getTotalItemCount(); - } - $this->pageCount = $this->_calculatePageCount(); - $this->currentItems = null; - $this->currentItemCount = null; - - return $this; - } - - /** - * Returns the number of items in a collection. - * - * @param mixed $items Items - * @return int - */ - public function getItemCount($items) - { - $itemCount = 0; - - if (is_array($items) || $items instanceof Countable) { - $itemCount = count($items); - } elseif ($items instanceof Traversable) { // $items is something like LimitIterator - $itemCount = iterator_count($items); + if ($this->cacheEnabled()) { + $data = static::$cache->getItem($this->_getCacheId($pageNumber)); + if ($data) { + return $data; + } } - return $itemCount; - } - - /** - * Returns the items for a given page. - * - * @param int $pageNumber - * @return mixed - */ - public function getItemsByPage($pageNumber) - { - $pageNumber = $this->normalizePageNumber($pageNumber); - $offset = ($pageNumber - 1) * $this->getItemCountPerPage(); $items = $this->adapter->getItems($offset, $this->getItemCountPerPage()); @@ -515,219 +54,12 @@ public function getItemsByPage($pageNumber) $items = new ArrayIterator($items); } - return $items; - } - - /** - * Returns a foreach-compatible iterator. - * - * @throws Exception\RuntimeException - * @return Traversable - */ - public function getIterator() - { - try { - return $this->getCurrentItems(); - } catch (\Exception $e) { - throw new Exception\RuntimeException('Error producing an iterator', null, $e); + if ($this->cacheEnabled()) { + $cacheId = $this->_getCacheId($pageNumber); + static::$cache->setItem($cacheId, $items); } - } - /** - * Returns the page range (see property declaration above). - * - * @return int - */ - public function getPageRange() - { - return $this->pageRange; - } - - /** - * Sets the page range (see property declaration above). - * - * @param int $pageRange - * @return Paginator $this - */ - public function setPageRange($pageRange) - { - $this->pageRange = (int) $pageRange; - - return $this; - } - - /** - * Returns the page collection. - * - * @param string $scrollingStyle Scrolling style - * @return \stdClass - */ - public function getPages($scrollingStyle = null) - { - if ($this->pages === null) { - $this->pages = $this->_createPages($scrollingStyle); - } - - return $this->pages; - } - - /** - * Returns a subset of pages within a given range. - * - * @param int $lowerBound Lower bound of the range - * @param int $upperBound Upper bound of the range - * @return array - */ - public function getPagesInRange($lowerBound, $upperBound) - { - $lowerBound = $this->normalizePageNumber($lowerBound); - $upperBound = $this->normalizePageNumber($upperBound); - - $pages = []; - - for ($pageNumber = $lowerBound; $pageNumber <= $upperBound; $pageNumber++) { - $pages[$pageNumber] = $pageNumber; - } - - return $pages; - } - - /** - * Brings the item number in range of the page. - * - * @param int $itemNumber - * @return int - */ - public function normalizeItemNumber($itemNumber) - { - $itemNumber = (int) $itemNumber; - - if ($itemNumber < 1) { - $itemNumber = 1; - } - - if ($itemNumber > $this->getItemCountPerPage()) { - $itemNumber = $this->getItemCountPerPage(); - } - - return $itemNumber; - } - - /** - * Brings the page number in range of the paginator. - * - * @param int $pageNumber - * @return int - */ - public function normalizePageNumber($pageNumber) - { - $pageNumber = (int) $pageNumber; - - if ($pageNumber < 1) { - $pageNumber = 1; - } - - $pageCount = $this->count(); - - if ($pageCount > 0 && $pageNumber > $pageCount) { - $pageNumber = $pageCount; - } - - return $pageNumber; - } - - /** - * Calculates the page count. - * - * @return int - */ - protected function _calculatePageCount() - { - return (int) ceil($this->getAdapter()->count() / $this->getItemCountPerPage()); - } - - /** - * Creates the page collection. - * - * @param string $scrollingStyle Scrolling style - * @return \stdClass - */ - protected function _createPages($scrollingStyle = null) - { - $pageCount = $this->count(); - $currentPageNumber = $this->getCurrentPageNumber(); - - $pages = new \stdClass(); - $pages->pageCount = $pageCount; - $pages->itemCountPerPage = $this->getItemCountPerPage(); - $pages->first = 1; - $pages->current = $currentPageNumber; - $pages->last = $pageCount; - - // Previous and next - if ($currentPageNumber - 1 > 0) { - $pages->previous = $currentPageNumber - 1; - } - - if ($currentPageNumber + 1 <= $pageCount) { - $pages->next = $currentPageNumber + 1; - } - - // Pages in range - $scrollingStyle = $this->_loadScrollingStyle($scrollingStyle); - $pages->pagesInRange = $scrollingStyle->getPages($this); - $pages->firstPageInRange = min($pages->pagesInRange); - $pages->lastPageInRange = max($pages->pagesInRange); - - // Item numbers - if ($this->getCurrentItems() !== null) { - $pages->currentItemCount = $this->getCurrentItemCount(); - $pages->totalItemCount = $this->getTotalItemCount(); - $pages->firstItemNumber = $pages->totalItemCount - ? (($currentPageNumber - 1) * $pages->itemCountPerPage) + 1 - : 0; - $pages->lastItemNumber = $pages->totalItemCount - ? $pages->firstItemNumber + $pages->currentItemCount - 1 - : 0; - } - - return $pages; + return $items; } - /** - * Loads a scrolling style. - * - * @param string $scrollingStyle - * @return ScrollingStyleInterface - * @throws Exception\InvalidArgumentException - */ - protected function _loadScrollingStyle($scrollingStyle = null) - { - if ($scrollingStyle === null) { - $scrollingStyle = static::$defaultScrollingStyle; - } - - switch (strtolower(gettype($scrollingStyle))) { - case 'object': - if (!$scrollingStyle instanceof ScrollingStyleInterface) { - throw new Exception\InvalidArgumentException( - 'Scrolling style must implement Zend\Paginator\ScrollingStyle\ScrollingStyleInterface' - ); - } - - return $scrollingStyle; - - case 'string': - return static::getScrollingStylePluginManager()->get($scrollingStyle); - - case 'null': - // Fall through to default case - - default: - throw new Exception\InvalidArgumentException( - 'Scrolling style must be a class ' . - 'name or object implementing Zend\Paginator\ScrollingStyle\ScrollingStyleInterface' - ); - } - } -} +} \ No newline at end of file diff --git a/src/PaginatorInterface.php b/src/PaginatorInterface.php new file mode 100644 index 0000000..ce35833 --- /dev/null +++ b/src/PaginatorInterface.php @@ -0,0 +1,219 @@ +adapter = $adapter; + } elseif ($adapter instanceof AdapterAggregateInterface) { + $this->adapter = $adapter->getPaginatorAdapter(); + } else { + throw new Exception\InvalidArgumentException( + 'Zend\Paginator only accepts instances of the type ' . + 'Zend\Paginator\Adapter\AdapterInterface or Zend\Paginator\AdapterAggregateInterface.' + ); + } + + $config = static::$config; + + if (!empty($config)) { + $setupMethods = ['ItemCountPerPage', 'PageRange']; + + foreach ($setupMethods as $setupMethod) { + $key = strtolower($setupMethod); + $value = isset($config[$key]) ? $config[$key] : null; + + if ($value !== null) { + $setupMethod = 'set' . $setupMethod; + $this->$setupMethod($value); + } + } + } + } + + /** + * Returns the number of pages. + * + * @return int + */ + public function count() + { + if (!$this->pageCount) { + $this->pageCount = $this->_calculatePageCount(); + } + + return $this->pageCount; + } + + /** + * Returns the total number of items available. + * + * @return int + */ + public function getTotalItemCount() + { + return count($this->getAdapter()); + } + + /** + * Returns the absolute item number for the specified item. + * + * @param int $relativeItemNumber Relative item number + * @param int $pageNumber Page number + * @return int + */ + public function getAbsoluteItemNumber($relativeItemNumber, $pageNumber = null) + { + $relativeItemNumber = $this->normalizeItemNumber($relativeItemNumber); + + if ($pageNumber === null) { + $pageNumber = $this->getCurrentPageNumber(); + } + + $pageNumber = $this->normalizePageNumber($pageNumber); + + return (($pageNumber - 1) * $this->getItemCountPerPage()) + $relativeItemNumber; + } + + /** + * Returns the adapter. + * + * @return AdapterInterface + */ + public function getAdapter() + { + return $this->adapter; + } + + /** + * Returns the number of items for the current page. + * + * @return int + */ + public function getCurrentItemCount() + { + if ($this->currentItemCount === null) { + $this->currentItemCount = $this->getItemCount($this->getCurrentItems()); + } + + return $this->currentItemCount; + } + + /** + * Returns the items for the current page. + * + * @return Traversable + */ + public function getCurrentItems() + { + if ($this->currentItems === null) { + $this->currentItems = $this->getItemsByPage($this->getCurrentPageNumber()); + } + + return $this->currentItems; + } + + /** + * Returns the current page number. + * + * @return int + */ + public function getCurrentPageNumber() + { + return $this->normalizePageNumber($this->currentPageNumber); + } + + /** + * Sets the current page number. + * + * @param int $pageNumber Page number + * @return Paginator $this + */ + public function setCurrentPageNumber($pageNumber) + { + $this->currentPageNumber = (int) $pageNumber; + $this->currentItems = null; + $this->currentItemCount = null; + + return $this; + } + + /** + * Returns an item from a page. The current page is used if there's no + * page specified. + * + * @param int $itemNumber Item number (1 to itemCountPerPage) + * @param int $pageNumber + * @throws Exception\InvalidArgumentException + * @return mixed + */ + public function getItem($itemNumber, $pageNumber = null) + { + if ($pageNumber === null) { + $pageNumber = $this->getCurrentPageNumber(); + } elseif ($pageNumber < 0) { + $pageNumber = ($this->count() + 1) + $pageNumber; + } + + $page = $this->getItemsByPage($pageNumber); + $itemCount = $this->getItemCount($page); + + if ($itemCount == 0) { + throw new Exception\InvalidArgumentException('Page ' . $pageNumber . ' does not exist'); + } + + if ($itemNumber < 0) { + $itemNumber = ($itemCount + 1) + $itemNumber; + } + + $itemNumber = $this->normalizeItemNumber($itemNumber); + + if ($itemNumber > $itemCount) { + throw new Exception\InvalidArgumentException( + "Page {$pageNumber} does not contain item number {$itemNumber}" + ); + } + + return $page[$itemNumber - 1]; + } + + /** + * Returns the number of items per page. + * + * @return int + */ + public function getItemCountPerPage() + { + if (empty($this->itemCountPerPage)) { + $this->itemCountPerPage = static::getDefaultItemCountPerPage(); + } + + return $this->itemCountPerPage; + } + + /** + * Sets the number of items per page. + * + * @param int $itemCountPerPage + * @return Paginator $this + */ + public function setItemCountPerPage($itemCountPerPage = -1) + { + $this->itemCountPerPage = (int) $itemCountPerPage; + if ($this->itemCountPerPage < 1) { + $this->itemCountPerPage = $this->getTotalItemCount(); + } + $this->pageCount = $this->_calculatePageCount(); + $this->currentItems = null; + $this->currentItemCount = null; + + return $this; + } + + /** + * Returns the number of items in a collection. + * + * @param mixed $items Items + * @return int + */ + public function getItemCount($items) + { + $itemCount = 0; + + if (is_array($items) || $items instanceof Countable) { + $itemCount = count($items); + } elseif ($items instanceof Traversable) { // $items is something like LimitIterator + $itemCount = iterator_count($items); + } + + return $itemCount; + } + + /** + * Returns the items for a given page. + * + * @param int $pageNumber + * @return mixed + */ + public function getItemsByPage($pageNumber) + { + $pageNumber = $this->normalizePageNumber($pageNumber); + + $offset = ($pageNumber - 1) * $this->getItemCountPerPage(); + + $items = $this->adapter->getItems($offset, $this->getItemCountPerPage()); + + if (!$items instanceof Traversable) { + $items = new ArrayIterator($items); + } + + return $items; + } + + /** + * Returns a foreach-compatible iterator. + * + * @throws Exception\RuntimeException + * @return Traversable + */ + public function getIterator() + { + try { + return $this->getCurrentItems(); + } catch (\Exception $e) { + throw new Exception\RuntimeException('Error producing an iterator', null, $e); + } + } + + /** + * Returns the page range (see property declaration above). + * + * @return int + */ + public function getPageRange() + { + return $this->pageRange; + } + + /** + * Sets the page range (see property declaration above). + * + * @param int $pageRange + * @return Paginator $this + */ + public function setPageRange($pageRange) + { + $this->pageRange = (int) $pageRange; + + return $this; + } + + /** + * Returns the page collection. + * + * @param string $scrollingStyle Scrolling style + * @return \stdClass + */ + public function getPages($scrollingStyle = null) + { + if ($this->pages === null) { + $this->pages = $this->_createPages($scrollingStyle); + } + + return $this->pages; + } + + /** + * Returns a subset of pages within a given range. + * + * @param int $lowerBound Lower bound of the range + * @param int $upperBound Upper bound of the range + * @return array + */ + public function getPagesInRange($lowerBound, $upperBound) + { + $lowerBound = $this->normalizePageNumber($lowerBound); + $upperBound = $this->normalizePageNumber($upperBound); + + $pages = []; + + for ($pageNumber = $lowerBound; $pageNumber <= $upperBound; $pageNumber++) { + $pages[$pageNumber] = $pageNumber; + } + + return $pages; + } + + /** + * Brings the item number in range of the page. + * + * @param int $itemNumber + * @return int + */ + public function normalizeItemNumber($itemNumber) + { + $itemNumber = (int) $itemNumber; + + if ($itemNumber < 1) { + $itemNumber = 1; + } + + if ($itemNumber > $this->getItemCountPerPage()) { + $itemNumber = $this->getItemCountPerPage(); + } + + return $itemNumber; + } + + /** + * Brings the page number in range of the paginator. + * + * @param int $pageNumber + * @return int + */ + public function normalizePageNumber($pageNumber) + { + $pageNumber = (int) $pageNumber; + + if ($pageNumber < 1) { + $pageNumber = 1; + } + + $pageCount = $this->count(); + + if ($pageCount > 0 && $pageNumber > $pageCount) { + $pageNumber = $pageCount; + } + + return $pageNumber; + } + + /** + * Calculates the page count. + * + * @return int + */ + protected function _calculatePageCount() + { + return (int) ceil($this->getAdapter()->count() / $this->getItemCountPerPage()); + } + + /** + * Creates the page collection. + * + * @param string $scrollingStyle Scrolling style + * @return \stdClass + */ + protected function _createPages($scrollingStyle = null) + { + $pageCount = $this->count(); + $currentPageNumber = $this->getCurrentPageNumber(); + + $pages = new \stdClass(); + $pages->pageCount = $pageCount; + $pages->itemCountPerPage = $this->getItemCountPerPage(); + $pages->first = 1; + $pages->current = $currentPageNumber; + $pages->last = $pageCount; + + // Previous and next + if ($currentPageNumber - 1 > 0) { + $pages->previous = $currentPageNumber - 1; + } + + if ($currentPageNumber + 1 <= $pageCount) { + $pages->next = $currentPageNumber + 1; + } + + // Pages in range + $scrollingStyle = $this->_loadScrollingStyle($scrollingStyle); + $pages->pagesInRange = $scrollingStyle->getPages($this); + $pages->firstPageInRange = min($pages->pagesInRange); + $pages->lastPageInRange = max($pages->pagesInRange); + + // Item numbers + if ($this->getCurrentItems() !== null) { + $pages->currentItemCount = $this->getCurrentItemCount(); + $pages->totalItemCount = $this->getTotalItemCount(); + $pages->firstItemNumber = $pages->totalItemCount + ? (($currentPageNumber - 1) * $pages->itemCountPerPage) + 1 + : 0; + $pages->lastItemNumber = $pages->totalItemCount + ? $pages->firstItemNumber + $pages->currentItemCount - 1 + : 0; + } + + return $pages; + } + + /** + * Loads a scrolling style. + * + * @param string $scrollingStyle + * @return ScrollingStyleInterface + * @throws Exception\InvalidArgumentException + */ + protected function _loadScrollingStyle($scrollingStyle = null) + { + if ($scrollingStyle === null) { + $scrollingStyle = static::$defaultScrollingStyle; + } + + switch (strtolower(gettype($scrollingStyle))) { + case 'object': + if (!$scrollingStyle instanceof ScrollingStyleInterface) { + throw new Exception\InvalidArgumentException( + 'Scrolling style must implement Zend\Paginator\ScrollingStyle\ScrollingStyleInterface' + ); + } + + return $scrollingStyle; + + case 'string': + return static::getScrollingStylePluginManager()->get($scrollingStyle); + + case 'null': + // Fall through to default case + + default: + throw new Exception\InvalidArgumentException( + 'Scrolling style must be a class ' . + 'name or object implementing Zend\Paginator\ScrollingStyle\ScrollingStyleInterface' + ); + } + } +} From 4d7e3df021c9a29a0473dfe522fa08bbed466ae0 Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sun, 27 Dec 2015 18:34:46 +0900 Subject: [PATCH 05/15] oops, missing CachedTrait --- src/Paginator.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Paginator.php b/src/Paginator.php index f28a275..01e5708 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -17,7 +17,8 @@ class Paginator extends SimplePaginator { use JsonSerializeTrait, RenderTrait, - FilterTrait; + FilterTrait, + CachedTrait; /** * The cache tag prefix used to namespace Paginator results in the cache From aba0bcc5ad416cb05ffaf985107b74e559017cff Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sun, 27 Dec 2015 19:03:00 +0900 Subject: [PATCH 06/15] Move Global (static access) to GlobalPaginator --- src/GlobalPaginator.php | 249 +++++++++++++++++++++++++++++++++++ src/GlobalSetupInterface.php | 62 +++++++++ src/Paginator.php | 2 +- src/PaginatorInterface.php | 45 ------- src/SimplePaginator.php | 174 +----------------------- 5 files changed, 315 insertions(+), 217 deletions(-) create mode 100644 src/GlobalPaginator.php create mode 100644 src/GlobalSetupInterface.php diff --git a/src/GlobalPaginator.php b/src/GlobalPaginator.php new file mode 100644 index 0000000..da3b24f --- /dev/null +++ b/src/GlobalPaginator.php @@ -0,0 +1,249 @@ +$setupMethod($value); + } + } + } + } + + /** + * Returns the number of items per page. + * + * @return int + */ + public function getItemCountPerPage() + { + if (empty($this->itemCountPerPage)) { + $this->itemCountPerPage = static::getDefaultItemCountPerPage(); + } + + return $this->itemCountPerPage; + } + + /** + * Returns the page collection. + * + * @param string $scrollingStyle Scrolling style + * @return \stdClass + */ + public function getPages($scrollingStyle = null) + { + if ($this->pages === null) { + $this->pages = $this->_createPages($scrollingStyle); + } + + return $this->pages; + } + + /** + * Loads a scrolling style. + * + * @param string $scrollingStyle + * @return ScrollingStyleInterface + * @throws Exception\InvalidArgumentException + */ + protected function _loadScrollingStyle($scrollingStyle = null) + { + if ($scrollingStyle === null) { + $scrollingStyle = static::$defaultScrollingStyle; + } + + switch (strtolower(gettype($scrollingStyle))) { + case 'object': + if (!$scrollingStyle instanceof ScrollingStyleInterface) { + throw new Exception\InvalidArgumentException( + 'Scrolling style must implement Zend\Paginator\ScrollingStyle\ScrollingStyleInterface' + ); + } + + return $scrollingStyle; + + case 'string': + return static::getScrollingStylePluginManager()->get($scrollingStyle); + + case 'null': + // Fall through to default case + + default: + throw new Exception\InvalidArgumentException( + 'Scrolling style must be a class ' . + 'name or object implementing Zend\Paginator\ScrollingStyle\ScrollingStyleInterface' + ); + } + } +} \ No newline at end of file diff --git a/src/GlobalSetupInterface.php b/src/GlobalSetupInterface.php new file mode 100644 index 0000000..84fc7ae --- /dev/null +++ b/src/GlobalSetupInterface.php @@ -0,0 +1,62 @@ +$setupMethod($value); - } - } - } } /** @@ -406,10 +248,6 @@ public function getItem($itemNumber, $pageNumber = null) */ public function getItemCountPerPage() { - if (empty($this->itemCountPerPage)) { - $this->itemCountPerPage = static::getDefaultItemCountPerPage(); - } - return $this->itemCountPerPage; } @@ -513,11 +351,12 @@ public function setPageRange($pageRange) /** * Returns the page collection. * - * @param string $scrollingStyle Scrolling style + * @param ScrollingStyleInterface $scrollingStyle Scrolling style * @return \stdClass */ public function getPages($scrollingStyle = null) { + if ($this->pages === null) { $this->pages = $this->_createPages($scrollingStyle); } @@ -657,10 +496,6 @@ protected function _createPages($scrollingStyle = null) */ protected function _loadScrollingStyle($scrollingStyle = null) { - if ($scrollingStyle === null) { - $scrollingStyle = static::$defaultScrollingStyle; - } - switch (strtolower(gettype($scrollingStyle))) { case 'object': if (!$scrollingStyle instanceof ScrollingStyleInterface) { @@ -671,9 +506,6 @@ protected function _loadScrollingStyle($scrollingStyle = null) return $scrollingStyle; - case 'string': - return static::getScrollingStylePluginManager()->get($scrollingStyle); - case 'null': // Fall through to default case From b9aad1807371bbdc3bea2de349992f7aefe5ac6e Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sun, 27 Dec 2015 19:18:24 +0900 Subject: [PATCH 07/15] fix namespace requirement --- src/GlobalPaginator.php | 3 +++ src/SimplePaginator.php | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/GlobalPaginator.php b/src/GlobalPaginator.php index da3b24f..62ca32d 100644 --- a/src/GlobalPaginator.php +++ b/src/GlobalPaginator.php @@ -8,8 +8,11 @@ */ namespace Zend\Paginator; +use Traversable; use Zend\Paginator\Adapter\AdapterInterface; use Zend\ServiceManager\ServiceManager; +use Zend\Stdlib\ArrayUtils; +use Zend\Paginator\ScrollingStyle\ScrollingStyleInterface; class GlobalPaginator extends SimplePaginator implements GlobalSetupInterface { diff --git a/src/SimplePaginator.php b/src/SimplePaginator.php index 52180e0..941fba8 100644 --- a/src/SimplePaginator.php +++ b/src/SimplePaginator.php @@ -14,7 +14,6 @@ use Traversable; use Zend\Paginator\Adapter\AdapterInterface; use Zend\Paginator\ScrollingStyle\ScrollingStyleInterface; -use Zend\Stdlib\ArrayUtils; class SimplePaginator implements PaginatorInterface { From 3143850a55af5e2aaec98a7b2893afde96062019 Mon Sep 17 00:00:00 2001 From: sasezaki Date: Wed, 30 Dec 2015 16:40:46 +0900 Subject: [PATCH 08/15] remove duplicate method --- src/GlobalPaginator.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/GlobalPaginator.php b/src/GlobalPaginator.php index 62ca32d..005b00d 100644 --- a/src/GlobalPaginator.php +++ b/src/GlobalPaginator.php @@ -198,21 +198,6 @@ public function getItemCountPerPage() return $this->itemCountPerPage; } - /** - * Returns the page collection. - * - * @param string $scrollingStyle Scrolling style - * @return \stdClass - */ - public function getPages($scrollingStyle = null) - { - if ($this->pages === null) { - $this->pages = $this->_createPages($scrollingStyle); - } - - return $this->pages; - } - /** * Loads a scrolling style. * From dcacdab6702710151c9213c77a2d733765666b07 Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sat, 16 Jan 2016 02:41:29 +0900 Subject: [PATCH 09/15] fix namespace import --- src/PaginatorInterface.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/PaginatorInterface.php b/src/PaginatorInterface.php index 46ae4db..b177f28 100644 --- a/src/PaginatorInterface.php +++ b/src/PaginatorInterface.php @@ -9,7 +9,9 @@ namespace Zend\Paginator; +use Countable; use Traversable; +use IteratorAggregate; use Zend\Paginator\Adapter\AdapterInterface; interface PaginatorInterface extends Countable, IteratorAggregate From a20555d7e767a52100aa7d9bfe32d300f54b9dc3 Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sat, 16 Jan 2016 02:46:22 +0900 Subject: [PATCH 10/15] default itemCountPerPage must be null, when global count exists getItemCountPerPage() works with static::getDefaultItemCountPerPage(), lazy count-per-page check --- src/GlobalPaginator.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/GlobalPaginator.php b/src/GlobalPaginator.php index 005b00d..cf630ea 100644 --- a/src/GlobalPaginator.php +++ b/src/GlobalPaginator.php @@ -45,6 +45,15 @@ class GlobalPaginator extends SimplePaginator implements GlobalSetupInterface */ protected static $defaultItemCountPerPage = 10; + /** + * Number of items per page + * Override parent's class default number. + * When called getItemCountPerPage() method, $defaultItemCountPerPage will be used. + * + * @var int + */ + protected $itemCountPerPage = null; + /** * Scrolling style plugin manager * From 07f259af2524dbf4b6de47657dbbb02222ddc27c Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sat, 16 Jan 2016 02:58:44 +0900 Subject: [PATCH 11/15] Add doc-comment in JsonSerializeTrait::jsonSerialize() --- src/JsonSerializeTrait.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/JsonSerializeTrait.php b/src/JsonSerializeTrait.php index ca55622..c9612f4 100644 --- a/src/JsonSerializeTrait.php +++ b/src/JsonSerializeTrait.php @@ -16,6 +16,11 @@ trait JsonSerializeTrait { + /** + * Serializes the object as a string. Proxies to {@link toJson()}. + * + * @return string + */ public function jsonSerialize() { return $this->toJson(); From 26f3bd810ec421fc78444d05f557a3db82981ff5 Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sat, 16 Jan 2016 03:12:01 +0900 Subject: [PATCH 12/15] move stdlib dependency as dev Stdlib only require ArrayUtils::iteratorToArray() method. It used in GlobalPaginator::setGlobalConfig. --- composer.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 5a8b404..3ff283e 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,7 @@ } }, "require": { - "php": ">=5.5", - "zendframework/zend-stdlib": "dev-develop as 2.8.0" + "php": ">=5.5" }, "require-dev": { "zendframework/zend-cache": "dev-develop as 2.6.0", @@ -24,6 +23,7 @@ "zendframework/zend-json": "~2.5", "zendframework/zend-mvc": "dev-develop as 2.7.0", "zendframework/zend-servicemanager": "dev-develop as 2.7.0", + "zendframework/zend-stdlib": "dev-develop as 2.8.0", "zendframework/zend-view": "dev-develop as 2.6.0", "fabpot/php-cs-fixer": "1.7.*", "phpunit/PHPUnit": "~4.0" @@ -34,6 +34,7 @@ "zendframework/zend-filter": "Zend\\Filter component", "zendframework/zend-json": "Zend\\Json component", "zendframework/zend-servicemanager": "Zend\\ServiceManager component", + "zendframework/zend-stdlib": "Zend\\Stdlib component", "zendframework/zend-view": "Zend\\View component" }, "minimum-stability": "dev", From f1d496777640c73932b50c5014d6fc78f4712616 Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sat, 16 Jan 2016 03:33:13 +0900 Subject: [PATCH 13/15] TypeHint must be PaginatorInterface --- src/ScrollingStyle/All.php | 4 ++-- src/ScrollingStyle/Elastic.php | 4 ++-- src/ScrollingStyle/Jumping.php | 4 ++-- src/ScrollingStyle/ScrollingStyleInterface.php | 4 ++-- src/ScrollingStyle/Sliding.php | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ScrollingStyle/All.php b/src/ScrollingStyle/All.php index c7f44b8..4417524 100644 --- a/src/ScrollingStyle/All.php +++ b/src/ScrollingStyle/All.php @@ -9,7 +9,7 @@ namespace Zend\Paginator\ScrollingStyle; -use Zend\Paginator\Paginator; +use Zend\Paginator\PaginatorInterface; /** * A scrolling style that returns every page in the collection. @@ -25,7 +25,7 @@ class All implements ScrollingStyleInterface * @param int $pageRange Unused * @return array */ - public function getPages(Paginator $paginator, $pageRange = null) + public function getPages(PaginatorInterface $paginator, $pageRange = null) { return $paginator->getPagesInRange(1, $paginator->count()); } diff --git a/src/ScrollingStyle/Elastic.php b/src/ScrollingStyle/Elastic.php index 8183ae2..486cfbc 100644 --- a/src/ScrollingStyle/Elastic.php +++ b/src/ScrollingStyle/Elastic.php @@ -9,7 +9,7 @@ namespace Zend\Paginator\ScrollingStyle; -use Zend\Paginator\Paginator; +use Zend\Paginator\PaginatorInterface; /** * A Google-like scrolling style. Incrementally expands the range to about @@ -27,7 +27,7 @@ class Elastic extends Sliding * @param int $pageRange Unused * @return array */ - public function getPages(Paginator $paginator, $pageRange = null) + public function getPages(PaginatorInterface $paginator, $pageRange = null) { $pageRange = $paginator->getPageRange(); $pageNumber = $paginator->getCurrentPageNumber(); diff --git a/src/ScrollingStyle/Jumping.php b/src/ScrollingStyle/Jumping.php index f560c9b..547c7be 100644 --- a/src/ScrollingStyle/Jumping.php +++ b/src/ScrollingStyle/Jumping.php @@ -9,7 +9,7 @@ namespace Zend\Paginator\ScrollingStyle; -use Zend\Paginator\Paginator; +use Zend\Paginator\PaginatorInterface; /** * A scrolling style in which the cursor advances to the upper bound @@ -25,7 +25,7 @@ class Jumping implements ScrollingStyleInterface * @param int $pageRange Unused * @return array */ - public function getPages(Paginator $paginator, $pageRange = null) + public function getPages(PaginatorInterface $paginator, $pageRange = null) { $pageRange = $paginator->getPageRange(); $pageNumber = $paginator->getCurrentPageNumber(); diff --git a/src/ScrollingStyle/ScrollingStyleInterface.php b/src/ScrollingStyle/ScrollingStyleInterface.php index 3bf7c97..66b91ee 100644 --- a/src/ScrollingStyle/ScrollingStyleInterface.php +++ b/src/ScrollingStyle/ScrollingStyleInterface.php @@ -9,7 +9,7 @@ namespace Zend\Paginator\ScrollingStyle; -use Zend\Paginator\Paginator; +use Zend\Paginator\PaginatorInterface; interface ScrollingStyleInterface { @@ -20,5 +20,5 @@ interface ScrollingStyleInterface * @param int $pageRange (Optional) Page range * @return array */ - public function getPages(Paginator $paginator, $pageRange = null); + public function getPages(PaginatorInterface $paginator, $pageRange = null); } diff --git a/src/ScrollingStyle/Sliding.php b/src/ScrollingStyle/Sliding.php index 00ecf50..c2822c9 100644 --- a/src/ScrollingStyle/Sliding.php +++ b/src/ScrollingStyle/Sliding.php @@ -9,7 +9,7 @@ namespace Zend\Paginator\ScrollingStyle; -use Zend\Paginator\Paginator; +use Zend\Paginator\PaginatorInterface; /** * A Yahoo! Search-like scrolling style. The cursor will advance to @@ -28,7 +28,7 @@ class Sliding implements ScrollingStyleInterface * @param int $pageRange (Optional) Page range * @return array */ - public function getPages(Paginator $paginator, $pageRange = null) + public function getPages(PaginatorInterface $paginator, $pageRange = null) { if ($pageRange === null) { $pageRange = $paginator->getPageRange(); From 5ef2ea7c9859c11a8b278466ee86c15f0bbb44fa Mon Sep 17 00:00:00 2001 From: sasezaki Date: Sat, 16 Jan 2016 04:07:38 +0900 Subject: [PATCH 14/15] Apply php-cs-fixer fix invalid lines, remove useless `use` --- src/CachedTrait.php | 3 +-- src/FilterTrait.php | 2 +- src/GlobalPaginator.php | 2 +- src/GlobalSetupInterface.php | 5 +---- src/JsonSerializeTrait.php | 2 +- src/Paginator.php | 4 +--- src/PaginatorInterface.php | 2 +- src/RenderTrait.php | 3 +-- src/SimplePaginator.php | 1 - 9 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/CachedTrait.php b/src/CachedTrait.php index be92b77..e4e32be 100644 --- a/src/CachedTrait.php +++ b/src/CachedTrait.php @@ -144,5 +144,4 @@ protected function _getCacheInternalId() $this->getItemCountPerPage() ])); } - -} \ No newline at end of file +} diff --git a/src/FilterTrait.php b/src/FilterTrait.php index 7340b04..2f6b468 100644 --- a/src/FilterTrait.php +++ b/src/FilterTrait.php @@ -43,4 +43,4 @@ public function setFilter(FilterInterface $filter) return $this; } -} \ No newline at end of file +} diff --git a/src/GlobalPaginator.php b/src/GlobalPaginator.php index cf630ea..a2306b1 100644 --- a/src/GlobalPaginator.php +++ b/src/GlobalPaginator.php @@ -243,4 +243,4 @@ protected function _loadScrollingStyle($scrollingStyle = null) ); } } -} \ No newline at end of file +} diff --git a/src/GlobalSetupInterface.php b/src/GlobalSetupInterface.php index 84fc7ae..5f5fae9 100644 --- a/src/GlobalSetupInterface.php +++ b/src/GlobalSetupInterface.php @@ -9,7 +9,6 @@ namespace Zend\Paginator; - interface GlobalSetupInterface { /** @@ -57,6 +56,4 @@ public static function setScrollingStylePluginManager($scrollingAdapters); * @return ScrollingStylePluginManager */ public static function getScrollingStylePluginManager(); - - -} \ No newline at end of file +} diff --git a/src/JsonSerializeTrait.php b/src/JsonSerializeTrait.php index c9612f4..4909700 100644 --- a/src/JsonSerializeTrait.php +++ b/src/JsonSerializeTrait.php @@ -40,4 +40,4 @@ public function toJson() } return Json::encode($currentItems); } -} \ No newline at end of file +} diff --git a/src/Paginator.php b/src/Paginator.php index e59e12e..898ee80 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -11,7 +11,6 @@ use ArrayIterator; use Traversable; -use Zend\Stdlib\ArrayUtils; class Paginator extends GlobalPaginator { @@ -62,5 +61,4 @@ public function getItemsByPage($pageNumber) return $items; } - -} \ No newline at end of file +} diff --git a/src/PaginatorInterface.php b/src/PaginatorInterface.php index b177f28..c29da98 100644 --- a/src/PaginatorInterface.php +++ b/src/PaginatorInterface.php @@ -173,4 +173,4 @@ public function normalizeItemNumber($itemNumber); * @return int */ public function normalizePageNumber($pageNumber); -} \ No newline at end of file +} diff --git a/src/RenderTrait.php b/src/RenderTrait.php index 39c8400..79c7840 100644 --- a/src/RenderTrait.php +++ b/src/RenderTrait.php @@ -82,5 +82,4 @@ public function __toString() return ''; } - -} \ No newline at end of file +} diff --git a/src/SimplePaginator.php b/src/SimplePaginator.php index 941fba8..c391c6d 100644 --- a/src/SimplePaginator.php +++ b/src/SimplePaginator.php @@ -355,7 +355,6 @@ public function setPageRange($pageRange) */ public function getPages($scrollingStyle = null) { - if ($this->pages === null) { $this->pages = $this->_createPages($scrollingStyle); } From a98d72550fd69d8906e171d29a1495dded4011cc Mon Sep 17 00:00:00 2001 From: sasezaki Date: Thu, 21 Jan 2016 01:21:26 +0900 Subject: [PATCH 15/15] remove jsonSerialize() Don't add new method when Refactoring. (oops, jsonSerialize() introduced with my misunderstood usage.) --- src/JsonSerializeTrait.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/JsonSerializeTrait.php b/src/JsonSerializeTrait.php index 4909700..a80a9fd 100644 --- a/src/JsonSerializeTrait.php +++ b/src/JsonSerializeTrait.php @@ -16,16 +16,6 @@ trait JsonSerializeTrait { - /** - * Serializes the object as a string. Proxies to {@link toJson()}. - * - * @return string - */ - public function jsonSerialize() - { - return $this->toJson(); - } - /** * Returns the items of the current page as JSON. *