sfneal /
queries
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Sfneal\Queries; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Model; |
||
| 6 | use Sfneal\Caching\Traits\Cacheable; |
||
| 7 | use Sfneal\Models\AbstractModel; |
||
| 8 | |||
| 9 | class NextOrPreviousModelQuery extends AbstractQuery |
||
| 10 | { |
||
| 11 | use Cacheable; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var bool Determine if the return model should be the 'next' or 'previous' |
||
| 15 | */ |
||
| 16 | private $next; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string Cache Key String ('next' or 'previous') |
||
| 20 | */ |
||
| 21 | private $cache_key_string; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var AbstractModel|string |
||
| 25 | */ |
||
| 26 | private $model; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var int Model ID |
||
| 30 | */ |
||
| 31 | private $model_id; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * NextOrPreviousModelQuery constructor. |
||
| 35 | * |
||
| 36 | * @param AbstractModel|string $model |
||
| 37 | * @param int $model_id |
||
| 38 | * @param bool $next |
||
| 39 | */ |
||
| 40 | public function __construct($model, int $model_id, bool $next = true) |
||
| 41 | { |
||
| 42 | $this->model = $model; |
||
| 43 | $this->model_id = $model_id; |
||
| 44 | $this->next = $next; |
||
| 45 | $this->cache_key_string = $next ? 'next' : 'previous'; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Retrieve the 'next' or 'previous' Model. |
||
| 50 | * |
||
| 51 | * @return Model|null |
||
| 52 | */ |
||
| 53 | public function execute() |
||
| 54 | { |
||
| 55 | $query = $this->model::query(); |
||
| 56 | |||
| 57 | // Next Model |
||
| 58 | if ($this->next == true) { |
||
|
0 ignored issues
–
show
|
|||
| 59 | return $query->getNextModel($this->model_id); |
||
| 60 | } |
||
| 61 | |||
| 62 | // Previous Model |
||
| 63 | else { |
||
| 64 | return $query->getPreviousModel($this->model_id); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Set the returned model to 'next'. |
||
| 70 | * |
||
| 71 | * @return $this |
||
| 72 | */ |
||
| 73 | public function next(): self |
||
| 74 | { |
||
| 75 | $this->next = true; |
||
| 76 | $this->cache_key_string = 'next'; |
||
| 77 | |||
| 78 | return $this; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set the returned model to 'previous'. |
||
| 83 | * |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | public function previous(): self |
||
| 87 | { |
||
| 88 | $this->next = false; |
||
| 89 | $this->cache_key_string = 'previous'; |
||
| 90 | |||
| 91 | return $this; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Retrieve the Queries Cache Key. |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | public function cacheKey(): string |
||
| 100 | { |
||
| 101 | return $this->model::getTableName().":{$this->cache_key_string}:#{$this->model_id}"; |
||
| 102 | } |
||
| 103 | } |
||
| 104 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.