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

Create Callback adapter for Zend\Paginator #5272

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions library/Zend/Paginator/Adapter/Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Paginator\Adapter;

use Zend\Stdlib\CallbackHandler;

class Callback implements AdapterInterface
{
/**
* Callback to be runned to get the items for a page.
*
* @var CallbackHandler
*/
protected $itemsCallback;

/**
* Callback to be runned to get the total number of items.
*
* @var CallbackHandler
*/
protected $countCallback;

/**
* Constructs instance.
*
* @param CallbackHandler|callable $itemsCallback Callback to be runned to get the items for a page.
* @param CallbackHandler|callable $countCallback Callback to be runned to get the total number of items.
*/
public function __construct($itemsCallback, $countCallback)
{
if (! $itemsCallback instanceof CallbackHandler) {
$itemsCallback = new CallbackHandler($itemsCallback);
}

if (! $countCallback instanceof CallbackHandler) {
$countCallback = new CallbackHandler($countCallback);
}

$this->itemsCallback = $itemsCallback;
$this->countCallback = $countCallback;
}

/**
* Returns an array of items for a page.
*
* Runs the callback defined as first constructor's argument.
*
* @param int $offset Page offset
* @param int $itemCountPerPage Number of items per page
* @return array
*/
public function getItems($offset, $itemCountPerPage)
{
return $this->itemsCallback->call(array($offset, $itemCountPerPage));
}

/**
* Returns the total number of items.
*
* Runs the callback defined as second constructor's argument.
*
* @return int
*/
public function count()
{
return $this->countCallback->call();
}
}
81 changes: 81 additions & 0 deletions tests/ZendTest/Paginator/Adapter/CallbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Paginator\Adapter;

use Zend\Paginator\Adapter\Callback;
use Zend\Stdlib\CallbackHandler;

/**
* @group Zend_Paginator
*/
class CallbackTest extends \PHPUnit_Framework_TestCase
{
public function testMustDefineTwoCallbacksOnContructor()
{
$itemsCallback = new CallbackHandler(function () {
return array();
});
$countCallback = new CallbackHandler(function () {
return 0;
});
$adapter = new Callback($itemsCallback, $countCallback);

$this->assertAttributeSame($itemsCallback, 'itemsCallback', $adapter);
$this->assertAttributeSame($countCallback, 'countCallback', $adapter);
}

public function testShouldAcceptAnyCallableOnContructor()
{
$itemsCallback = function () {
return range(1, 10);
};
$countCallback = 'rand';
$adapter = new Callback($itemsCallback, $countCallback);

$this->assertAttributeInstanceOf('Zend\Stdlib\CallbackHandler', 'itemsCallback', $adapter);
$this->assertAttributeInstanceOf('Zend\Stdlib\CallbackHandler', 'countCallback', $adapter);
}

public function testMustRunItemCallbackToGetItems()
{
$data = range(1, 10);
$itemsCallback = function () use ($data) {
return $data;
};
$countCallback = function () {};
$adapter = new Callback($itemsCallback, $countCallback);

$this->assertSame($data, $adapter->getItems(0, 10));
}

public function testMustPassArgumentsToGetItemCallback()
{
$data = array(0, 1, 2, 3);
$itemsCallback = function ($offset, $itemCountPerPage) {
return range($offset, $itemCountPerPage);
};
$countCallback = function () {};
$adapter = new Callback($itemsCallback, $countCallback);

$this->assertSame($data, $adapter->getItems(0, 3));
}

public function testMustRunCountCallbackToCount()
{
$count = 1988;
$itemsCallback = function () {};
$countCallback = function () use ($count) {
return $count;
};
$adapter = new Callback($itemsCallback, $countCallback);

$this->assertSame($count, $adapter->count());
}
}