|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2021 Cloud Creativity Limited |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace LaravelJsonApi\CursorPagination\Cursor; |
| 21 | + |
| 22 | +use InvalidArgumentException; |
| 23 | + |
| 24 | +class Cursor |
| 25 | +{ |
| 26 | + |
| 27 | + /** |
| 28 | + * @var string|null |
| 29 | + */ |
| 30 | + private ?string $before; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var string|null |
| 34 | + */ |
| 35 | + private ?string $after; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var int|null |
| 39 | + */ |
| 40 | + private ?int $limit; |
| 41 | + |
| 42 | + /** |
| 43 | + * Cursor constructor. |
| 44 | + * |
| 45 | + * @param string|null $before |
| 46 | + * @param string|null $after |
| 47 | + * @param int|null $limit |
| 48 | + */ |
| 49 | + public function __construct(string $before = null, string $after = null, int $limit = null) |
| 50 | + { |
| 51 | + if (is_int($limit) && 1 > $limit) { |
| 52 | + throw new InvalidArgumentException('Expecting a limit that is 1 or greater.'); |
| 53 | + } |
| 54 | + |
| 55 | + $this->before = $before ?: null; |
| 56 | + $this->after = $after ?: null; |
| 57 | + $this->limit = $limit; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return bool |
| 62 | + */ |
| 63 | + public function isBefore(): bool |
| 64 | + { |
| 65 | + return !is_null($this->before); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return string|null |
| 70 | + */ |
| 71 | + public function getBefore(): ?string |
| 72 | + { |
| 73 | + return $this->before; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @return bool |
| 78 | + */ |
| 79 | + public function isAfter(): bool |
| 80 | + { |
| 81 | + return !is_null($this->after) && !$this->isBefore(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @return string|null |
| 86 | + */ |
| 87 | + public function getAfter(): ?string |
| 88 | + { |
| 89 | + return $this->after; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Set a limit, if no limit is set on the cursor. |
| 94 | + * |
| 95 | + * @param int $limit |
| 96 | + * @return Cursor |
| 97 | + */ |
| 98 | + public function withDefaultLimit(int $limit): self |
| 99 | + { |
| 100 | + if (is_null($this->limit)) { |
| 101 | + $copy = clone $this; |
| 102 | + $copy->limit = $limit; |
| 103 | + return $copy; |
| 104 | + } |
| 105 | + |
| 106 | + return $this; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return int|null |
| 111 | + */ |
| 112 | + public function getLimit(): ?int |
| 113 | + { |
| 114 | + return $this->limit; |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments