Skip to content
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

add mapping adapter #195

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

### MappingAdapter

This adapter allows you to define a callback t be applied to all items coming out of the adapter.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo here: callback t => callback to

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 ($item) {
return $item + 1;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs should be updated after your last changes

});
```

## Views

Views are to render pagerfantas, this way you can reuse your
Expand Down
49 changes: 49 additions & 0 deletions src/Pagerfanta/Adapter/MappingAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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
{
protected $innerAdapter;
protected $callback;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest making these private. It makes maintenance much easier to deal with semver (protected stuff are subject to backward compatibility too)


/**
* MappingAdapter constructor.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line does not provide any value

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean $callback must be callback here?

}

$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));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing new line after this

73 changes: 73 additions & 0 deletions tests/Pagerfanta/Tests/Adapter/MappingAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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 Omni\CommonBundle\Tests\Pagerfanta;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong namespace


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');
}
}