| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Ray\AuraSqlModule\Pagerfanta; |
||
| 6 | |||
| 7 | use Aura\Sql\ExtendedPdoInterface; |
||
| 8 | use Override; |
||
| 9 | use Pagerfanta\Exception\LogicException; |
||
| 10 | use Pagerfanta\Pagerfanta; |
||
| 11 | use Pagerfanta\View\ViewInterface; |
||
| 12 | use Ray\AuraSqlModule\Annotation\PagerViewOption; |
||
| 13 | use Ray\AuraSqlModule\Exception\NotInitialized; |
||
| 14 | // phpcs:ignore SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse |
||
| 15 | use ReturnTypeWillChange; |
||
|
0 ignored issues
–
show
|
|||
| 16 | |||
| 17 | use function assert; |
||
| 18 | use function class_exists; |
||
| 19 | |||
| 20 | /** @template T */ |
||
| 21 | final class AuraSqlPager implements AuraSqlPagerInterface |
||
| 22 | { |
||
| 23 | private ?RouteGeneratorInterface $routeGenerator = null; |
||
| 24 | private ExtendedPdoInterface $pdo; |
||
| 25 | private string $sql; |
||
| 26 | private ?string $entity = null; |
||
| 27 | |||
| 28 | /** @var array<mixed> */ |
||
| 29 | private array $params; |
||
| 30 | |||
| 31 | /** @phpstan-var positive-int */ |
||
| 32 | private int $paging; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param array<string, mixed> $viewOptions |
||
| 36 | * |
||
| 37 | * @PagerViewOption("viewOptions") |
||
| 38 | */ |
||
| 39 | #[PagerViewOption('viewOptions')] |
||
| 40 | public function __construct(private readonly ViewInterface $view, private readonly array $viewOptions) |
||
| 41 | { |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritDoc} |
||
| 46 | * |
||
| 47 | * @phpstan-param positive-int $paging |
||
| 48 | */ |
||
| 49 | #[Override] |
||
| 50 | public function init(ExtendedPdoInterface $pdo, $sql, array $params, $paging, RouteGeneratorInterface $routeGenerator, ?string $entity = null): void |
||
| 51 | { |
||
| 52 | $this->pdo = $pdo; |
||
| 53 | $this->sql = $sql; |
||
| 54 | $this->params = $params; |
||
| 55 | $this->paging = $paging; |
||
| 56 | $this->routeGenerator = $routeGenerator; |
||
| 57 | $this->entity = $entity; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritDoc} |
||
| 62 | */ |
||
| 63 | #[Override] |
||
| 64 | #[ReturnTypeWillChange] |
||
| 65 | public function offsetExists($offset): bool |
||
| 66 | { |
||
| 67 | throw new LogicException('unsupported'); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritDoc} |
||
| 72 | * |
||
| 73 | * @phpstan-param positive-int $currentPage |
||
| 74 | */ |
||
| 75 | #[Override] |
||
| 76 | public function offsetGet($currentPage): Page |
||
| 77 | { |
||
| 78 | if (! $this->routeGenerator instanceof RouteGeneratorInterface) { |
||
| 79 | throw new NotInitialized(); |
||
| 80 | } |
||
| 81 | |||
| 82 | $pagerfanta = new Pagerfanta($this->getPdoAdapter()); |
||
| 83 | $pagerfanta->setMaxPerPage($this->paging); |
||
| 84 | $pagerfanta->setCurrentPage($currentPage); |
||
| 85 | $page = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions); |
||
| 86 | $page->maxPerPage = $pagerfanta->getMaxPerPage(); |
||
| 87 | $page->current = $pagerfanta->getCurrentPage(); |
||
| 88 | /** @psalm-suppress UndefinedDocblockClass */ |
||
| 89 | $page->hasNext = (bool) $pagerfanta->hasNextPage(); |
||
| 90 | /** @psalm-suppress UndefinedDocblockClass */ |
||
| 91 | $page->hasPrevious = $pagerfanta->hasPreviousPage(); |
||
| 92 | $page->data = $pagerfanta->getCurrentPageResults(); |
||
| 93 | $page->total = $pagerfanta->getNbResults(); |
||
| 94 | |||
| 95 | return $page; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritDoc} |
||
| 100 | */ |
||
| 101 | #[Override] |
||
| 102 | public function offsetSet($offset, $value): void |
||
| 103 | { |
||
| 104 | throw new LogicException('read only'); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritDoc} |
||
| 109 | */ |
||
| 110 | #[Override] |
||
| 111 | public function offsetUnset($offset): void |
||
| 112 | { |
||
| 113 | throw new LogicException('read only'); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** @return ExtendedPdoAdapter<T> */ |
||
| 117 | private function getPdoAdapter(): ExtendedPdoAdapter |
||
| 118 | { |
||
| 119 | assert($this->entity === null || class_exists($this->entity)); |
||
| 120 | $fetcher = $this->entity ? new FetchEntity($this->pdo, $this->entity) : new FetchAssoc($this->pdo); |
||
|
0 ignored issues
–
show
The type
Ray\AuraSqlModule\Pagerfanta\FetchEntity was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
The type
Ray\AuraSqlModule\Pagerfanta\FetchAssoc was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 121 | |||
| 122 | /** @var ExtendedPdoAdapter<T> $adapter */ |
||
| 123 | $adapter = new ExtendedPdoAdapter($this->pdo, $this->sql, $this->params, $fetcher); |
||
| 124 | |||
| 125 | return $adapter; |
||
| 126 | } |
||
| 127 | } |
||
| 128 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths