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