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

Commit

Permalink
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 448 deletions.
67 changes: 0 additions & 67 deletions src/AdapterPluginManager.php

This file was deleted.

88 changes: 1 addition & 87 deletions src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@
*/
class Paginator implements Countable, IteratorAggregate
{
/**
* Specifies that the factory should try to detect the proper adapter type first
*
* @var string
*/
const INTERNAL_ADAPTER = 'Zend\Paginator\Adapter\Internal';

/**
* The cache tag prefix used to namespace Paginator results in the cache
Expand Down Expand Up @@ -164,87 +158,13 @@ class Paginator implements Countable, IteratorAggregate
*/
protected $view = null;

/**
* Factory.
*
* @param mixed $data
* @param string $adapter
* @throws Exception\InvalidArgumentException
* @return Paginator
*/
public static function factory($data, $adapter = self::INTERNAL_ADAPTER)
{
if ($data instanceof AdapterAggregateInterface) {
return new self($data->getPaginatorAdapter());
}

if ($adapter == self::INTERNAL_ADAPTER) {
if (is_array($data)) {
$adapter = 'array';
} elseif ($data instanceof Sql\Select) {
$adapter = 'db_select';
} elseif ($data instanceof Iterator) {
$adapter = 'iterator';
} elseif (is_integer($data)) {
$adapter = 'null';
} else {
$type = (is_object($data)) ? get_class($data) : gettype($data);
throw new Exception\InvalidArgumentException('No adapter for type ' . $type);
}
}

$adapters = self::getAdapterPluginManager();
$adapter = $adapters->get($adapter, $data);
return new self($adapter);
}

/**
* Set the adapter plugin manager
*
* @param string|AdapterPluginManager $adapters
* @throws Exception\InvalidArgumentException
*/
public static function setAdapterPluginManager($adapters)
{
if (is_string($adapters)) {
if (!class_exists($adapters)) {
throw new Exception\InvalidArgumentException(sprintf(
'Unable to locate adapter plugin manager with class "%s"; class not found',
$adapters
));
}
$adapters = new $adapters();
}
if (!$adapters instanceof AdapterPluginManager) {
throw new Exception\InvalidArgumentException(sprintf(
'Pagination adapter manager must extend AdapterPluginManager; received "%s"',
(is_object($adapters) ? get_class($adapters) : gettype($adapters))
));
}
self::$adapters = $adapters;
}

/**
* Returns the adapter plugin manager. If it doesn't exist it's created.
*
* @return AdapterPluginManager
*/
public static function getAdapterPluginManager()
{
if (self::$adapters === null) {
self::setAdapterPluginManager(new AdapterPluginManager());
}

return self::$adapters;
}

/**
* Set a global config
*
* @param array|\Traversable $config
* @throws Exception\InvalidArgumentException
*/
public static function setOptions($config)
public static function setGlobalConfig($config)
{
if ($config instanceof Traversable) {
$config = ArrayUtils::iteratorToArray($config);
Expand All @@ -255,12 +175,6 @@ public static function setOptions($config)

self::$config = $config;

if (isset($config['adapter_plugins'])
&& null !== ($adapters = $config['adapter_plugins'])
) {
self::setAdapterPluginManager($adapters);
}

if (isset($config['scrolling_style_plugins'])
&& null !== ($adapters = $config['scrolling_style_plugins'])
) {
Expand Down
16 changes: 8 additions & 8 deletions test/Adapter/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,42 @@ class ArrayTest extends \PHPUnit_Framework_TestCase
/**
* @var Zend\Paginator\Adapter\Array
*/
private $_adapter;
private $adapter;

/**
* Prepares the environment before running a test.
*/
protected function setUp ()
{
parent::setUp();
$this->_adapter = new Adapter\ArrayAdapter(range(1, 101));
$this->adapter = new Adapter\ArrayAdapter(range(1, 101));
}
/**
* Cleans up the environment after running a test.
*/
protected function tearDown ()
{
$this->_adapter = null;
$this->adapter = null;
parent::tearDown();
}

public function testGetsItemsAtOffsetZero()
{
$expected = range(1, 10);
$actual = $this->_adapter->getItems(0, 10);
$actual = $this->adapter->getItems(0, 10);
$this->assertEquals($expected, $actual);
}

public function testGetsItemsAtOffsetTen()
{
$expected = range(11, 20);
$actual = $this->_adapter->getItems(10, 10);
$actual = $this->adapter->getItems(10, 10);
$this->assertEquals($expected, $actual);
}

public function testReturnsCorrectCount()
{
$this->assertEquals(101, $this->_adapter->count());
$this->assertEquals(101, $this->adapter->count());
}


Expand All @@ -67,8 +67,8 @@ public function testReturnsCorrectCount()
*/
public function testEmptySet()
{
$this->_adapter = new Adapter\ArrayAdapter(array());
$actual = $this->_adapter->getItems(0, 10);
$this->adapter = new Adapter\ArrayAdapter(array());
$actual = $this->adapter->getItems(0, 10);
$this->assertEquals(array(), $actual);
}
}
23 changes: 12 additions & 11 deletions test/Adapter/IteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace ZendTest\Paginator\Adapter;

use Zend\Paginator\Adapter;
use Zend\Paginator\Paginator;
use Zend\Paginator\Exception;

/**
Expand All @@ -24,7 +25,7 @@ class IteratorTest extends \PHPUnit_Framework_TestCase
/**
* @var \Zend\Paginator\Adapter\Iterator
*/
private $_adapter;
private $adapter;

/**
* Prepares the environment before running a test.
Expand All @@ -33,20 +34,20 @@ protected function setUp ()
{
parent::setUp();
$iterator = new \ArrayIterator(range(1, 101));
$this->_adapter = new Adapter\Iterator($iterator);
$this->adapter = new Adapter\Iterator($iterator);
}
/**
* Cleans up the environment after running a test.
*/
protected function tearDown ()
{
$this->_adapter = null;
$this->adapter = null;
parent::tearDown();
}

public function testGetsItemsAtOffsetZero()
{
$actual = $this->_adapter->getItems(0, 10);
$actual = $this->adapter->getItems(0, 10);
$this->assertInstanceOf('LimitIterator', $actual);

$i = 1;
Expand All @@ -58,7 +59,7 @@ public function testGetsItemsAtOffsetZero()

public function testGetsItemsAtOffsetTen()
{
$actual = $this->_adapter->getItems(10, 10);
$actual = $this->adapter->getItems(10, 10);
$this->assertInstanceOf('LimitIterator', $actual);

$i = 11;
Expand All @@ -70,7 +71,7 @@ public function testGetsItemsAtOffsetTen()

public function testReturnsCorrectCount()
{
$this->assertEquals(101, $this->_adapter->count());
$this->assertEquals(101, $this->adapter->count());
}

public function testThrowsExceptionIfNotCountable()
Expand All @@ -86,8 +87,8 @@ public function testThrowsExceptionIfNotCountable()
*/
public function testDoesNotThrowOutOfBoundsExceptionIfIteratorIsEmpty()
{
$this->_paginator = \Zend\Paginator\Paginator::factory(new \ArrayIterator(array()));
$items = $this->_paginator->getCurrentItems();
$this->paginator = new Paginator(new Adapter\Iterator(new \ArrayIterator(array())));
$items = $this->paginator->getCurrentItems();

foreach ($items as $item);
}
Expand All @@ -97,7 +98,7 @@ public function testDoesNotThrowOutOfBoundsExceptionIfIteratorIsEmpty()
*/
public function testGetItemsSerializable()
{
$items = $this->_adapter->getItems(0, 1);
$items = $this->adapter->getItems(0, 1);
$innerIterator = $items->getInnerIterator();
$items = unserialize(serialize($items));
$this->assertTrue( ($items->getInnerIterator() == $innerIterator), 'getItems has to be serializable to use caching');
Expand All @@ -109,8 +110,8 @@ public function testGetItemsSerializable()
public function testEmptySet()
{
$iterator = new \ArrayIterator(array());
$this->_adapter = new Adapter\Iterator($iterator);
$actual = $this->_adapter->getItems(0, 10);
$this->adapter = new Adapter\Iterator($iterator);
$actual = $this->adapter->getItems(0, 10);
$this->assertEquals(array(), $actual);
}
}
Loading

0 comments on commit 27f9b6d

Please sign in to comment.