-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
add mapping adapter #195
Changes from 4 commits
d1472d7
bbb714f
4f44c6f
aa347a2
a80d8d5
ea12155
09305ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you mean |
||
} | ||
|
||
$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)); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing new line after this |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
} | ||
} |
There was a problem hiding this comment.
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