1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Artprima\QueryFilterBundle\Request; |
4
|
|
|
|
5
|
|
|
use Artprima\QueryFilterBundle\Exception\InvalidArgumentException; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpRequest; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Request |
10
|
|
|
* |
11
|
|
|
* @author Denis Voytyuk <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class Request |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
private $pageNum; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $sortBy; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $sortDir; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $query; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
private $limit; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
private $simple; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Request constructor. |
47
|
|
|
* @param HttpRequest $request |
48
|
|
|
* @todo consider paramenter names not to be hard-coded |
49
|
|
|
*/ |
50
|
12 |
|
public function __construct(HttpRequest $request) |
51
|
|
|
{ |
52
|
12 |
|
$this->pageNum = (int)$request->query->get('page', 1); |
53
|
12 |
|
$this->limit = (int)$request->query->get('limit', -1); |
54
|
12 |
|
$this->query = $request->query->get('filter'); |
55
|
12 |
|
if ($this->query !== null && !is_array($this->query)) { |
56
|
1 |
|
throw new InvalidArgumentException('Query filter must be an array'); |
57
|
|
|
} |
58
|
11 |
|
$this->sortBy = $request->query->get('sortby'); |
59
|
11 |
|
$this->sortDir = $request->query->get('sortdir', 'asc'); |
60
|
11 |
|
if (!is_string($this->sortDir) || !in_array(strtolower($this->sortDir), ['asc', 'desc'], true)) { |
61
|
2 |
|
throw new InvalidArgumentException('Query sort direction must be one of those: asc or desc'); |
62
|
|
|
} |
63
|
9 |
|
$this->simple = (bool)$request->query->get('simple', true); |
|
|
|
|
64
|
9 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return int |
68
|
|
|
*/ |
69
|
6 |
|
public function getPageNum(): int |
70
|
|
|
{ |
71
|
6 |
|
return $this->pageNum; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string|null |
76
|
|
|
*/ |
77
|
5 |
|
public function getSortBy(): ?string |
78
|
|
|
{ |
79
|
5 |
|
return $this->sortBy; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string|null |
84
|
|
|
*/ |
85
|
6 |
|
public function getSortDir(): ?string |
86
|
|
|
{ |
87
|
6 |
|
return $this->sortDir; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
5 |
|
public function getLimit(): int |
94
|
|
|
{ |
95
|
5 |
|
return $this->limit; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array|null |
100
|
|
|
*/ |
101
|
6 |
|
public function getQuery(): ?array |
102
|
|
|
{ |
103
|
6 |
|
return $this->query; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
7 |
|
public function isSimple(): bool |
110
|
|
|
{ |
111
|
7 |
|
return $this->simple; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|