Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Refactored Zend\Paginator to remove front controller dependency
Browse files Browse the repository at this point in the history
- Also:
  - added explicit imports
  - cleaned up docblocks
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 66 deletions.
118 changes: 68 additions & 50 deletions src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,27 @@
*/
namespace Zend\Paginator;

use Zend\Controller\Front as FrontController,
Zend\View,
Zend\Json\Json;
use ArrayIterator,
Countable,
Iterator,
IteratorAggregate,
Traversable,
Zend\Cache\Frontend\Core as CacheCore,
Zend\Db\Select as DbSelect,
Zend\Db\Table\AbstractRowset as DbAbstractRowset,
Zend\Db\Table\Select as DbTableSelect,
Zend\Filter\Filter,
Zend\Json\Json,
Zend\Stdlib\IteratorToArray,
Zend\View;

/**
* @category Zend
* @package Zend_Paginator
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Paginator implements \Countable, \IteratorAggregate
class Paginator implements Countable, IteratorAggregate
{
/**
* Specifies that the factory should try to detect the proper adapter type first
Expand All @@ -58,7 +68,7 @@ class Paginator implements \Countable, \IteratorAggregate
/**
* Configuration file
*
* @var \Zend\Config\Config
* @var array|null
*/
protected static $_config = null;

Expand Down Expand Up @@ -86,7 +96,7 @@ class Paginator implements \Countable, \IteratorAggregate
/**
* Cache object
*
* @var \Zend\Cache\Frontend\Core
* @var CacheCore
*/
protected static $_cache;

Expand All @@ -100,7 +110,7 @@ class Paginator implements \Countable, \IteratorAggregate
/**
* Adapter
*
* @var \Zend\Paginator\Adapter
* @var Adapter
*/
protected $_adapter = null;

Expand Down Expand Up @@ -128,7 +138,7 @@ class Paginator implements \Countable, \IteratorAggregate
/**
* Result filter
*
* @var \Zend\Filter\Filter
* @var Filter
*/
protected $_filter = null;

Expand Down Expand Up @@ -164,7 +174,7 @@ class Paginator implements \Countable, \IteratorAggregate
/**
* View instance used for self rendering
*
* @var \Zend\View\Renderer
* @var View\Renderer
*/
protected $_view = null;

Expand All @@ -174,7 +184,7 @@ class Paginator implements \Countable, \IteratorAggregate
* @param mixed $data
* @param string $adapter
* @param array $prefixPaths
* @return \Zend\Paginator\Paginator
* @return Paginator
*/
public static function factory($data, $adapter = self::INTERNAL_ADAPTER,
array $prefixPaths = null)
Expand All @@ -186,11 +196,11 @@ public static function factory($data, $adapter = self::INTERNAL_ADAPTER,
if ($adapter == self::INTERNAL_ADAPTER) {
if (is_array($data)) {
$adapter = 'array';
} else if ($data instanceof \Zend\Db\Table\Select) {
} else if ($data instanceof DbTableSelect) {
$adapter = 'db_table_select';
} else if ($data instanceof \Zend\Db\Select) {
} else if ($data instanceof DbSelect) {
$adapter = 'db_select';
} else if ($data instanceof \Iterator) {
} else if ($data instanceof Iterator) {
$adapter = 'iterator';
} else if (is_integer($data)) {
$adapter = 'null';
Expand Down Expand Up @@ -248,21 +258,32 @@ public static function getAdapterBroker()
/**
* Set a global config
*
* @param \Zend\Config\Config $config
* @param array|Traversable $config
*/
public static function setConfig(\Zend\Config\Config $config)
public static function setConfig($config)
{
if ($config instanceof Traversable) {
$config = IteratorToArray::convert($config);
}
if (!is_array($config)) {
throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable');
}

self::$_config = $config;

if (null !== ($broker = $config->get('adapter_broker'))) {
if (isset($config['adapter_broker'])
&& null !== ($broker = $config['adapter_broker'])
) {
self::setAdapterBroker($broker);
}

if (null !== ($broker = $config->get('scrolling_style_broker'))) {
if (isset($config['scrolling_style_broker'])
&& null !== ($broker = $config['scrolling_style_broker'])
) {
self::setScrollingStyleBroker($broker);
}

$scrollingStyle = $config->get('scrolling_style');
$scrollingStyle = isset($config['scrolling_style']) ? $config['scrolling_style'] : null;

if ($scrollingStyle != null) {
self::setDefaultScrollingStyle($scrollingStyle);
Expand Down Expand Up @@ -302,9 +323,9 @@ public static function setDefaultItemCountPerPage($count)
/**
* Sets a cache object
*
* @param \Zend\Cache\Frontend\Core $cache
* @param CacheCore $cache
*/
public static function setCache(\Zend\Cache\Frontend\Core $cache)
public static function setCache(CacheCore $cache)
{
self::$_cache = $cache;
}
Expand Down Expand Up @@ -357,7 +378,7 @@ public static function getScrollingStyleBroker()
/**
* Constructor.
*
* @param Zend_Paginator_Adapter_Interface|\Zend\Paginator\AdapterAggregate $adapter
* @param Adapter|AdapterAggregate $adapter
*/
public function __construct($adapter)
{
Expand All @@ -374,11 +395,12 @@ public function __construct($adapter)

$config = self::$_config;

if ($config != null) {
if (!empty($config)) {
$setupMethods = array('ItemCountPerPage', 'PageRange');

foreach ($setupMethods as $setupMethod) {
$value = $config->get(strtolower($setupMethod));
$key = strtolower($setupMethod);
$value = isset($config[$key]) ? $config[$key] : null;

if ($value != null) {
$setupMethod = 'set' . $setupMethod;
Expand Down Expand Up @@ -409,7 +431,7 @@ public function __toString()
* Enables/Disables the cache for this instance
*
* @param bool $enable
* @return \Zend\Paginator\Paginator
* @return Paginator
*/
public function setCacheEnabled($enable)
{
Expand Down Expand Up @@ -445,7 +467,7 @@ public function getTotalItemCount()
* Clear the page item cache.
*
* @param int $pageNumber
* @return \Zend\Paginator\Paginator
* @return Paginator
*/
public function clearPageItemCache($pageNumber = null)
{
Expand Down Expand Up @@ -489,7 +511,7 @@ public function getAbsoluteItemNumber($relativeItemNumber, $pageNumber = null)
/**
* Returns the adapter.
*
* @return \Zend\Paginator\Adapter
* @return Adapter
*/
public function getAdapter()
{
Expand Down Expand Up @@ -538,7 +560,7 @@ public function getCurrentPageNumber()
* Sets the current page number.
*
* @param integer $pageNumber Page number
* @return \Zend\Paginator\Paginator $this
* @return Paginator $this
*/
public function setCurrentPageNumber($pageNumber)
{
Expand All @@ -552,7 +574,7 @@ public function setCurrentPageNumber($pageNumber)
/**
* Get the filter
*
* @return \Zend\Filter\Filter
* @return Filter
*/
public function getFilter()
{
Expand All @@ -562,10 +584,10 @@ public function getFilter()
/**
* Set a filter chain
*
* @param \Zend\Filter\Filter $filter
* @return \Zend\Paginator\Paginator
* @param Filter $filter
* @return Paginator
*/
public function setFilter(\Zend\Filter\Filter $filter)
public function setFilter(Filter $filter)
{
$this->_filter = $filter;

Expand Down Expand Up @@ -627,7 +649,7 @@ public function getItemCountPerPage()
* Sets the number of items per page.
*
* @param integer $itemCountPerPage
* @return \Zend\Paginator\Paginator $this
* @return Paginator $this
*/
public function setItemCountPerPage($itemCountPerPage = -1)
{
Expand All @@ -652,7 +674,7 @@ public function getItemCount($items)
{
$itemCount = 0;

if (is_array($items) || $items instanceof \Countable) {
if (is_array($items) || $items instanceof Countable) {
$itemCount = count($items);
} else { // $items is something like LimitIterator
$itemCount = iterator_count($items);
Expand Down Expand Up @@ -687,8 +709,8 @@ public function getItemsByPage($pageNumber)
$items = $filter->filter($items);
}

if (!$items instanceof \Traversable) {
$items = new \ArrayIterator($items);
if (!$items instanceof Traversable) {
$items = new ArrayIterator($items);
}

if ($this->_cacheEnabled()) {
Expand Down Expand Up @@ -726,7 +748,7 @@ public function getPageRange()
* Sets the page range (see property declaration above).
*
* @param integer $pageRange
* @return \Zend\Paginator\Paginator $this
* @return Paginator $this
*/
public function setPageRange($pageRange)
{
Expand Down Expand Up @@ -790,20 +812,16 @@ public function getPageItemCache()
}

/**
* Retrieves the view instance. If none registered, attempts to pull f
* rom ViewRenderer.
* Retrieves the view instance.
*
* @return \Zend\View\Renderer|null
* If none registered, instantiates a PhpRenderer instance.
*
* @return View\Renderer|null
*/
public function getView()
{
if ($this->_view === null) {
$front = FrontController::getInstance();
$viewRenderer = $front->getHelperBroker()->load('viewRenderer');
if ($viewRenderer->view === null) {
$viewRenderer->initView();
}
$this->_view = $viewRenderer->view;
$this->setView(new View\PhpRenderer());
}

return $this->_view;
Expand All @@ -812,8 +830,8 @@ public function getView()
/**
* Sets the view object.
*
* @param \Zend\View\Renderer $view
* @return \Zend\Paginator\Paginator
* @param View\Renderer $view
* @return Paginator
*/
public function setView(View\Renderer $view = null)
{
Expand Down Expand Up @@ -869,7 +887,7 @@ public function normalizePageNumber($pageNumber)
/**
* Renders the paginator.
*
* @param \Zend\View\Renderer $view
* @param View\Renderer $view
* @return string
*/
public function render(View\Renderer $view = null)
Expand All @@ -892,7 +910,7 @@ public function toJson()
{
$currentItems = $this->getCurrentItems();

if ($currentItems instanceof \Zend\Db\Table\AbstractRowset) {
if ($currentItems instanceof DbAbstractRowset) {
return Json::encode($currentItems->toArray());
} else {
return Json::encode($currentItems);
Expand Down Expand Up @@ -1003,7 +1021,7 @@ protected function _createPages($scrollingStyle = null)
* Loads a scrolling style.
*
* @param string $scrollingStyle
* @return \Zend\Paginator\ScrollingStyle
* @return ScrollingStyle
* @throws Exception\InvalidArgumentException
*/
protected function _loadScrollingStyle($scrollingStyle = null)
Expand Down
19 changes: 3 additions & 16 deletions test/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
*/
namespace ZendTest\Paginator;

use Zend\Paginator,
Zend\Controller\Front as FrontController,
use PHPUnit_Framework_TestCase as TestCase,
Zend\Paginator,
Zend\View\Helper,
Zend\View,
Zend\Config,
Expand All @@ -41,7 +41,7 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @group Zend_Paginator
*/
class PaginatorTest extends \PHPUnit_Framework_TestCase
class PaginatorTest extends TestCase
{
/**
* Paginator instance
Expand Down Expand Up @@ -76,10 +76,6 @@ protected function setUp()
$this->_paginator = Paginator\Paginator::factory($this->_testCollection);

$this->_config = new Config\Xml(__DIR__ . '/_files/config.xml');
// get a fresh new copy of ViewRenderer in each tests
$this->front = FrontController::getInstance();
$this->front->resetInstance();
$this->broker = $this->front->getHelperBroker();

$fO = array('lifetime' => 3600, 'automatic_serialization' => true);
$bO = array('cache_dir'=> $this->_getTmpDir());
Expand Down Expand Up @@ -515,14 +511,6 @@ public function testGetsItemCount()
$this->assertEquals(101, $this->_paginator->getItemCount($limitIterator));
}

public function testGetsViewFromViewRenderer()
{
$viewRenderer = $this->broker->load('viewRenderer');
$viewRenderer->setView(new View\PhpRenderer());

$this->assertInstanceOf('Zend\\View\\Renderer', $this->_paginator->getView());
}

public function testGeneratesViewIfNonexistent()
{
$this->assertInstanceOf('Zend\\View\\Renderer', $this->_paginator->getView());
Expand Down Expand Up @@ -803,7 +791,6 @@ public function testArrayAccessInClassSerializableLimitIterator()
$this->assertFalse(isset($items[2]));
$this->assertTrue(isset($items[1]));
$this->assertFalse(isset($items[3]));
$this->assertEquals(0, $items->key());
}
}

Expand Down

0 comments on commit 615f1cb

Please sign in to comment.