Skip to content

Commit

Permalink
added pagination in ArrayDataProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
pfilsx committed Apr 25, 2019
1 parent 8c4d59d commit 6996376
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Grid/Columns/ActionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function generateUrl($item, $action)
} elseif (!empty($this->identifier) && $item->has($this->identifier)) {
return $this->container['router']->generate($this->pathPrefix . $action, ['id' => $item->get($this->identifier)]);
} elseif ($item->has('id')) {
return $this->container['router']->generate($this->pathPrefix . $action, ['id' => $item->id]);
return $this->container['router']->generate($this->pathPrefix . $action, ['id' => $item->get('id')]);
} else {
throw new DataGridException('Could not generate url for action: ' . $action);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Grid/DataGridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Pfilsx\DataGrid\Grid\Columns\AbstractColumn;
use Pfilsx\DataGrid\Grid\Columns\DataColumn;
use InvalidArgumentException;
use Pfilsx\DataGrid\Grid\Providers\DataProvider;
use Pfilsx\DataGrid\Grid\Providers\DataProviderInterface;

class DataGridBuilder implements DataGridBuilderInterface
Expand All @@ -21,7 +20,7 @@ class DataGridBuilder implements DataGridBuilderInterface
*/
protected $container;
/**
* @var DataProvider
* @var DataProviderInterface
*/
protected $provider;

Expand Down Expand Up @@ -143,7 +142,7 @@ public function getOptions(): array
return $this->options;
}

public function getProvider(): DataProvider
public function getProvider(): DataProviderInterface
{
return $this->provider;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Grid/DataGridBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Pfilsx\DataGrid\Grid;


use Pfilsx\DataGrid\Grid\Providers\DataProvider;
use Pfilsx\DataGrid\Grid\Providers\DataProviderInterface;

interface DataGridBuilderInterface
Expand Down Expand Up @@ -36,7 +35,7 @@ public function getColumns(): array;
*/
public function getOptions(): array;

public function getProvider(): DataProvider;
public function getProvider(): DataProviderInterface;

/**
* @internal
Expand Down
3 changes: 1 addition & 2 deletions src/Grid/DataGridFiltersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Pfilsx\DataGrid\Grid;


use Pfilsx\DataGrid\Grid\Providers\DataProvider;
use Pfilsx\DataGrid\Grid\Providers\DataProviderInterface;

class DataGridFiltersBuilder implements DataGridFiltersBuilderInterface
Expand All @@ -14,7 +13,7 @@ class DataGridFiltersBuilder implements DataGridFiltersBuilderInterface
*/
protected $params = [];
/**
* @var DataProvider
* @var DataProviderInterface
*/
protected $provider;

Expand Down
3 changes: 3 additions & 0 deletions src/Grid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function setOptions(array $options)
}
}

/**
* @return int
*/
public function getFirst()
{
return ($this->page - 1) * $this->limit;
Expand Down
7 changes: 5 additions & 2 deletions src/Grid/Providers/ArrayDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public function __construct(array $data)

public function getItems(): array
{
if ($this->getPager()->isEnabled()){
$this->data = array_slice($this->data, $this->getPager()->getFirst(), $this->getPager()->getLimit());
}
return array_map(function ($row) {
$item = new DataGridItem();
$item->setRow($row);
Expand Down Expand Up @@ -82,13 +85,13 @@ protected function buildCompare($attribute, $order)
if ($attrValueA == $attrValueB) {
return 0;
}
if (($type1 = gettype($attrValueA)) != ($type2 = gettype($attrValueB))) {
if (($type1 = gettype($attrValueA)) != gettype($attrValueB)) {
return 0;
}
if ($type1 == 'string') {
return $order == 'ASC' ? strcmp($attrValueA, $attrValueB) : -strcmp($attrValueA, $attrValueB);
}
return $attrValueA < $attrValueB ? ($order == 'ASC' ? -1 : 1) : ($order == 'ASC' ? 1 : -1);
return $order == 'ASC' ? $attrValueA <=> $attrValueB : $attrValueB <=> $attrValueA;
};
}

Expand Down
24 changes: 15 additions & 9 deletions src/Grid/Providers/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use Doctrine\ORM\QueryBuilder;
use Pfilsx\DataGrid\DataGridException;
use Pfilsx\DataGrid\Grid\Pager;
use Symfony\Component\Lock\Exception\NotSupportedException;

abstract class DataProvider implements DataProviderInterface
{
/**
* @var Pager
*/
protected $pager;

protected $countFieldName;
Expand Down Expand Up @@ -49,22 +51,22 @@ public function getCountFieldName(): string

public function addEqualFilter(string $attribute, $value): DataProviderInterface
{
throw new NotSupportedException("Method addEqualFilter() is not supported in " . static::class);
throw new DataGridException("Method addEqualFilter() is not supported in " . static::class);
}

public function addLikeFilter(string $attribute, $value): DataProviderInterface
{
throw new NotSupportedException("Method addLikeFilter() is not supported in " . static::class);
throw new DataGridException("Method addLikeFilter() is not supported in " . static::class);
}

public function addRelationFilter(string $attribute, $value, string $relationClass): DataProviderInterface
{
throw new NotSupportedException("Method addRelationFilter() is not supported in " . static::class);
throw new DataGridException("Method addRelationFilter() is not supported in " . static::class);
}

public function addCustomFilter(string $attribute, $value, callable $callback): DataProviderInterface
{
throw new NotSupportedException("Method addCustomFilter() is not supported in " . static::class);
throw new DataGridException("Method addCustomFilter() is not supported in " . static::class);
}

public function addDateFilter(string $attribute, $value, string $comparison = 'equal'): DataProviderInterface
Expand All @@ -78,18 +80,22 @@ public function addDateFilter(string $attribute, $value, string $comparison = 'e
return $this;
}

/**
* @param $attribute
* @param $value
*/
protected function equalDate($attribute, $value): void
{
throw new NotSupportedException("Method equalDate() is not supported in " . static::class);
throw new DataGridException("Method equalDate() is not supported in " . static::class);
}


public static function create($data, EntityManager $doctrine = null): DataProviderInterface
{
if ($data instanceof EntityRepository) {
if ($data instanceof EntityRepository && $doctrine !== null) {
return new RepositoryDataProvider($data, $doctrine);
}
if ($data instanceof QueryBuilder) {
if ($data instanceof QueryBuilder && $doctrine !== null) {
return new QueryBuilderDataProvider($data, $doctrine);
}
if (is_array($data)) {
Expand All @@ -101,4 +107,4 @@ public static function create($data, EntityManager $doctrine = null): DataProvid
'Array'
]) . ', ' . (($type = gettype($data)) == 'object' ? get_class($data) : $type) . ' given');
}
}
}
3 changes: 3 additions & 0 deletions tests/providers/ArrayDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateTime;
use Pfilsx\DataGrid\Grid\DataGridItem;
use Pfilsx\DataGrid\Grid\Pager;
use Pfilsx\DataGrid\Grid\Providers\ArrayDataProvider;
use Pfilsx\DataGrid\Grid\Providers\DataProvider;
use PHPUnit\Framework\TestCase;
Expand All @@ -28,6 +29,7 @@ protected function setUp(): void
$itemsSets[2],
$itemsSets[4]
]);
$this->provider->setPager(new Pager());
}

public function testType(): void
Expand Down Expand Up @@ -113,6 +115,7 @@ public function testWrongTypeDateFilter($comparer): void
'created' => 1
]
]);
$provider->setPager(new Pager());
$this->assertCount(0, $provider->addDateFilter('created', '01.01.1990', $comparer)->getItems());
}

Expand Down

0 comments on commit 6996376

Please sign in to comment.