Skip to content

Commit

Permalink
feat: friendly error message if the underlying collection's item is n…
Browse files Browse the repository at this point in the history
…ot an array or object (#79)
  • Loading branch information
priyadi authored Jun 17, 2024
1 parent 8dfdcbf commit fd48fbb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* test: add `UnsupportedCollectionTest`
* fix: improve error message if a property does not exist or the value is null
* feat: friendly error message if the underlying collection's item is not an array or object

# 0.11.0

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/rekapager package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Rekapager\Doctrine\Collections\Exception;

use Rekalogika\Contracts\Rekapager\Exception\UnexpectedValueException;

class UnsupportedCollectionItemException extends UnexpectedValueException
{
public function __construct(string $type, \Throwable $previous)
{
parent::__construct(sprintf('Unsupported collection type. The items in the collection must be objects or arrays, an %s was given.', $type), 0, $previous);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Order;
use Doctrine\Common\Collections\Selectable;
use Rekalogika\Rekapager\Doctrine\Collections\Exception\UnsupportedCollectionItemException;
use Rekalogika\Rekapager\Doctrine\Collections\Internal\SelectableKeysetItem;
use Rekalogika\Rekapager\Keyset\Contracts\BoundaryType;
use Rekalogika\Rekapager\Keyset\KeysetPaginationAdapterInterface;
Expand Down Expand Up @@ -72,7 +73,15 @@ public function getOffsetItems(int $offset, int $limit): array
->setFirstResult($offset)
->setMaxResults($limit);

return $this->collection->matching($criteria)->toArray();
try {
return $this->collection->matching($criteria)->toArray();
} catch (\TypeError $e) {
if (preg_match('|ClosureExpressionVisitor::getObjectFieldValue\(\): Argument \#1 \(\$object\) must be of type object\|array, (\S+) given|', $e->getMessage(), $matches)) {
throw new UnsupportedCollectionItemException($matches[1], $e);
}
throw $e;

}
}

public function countOffsetItems(int $offset = 0, ?int $limit = null): int
Expand Down Expand Up @@ -256,7 +265,15 @@ public function getKeysetItems(
): array {
$criteria = $this->getCriteria($offset, $limit, $boundaryValues, $boundaryType);

$items = $this->collection->matching($criteria)->toArray();
try {
$items = $this->collection->matching($criteria)->toArray();
} catch (\TypeError $e) {
if (preg_match('|ClosureExpressionVisitor::getObjectFieldValue\(\): Argument \#1 \(\$object\) must be of type object\|array, (\S+) given|', $e->getMessage(), $matches)) {
throw new UnsupportedCollectionItemException($matches[1], $e);
}
throw $e;

}

if ($boundaryType === BoundaryType::Upper) {
$items = array_reverse($items);
Expand Down
12 changes: 3 additions & 9 deletions tests/src/UnitTests/UnsupportedCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,31 @@
namespace Rekalogika\Rekapager\Tests\UnitTests;

use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\TestCase;
use Rekalogika\Rekapager\Doctrine\Collections\Exception\UnsupportedCollectionItemException;
use Rekalogika\Rekapager\Doctrine\Collections\SelectableAdapter;
use Rekalogika\Rekapager\Keyset\KeysetPageable;
use Rekalogika\Rekapager\Offset\OffsetPageable;

class UnsupportedCollectionTest extends TestCase
{
/**
* @todo Fix after the resolution of https://github.com/doctrine/collections/pull/421
*/
#[RequiresPhp('99999')]
public function testCollectionOfScalarWithKeysetPagination(): void
{
$collection = new ArrayCollection([1, 2, 3, 4, 5]);
$adapter = new SelectableAdapter($collection);
$pageable = new KeysetPageable($adapter, 2);

$this->expectException(UnsupportedCollectionItemException::class);
foreach ($pageable->getFirstPage() as $item);
}

/**
* @todo Fix after the resolution of https://github.com/doctrine/collections/pull/421
*/
#[RequiresPhp('99999')]
public function testCollectionOfScalarWithOffsetPagination(): void
{
$collection = new ArrayCollection([1, 2, 3, 4, 5]);
$adapter = new SelectableAdapter($collection);
$pageable = new OffsetPageable($adapter, 2);

$this->expectException(UnsupportedCollectionItemException::class);
foreach ($pageable->getFirstPage() as $item);
}
}

0 comments on commit fd48fbb

Please sign in to comment.