-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Query; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Pagination\PaginationData; | ||
use Kreyu\Bundle\DataTableBundle\Sorting\SortingData; | ||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
|
||
class ArrayProxyQuery implements ProxyQueryInterface | ||
{ | ||
private ?array $originalData = null; | ||
private ?array $sortedData = null; | ||
|
||
public function __construct( | ||
private array $data, | ||
private ?int $totalItemCount = null, | ||
) { | ||
$this->originalData = $this->data; | ||
$this->sortedData = $this->data; | ||
$this->totalItemCount ??= count($this->data); | ||
} | ||
|
||
public function sort(SortingData $sortingData): void | ||
{ | ||
$propertyAccessor = PropertyAccess::createPropertyAccessor(); | ||
|
||
$this->originalData ??= $this->data; | ||
|
||
$this->data = $this->originalData; | ||
|
||
usort($this->data, function ($a, $b) use ($sortingData, $propertyAccessor) { | ||
foreach ($sortingData->getColumns() as $sortingColumnData) { | ||
$propertyPath = $sortingColumnData->getPropertyPath(); | ||
$direction = $sortingColumnData->getDirection(); | ||
|
||
$valueA = $propertyAccessor->getValue($a, $propertyPath); | ||
$valueB = $propertyAccessor->getValue($b, $propertyPath); | ||
|
||
if ($valueA < $valueB) { | ||
return 'asc' === $direction ? -1 : 1; | ||
} elseif ($valueA > $valueB) { | ||
return 'asc' === $direction ? 1 : -1; | ||
} | ||
} | ||
|
||
return 0; | ||
}); | ||
|
||
$this->sortedData = $this->data; | ||
} | ||
|
||
public function paginate(PaginationData $paginationData): void | ||
{ | ||
$this->data = array_slice( | ||
$this->sortedData ?? $this->originalData, | ||
$paginationData->getOffset(), | ||
$paginationData->getPerPage(), | ||
); | ||
} | ||
|
||
public function getResult(): ResultSetInterface | ||
{ | ||
return new ResultSet( | ||
iterator: new \ArrayIterator($this->data), | ||
currentPageItemCount: count($this->data), | ||
totalItemCount: $this->totalItemCount, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Query; | ||
|
||
class ArrayProxyQueryFactory implements ProxyQueryFactoryInterface | ||
{ | ||
public function create(mixed $data): ProxyQueryInterface | ||
{ | ||
return new ArrayProxyQuery($data); | ||
} | ||
|
||
public function supports(mixed $data): bool | ||
{ | ||
return is_array($data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Query; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Query\ArrayProxyQuery; | ||
use Kreyu\Bundle\DataTableBundle\Query\ArrayProxyQueryFactory; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ArrayProxyQueryFactoryTest extends TestCase | ||
{ | ||
private ArrayProxyQueryFactory $factory; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->factory = new ArrayProxyQueryFactory(); | ||
} | ||
|
||
public function testCreate() | ||
{ | ||
$this->assertInstanceOf(ArrayProxyQuery::class, $this->factory->create([])); | ||
} | ||
|
||
#[DataProvider('provideSupportsCases')] | ||
public function testSupports(mixed $data, bool $expected) | ||
{ | ||
$this->assertEquals($expected, $this->factory->supports($data)); | ||
} | ||
|
||
public static function provideSupportsCases(): iterable | ||
{ | ||
yield 'array' => [[], true]; | ||
yield 'string' => ['', false]; | ||
yield 'integer' => [123, false]; | ||
yield 'bool' => [true, false]; | ||
yield 'null' => [null, false]; | ||
yield 'object' => [new \stdClass(), false]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Query; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Pagination\PaginationData; | ||
use Kreyu\Bundle\DataTableBundle\Query\ArrayProxyQuery; | ||
use Kreyu\Bundle\DataTableBundle\Sorting\SortingColumnData; | ||
use Kreyu\Bundle\DataTableBundle\Sorting\SortingData; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ArrayProxyQueryTest extends TestCase | ||
{ | ||
public function testGetResult() | ||
{ | ||
$query = new ArrayProxyQuery( | ||
data: [['foo' => 'bar'], ['bar' => 'baz']], | ||
); | ||
|
||
$result = $query->getResult(); | ||
|
||
$this->assertEquals( | ||
[['foo' => 'bar'], ['bar' => 'baz']], | ||
iterator_to_array($result->getIterator()), | ||
); | ||
|
||
$this->assertEquals(2, $result->count()); | ||
$this->assertEquals(2, $result->getTotalItemCount()); | ||
$this->assertEquals(2, $result->getCurrentPageItemCount()); | ||
} | ||
|
||
public function testGetResultWithTotalItemCountSet() | ||
{ | ||
$query = new ArrayProxyQuery( | ||
data: [['foo' => 'bar'], ['bar' => 'baz']], | ||
totalItemCount: 25, | ||
); | ||
|
||
$result = $query->getResult(); | ||
|
||
$this->assertEquals( | ||
[['foo' => 'bar'], ['bar' => 'baz']], | ||
iterator_to_array($result->getIterator()), | ||
); | ||
|
||
$this->assertEquals(2, $result->count()); | ||
$this->assertEquals(25, $result->getTotalItemCount()); | ||
$this->assertEquals(2, $result->getCurrentPageItemCount()); | ||
} | ||
|
||
public function testSort() | ||
{ | ||
$query = new ArrayProxyQuery( | ||
data: [['id' => 1], ['id' => 2], ['id' => 3]], | ||
); | ||
|
||
$query->sort(new SortingData([ | ||
new SortingColumnData('id', 'desc', '[id]'), | ||
])); | ||
|
||
$result = $query->getResult(); | ||
|
||
$this->assertEquals( | ||
[['id' => 3], ['id' => 2], ['id' => 1]], | ||
iterator_to_array($result->getIterator()), | ||
); | ||
} | ||
|
||
public function testPaginate() | ||
{ | ||
$query = new ArrayProxyQuery( | ||
data: [['id' => 1], ['id' => 2], ['id' => 3]], | ||
); | ||
|
||
$query->paginate(new PaginationData(page: 2, perPage: 1)); | ||
|
||
$result = $query->getResult(); | ||
|
||
$this->assertEquals([['id' => 2]], iterator_to_array($result->getIterator())); | ||
$this->assertEquals(1, $result->getCurrentPageItemCount()); | ||
$this->assertEquals(3, $result->getTotalItemCount()); | ||
} | ||
|
||
public function testSortAndPaginate() | ||
{ | ||
$query = new ArrayProxyQuery( | ||
data: [['id' => 1], ['id' => 2], ['id' => 3]], | ||
); | ||
|
||
$query->sort(new SortingData([ | ||
new SortingColumnData('id', 'desc', '[id]'), | ||
])); | ||
|
||
$query->paginate(new PaginationData(page: 3, perPage: 1)); | ||
|
||
$result = $query->getResult(); | ||
|
||
$this->assertEquals([['id' => 1]], iterator_to_array($result->getIterator())); | ||
$this->assertEquals(1, $result->getCurrentPageItemCount()); | ||
$this->assertEquals(3, $result->getTotalItemCount()); | ||
} | ||
} |