Skip to content

add mapping adapter #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,26 @@ $results = array(/* ... */);
$adapter = new FixedAdapter($nbResults, $results);
```

### MappingAdapter

This adapter allows you to define a callback to be applied to all items coming out of the adapter.
Useful when you need to do something simple to the items that you only need to do when they
are paginated.

```php
<?php

use Pagerfanta\Adapter\MappingAdapter;

$results = array(/* ... */);

$adapter = new MappingAdapter(new ArrayAdapter($results), function (array $items) {
return array_map(function ($item) {
return $item + 1;
}, $items);
});
```

## Views

Views are to render pagerfantas, this way you can reuse your
Expand Down
48 changes: 48 additions & 0 deletions src/Pagerfanta/Adapter/MappingAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Pagerfanta package.
*
* (c) Pablo Díez <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pagerfanta\Adapter;

class MappingAdapter implements AdapterInterface
{
private $innerAdapter;
private $callback;

/**
* @param AdapterInterface $innerAdapter
* @param callable $callback
*/
public function __construct(AdapterInterface $innerAdapter, $callback)
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException('$callback must be callable');
}

$this->innerAdapter = $innerAdapter;
$this->callback = $callback;
}

/**
* {@inheritdoc}
*/
public function getNbResults()
{
return $this->innerAdapter->getNbResults();
}

/**
* {@inheritdoc}
*/
public function getSlice($offset, $length)
{
return call_user_func($this->callback, $this->innerAdapter->getSlice($offset, $length));
}
}
72 changes: 72 additions & 0 deletions tests/Pagerfanta/Tests/Adapter/MappingAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the Pagerfanta package.
*
* (c) Pablo Díez <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pagerfanta\Tests\Adapter;

use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Adapter\MappingAdapter;

class MappingAdapterTest extends \PHPUnit_Framework_TestCase
{
/** @var MappingAdapter */
protected $adapter;

public function setUp()
{
$this->adapter = new MappingAdapter(new ArrayAdapter(array(
34,
45,
67,
12
)), function (array $items) {
return array_map(
function ($item) {
return $item + 1;
},
$items
);
});
}

public function testGetNbResults()
{
$this->assertEquals(4, $this->adapter->getNbResults());
}

public function testGetSlice()
{
$this->assertEquals(array(
35,
46,
68,
13
), $this->adapter->getSlice(0, 34));
$this->assertEquals(array(
35,
46,
), $this->adapter->getSlice(0, 2));
$this->assertEquals(array(
68,
13,
), $this->adapter->getSlice(2, 5));
$this->assertEquals(array(
68,
), $this->adapter->getSlice(2, 1));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testConstructorWithNonCallableForCallback()
{
new MappingAdapter(new ArrayAdapter(array()), 'bla');
}
}