|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Povs\ListerBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Povs\ListerBundle\Mapper\FilterMapper; |
|
6
|
|
|
use Povs\ListerBundle\Mapper\ListField; |
|
7
|
|
|
use Povs\ListerBundle\Mapper\ListMapper; |
|
8
|
|
|
use Symfony\Component\Form\FormInterface; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Povilas Margaiatis <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class RequestHandler |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Request |
|
19
|
|
|
*/ |
|
20
|
|
|
private $request; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ConfigurationResolver |
|
24
|
|
|
*/ |
|
25
|
|
|
private $configuration; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* ListRequestHandler constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param RequestStack $requestStack |
|
31
|
|
|
* @param ConfigurationResolver $configurationResolver |
|
32
|
6 |
|
*/ |
|
33
|
|
|
public function __construct(RequestStack $requestStack, ConfigurationResolver $configurationResolver) |
|
34
|
6 |
|
{ |
|
35
|
6 |
|
$this->request = $requestStack->getCurrentRequest(); |
|
36
|
6 |
|
$this->configuration = $configurationResolver; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param ListMapper $listMapper |
|
41
|
|
|
* @param FilterMapper $filterMapper |
|
42
|
|
|
* @param FormInterface $form |
|
43
|
1 |
|
*/ |
|
44
|
|
|
public function handleRequest(ListMapper $listMapper, FilterMapper $filterMapper, FormInterface $form): void |
|
45
|
1 |
|
{ |
|
46
|
1 |
|
$this->handleListRequest($listMapper); |
|
47
|
1 |
|
$this->handleFilterRequest($filterMapper, $form); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return int|null |
|
52
|
1 |
|
*/ |
|
53
|
|
|
public function getCurrentPage(): ?int |
|
54
|
1 |
|
{ |
|
55
|
|
|
$currentPage = $this->getValue('page'); |
|
56
|
1 |
|
|
|
57
|
|
|
return $currentPage ? (int) $currentPage : null; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return int|null |
|
62
|
1 |
|
*/ |
|
63
|
|
|
public function getLength(): ?int |
|
64
|
1 |
|
{ |
|
65
|
|
|
$perPage = $this->getValue('length'); |
|
66
|
1 |
|
|
|
67
|
|
|
return $perPage ? (int) $perPage : null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $name |
|
72
|
|
|
* |
|
73
|
|
|
* @return mixed|null |
|
74
|
4 |
|
*/ |
|
75
|
|
|
public function getValue(string $name) |
|
76
|
4 |
|
{ |
|
77
|
|
|
$name = $this->getName($name); |
|
78
|
4 |
|
|
|
79
|
|
|
return $this->request->query->get($name); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $name |
|
84
|
|
|
* |
|
85
|
|
|
* @return string|null |
|
86
|
5 |
|
*/ |
|
87
|
|
|
public function getName(string $name): ?string |
|
88
|
5 |
|
{ |
|
89
|
|
|
return $this->configuration->getRequestConfiguration($name); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return Request |
|
94
|
1 |
|
*/ |
|
95
|
|
|
public function getRequest(): Request |
|
96
|
1 |
|
{ |
|
97
|
|
|
return $this->request; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param FilterMapper $filterMapper |
|
102
|
|
|
* @param FormInterface $form |
|
103
|
1 |
|
*/ |
|
104
|
|
|
private function handleFilterRequest(FilterMapper $filterMapper, FormInterface $form): void |
|
105
|
1 |
|
{ |
|
106
|
|
|
$form->handleRequest($this->request); |
|
107
|
1 |
|
|
|
108
|
1 |
|
if ($form->isSubmitted() && $form->isValid()) { |
|
109
|
|
|
$data = $form->getData(); |
|
110
|
1 |
|
|
|
111
|
1 |
|
foreach ($data as $name => $datum) { |
|
112
|
1 |
|
$field = $filterMapper->get($name); |
|
113
|
|
|
$field->setValue($datum); |
|
114
|
|
|
} |
|
115
|
1 |
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param ListMapper $listMapper |
|
120
|
1 |
|
*/ |
|
121
|
|
|
private function handleListRequest(ListMapper $listMapper): void |
|
122
|
1 |
|
{ |
|
123
|
|
|
$sort = $this->getValue('sort') ?? []; |
|
124
|
1 |
|
|
|
125
|
1 |
|
if ($sort) { |
|
126
|
|
|
$fieldsSet = null; |
|
127
|
1 |
|
|
|
128
|
1 |
|
foreach ($sort as $id => $direction) { |
|
129
|
|
|
$direction = strtoupper($direction); |
|
130
|
1 |
|
|
|
131
|
1 |
|
if (!in_array($direction, [ListField::SORT_DESC, ListField::SORT_ASC], true)) { |
|
132
|
|
|
continue; |
|
133
|
|
|
} |
|
134
|
1 |
|
|
|
135
|
1 |
|
$field = $listMapper->get($id); |
|
136
|
1 |
|
$field->setOption(ListField::OPTION_SORT_VALUE, $direction); |
|
137
|
|
|
$field->setOption(ListField::OPTION_LAZY, false); |
|
138
|
1 |
|
|
|
139
|
1 |
|
if (false === $this->configuration->isMultiColumnSortable()) { |
|
140
|
1 |
|
$fieldsSet = $field->getId(); |
|
141
|
|
|
break; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
1 |
|
|
|
145
|
1 |
|
if (null !== $fieldsSet) { |
|
146
|
1 |
|
foreach ($listMapper->getFields() as $field) { |
|
147
|
1 |
|
if ($field->getId() !== $fieldsSet) { |
|
148
|
|
|
$field->setOption(ListField::OPTION_SORT_VALUE, null); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
1 |
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|