1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
namespace Spiral\Listing; |
9
|
|
|
|
10
|
|
|
use Spiral\Http\Request\InputInterface; |
11
|
|
|
use Spiral\Listing\DirectionalSorterInterface as DirectionalSorter; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* State depended on input source. |
15
|
|
|
*/ |
16
|
|
|
class InputState implements StateInterface |
17
|
|
|
{ |
18
|
|
|
const FILTERS = 'filters'; |
19
|
|
|
const FILTER_VALUES = 'values'; |
20
|
|
|
const SORTER = 'sortBy'; |
21
|
|
|
const DIRECTION = 'order'; |
22
|
|
|
const PAGE = 'page'; |
23
|
|
|
const LIMIT = 'limit'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $namespace = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var InputInterface |
32
|
|
|
*/ |
33
|
|
|
private $input = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $source = 'query'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param InputInterface $input |
42
|
|
|
* @param string $source |
43
|
|
|
* @param string $namespace |
44
|
|
|
*/ |
45
|
|
|
public function __construct(InputInterface $input, $source = 'query', $namespace = '') |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$this->input = $input; |
48
|
|
|
$this->namespace = $namespace; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $source |
53
|
|
|
* @return InputState |
54
|
|
|
*/ |
55
|
|
|
public function withSource($source) |
56
|
|
|
{ |
57
|
|
|
$state = clone $this; |
58
|
|
|
$state->source = $source; |
59
|
|
|
|
60
|
|
|
return $state; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getSource() |
67
|
|
|
{ |
68
|
|
|
return $this->source; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function isActive() |
75
|
|
|
{ |
76
|
|
|
if (empty($this->source)) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$input = $this->input->getValue($this->source, "{$this->namespace}"); |
81
|
|
|
|
82
|
|
|
return !empty($input); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function withNamespace($namespace) |
89
|
|
|
{ |
90
|
|
|
$state = clone $this; |
91
|
|
|
$state->namespace = $namespace; |
92
|
|
|
|
93
|
|
|
return $state; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function getNamespace() |
100
|
|
|
{ |
101
|
|
|
return $this->namespace; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function activeFilters() |
108
|
|
|
{ |
109
|
|
|
$filters = $this->input->getValue($this->source, "{$this->namespace}." . self::FILTERS); |
110
|
|
|
|
111
|
|
|
if (empty($filters) || !is_array($filters)) { |
112
|
|
|
$filters = []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return array_values($filters); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function getValue($filter, $default = null) |
122
|
|
|
{ |
123
|
|
|
$value = $this->input->getValue( |
124
|
|
|
$this->source, |
125
|
|
|
"{$this->namespace}." . self::FILTER_VALUES . '.' . $filter |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
if (null == $value) { |
129
|
|
|
return $default; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $value; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function activeSorter() |
139
|
|
|
{ |
140
|
|
|
$sorter = $this->input->getValue($this->source, "{$this->namespace}." . self::SORTER); |
141
|
|
|
if (empty($sorter)) { |
142
|
|
|
$sorter = 'id'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $sorter; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function sortDirection() |
152
|
|
|
{ |
153
|
|
|
$direction = $this->input->getValue($this->source, "{$this->namespace}." . self::DIRECTION); |
154
|
|
|
|
155
|
|
|
if (strtolower($direction) == 'desc' || $direction == -1) { |
156
|
|
|
return DirectionalSorter::DESC; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return DirectionalSorter::ASC; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function getPage() |
166
|
|
|
{ |
167
|
|
|
return (int)$this->input->getValue($this->source, "{$this->namespace}." . self::PAGE); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function getLimit() |
174
|
|
|
{ |
175
|
|
|
return (int)$this->input->getValue($this->source, "{$this->namespace}." . self::LIMIT); |
176
|
|
|
} |
177
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.