ShortUrlRepositoryAdapter.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shlinkio\Shlink\Core\Paginator\Adapter;
  4. use Laminas\Paginator\Adapter\AdapterInterface;
  5. use Shlinkio\Shlink\Common\Util\DateRange;
  6. use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
  7. use function strip_tags;
  8. use function trim;
  9. class ShortUrlRepositoryAdapter implements AdapterInterface
  10. {
  11. public const ITEMS_PER_PAGE = 10;
  12. private ShortUrlRepositoryInterface $repository;
  13. private ?string $searchTerm;
  14. /** @var null|array|string */
  15. private $orderBy;
  16. private array $tags;
  17. private ?DateRange $dateRange;
  18. /**
  19. * @param string|array|null $orderBy
  20. */
  21. public function __construct(
  22. ShortUrlRepositoryInterface $repository,
  23. ?string $searchTerm = null,
  24. array $tags = [],
  25. $orderBy = null,
  26. ?DateRange $dateRange = null
  27. ) {
  28. $this->repository = $repository;
  29. $this->searchTerm = $searchTerm !== null ? trim(strip_tags($searchTerm)) : null;
  30. $this->orderBy = $orderBy;
  31. $this->tags = $tags;
  32. $this->dateRange = $dateRange;
  33. }
  34. /**
  35. * Returns a collection of items for a page.
  36. *
  37. * @param int $offset Page offset
  38. * @param int $itemCountPerPage Number of items per page
  39. */
  40. public function getItems($offset, $itemCountPerPage): array // phpcs:ignore
  41. {
  42. return $this->repository->findList(
  43. $itemCountPerPage,
  44. $offset,
  45. $this->searchTerm,
  46. $this->tags,
  47. $this->orderBy,
  48. $this->dateRange,
  49. );
  50. }
  51. /**
  52. * Count elements of an object
  53. * @link http://php.net/manual/en/countable.count.php
  54. * @return int The custom count as an integer.
  55. * </p>
  56. * <p>
  57. * The return value is cast to an integer.
  58. * @since 5.1.0
  59. */
  60. public function count(): int
  61. {
  62. return $this->repository->countList($this->searchTerm, $this->tags, $this->dateRange);
  63. }
  64. }