1 | <?php |
||
19 | class Group extends ControllerAbstract |
||
20 | { |
||
21 | /** |
||
22 | * process the class |
||
23 | * |
||
24 | * @since 4.0.0 |
||
25 | * |
||
26 | * @param string $action action to process |
||
27 | * |
||
28 | * @return string |
||
29 | */ |
||
30 | |||
31 | 10 | public function process(string $action = null) : string |
|
32 | { |
||
33 | 10 | $postArray = $this->_normalizePost($this->_sanitizePost()); |
|
34 | 10 | $validateArray = $this->_validatePost($postArray); |
|
|
|||
35 | |||
36 | /* validate post */ |
||
37 | |||
38 | 10 | if ($validateArray) |
|
39 | { |
||
40 | 6 | return $this->_error( |
|
41 | [ |
||
42 | 6 | 'route' => $this->_getErrorRoute($postArray), |
|
43 | 6 | 'message' => $validateArray |
|
44 | ]); |
||
45 | } |
||
46 | |||
47 | /* handle create */ |
||
48 | |||
49 | 4 | if ($action === 'create') |
|
50 | { |
||
51 | $createArray = |
||
52 | [ |
||
53 | 1 | 'name' => $postArray['name'], |
|
54 | 1 | 'alias' => $postArray['alias'], |
|
55 | 1 | 'description' => $postArray['description'], |
|
56 | 1 | 'categories' => $postArray['categories'], |
|
57 | 1 | 'articles' => $postArray['articles'], |
|
58 | 1 | 'extras' => $postArray['extras'], |
|
59 | 1 | 'comments' => $postArray['comments'], |
|
60 | 1 | 'groups' => $postArray['groups'], |
|
61 | 1 | 'users' => $postArray['users'], |
|
62 | 1 | 'modules' => $postArray['modules'], |
|
63 | 1 | 'settings' => $postArray['settings'], |
|
64 | 1 | 'filter' => $postArray['filter'], |
|
65 | 1 | 'status' => $postArray['status'] |
|
66 | ]; |
||
67 | 1 | if ($this->_create($createArray)) |
|
68 | { |
||
69 | 1 | return $this->_success( |
|
70 | [ |
||
71 | 1 | 'route' => $this->_getSuccessRoute($postArray), |
|
72 | 1 | 'timeout' => 2 |
|
73 | ]); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | /* handle update */ |
||
78 | |||
79 | 3 | if ($action === 'update') |
|
80 | { |
||
81 | $updateFullArray = |
||
82 | [ |
||
83 | 2 | 'name' => $postArray['name'], |
|
84 | 2 | 'alias' => $postArray['alias'], |
|
85 | 2 | 'description' => $postArray['description'], |
|
86 | 2 | 'categories' => $postArray['categories'], |
|
87 | 2 | 'articles' => $postArray['articles'], |
|
88 | 2 | 'extras' => $postArray['extras'], |
|
89 | 2 | 'comments' => $postArray['comments'], |
|
90 | 2 | 'groups' => $postArray['groups'], |
|
91 | 2 | 'users' => $postArray['users'], |
|
92 | 2 | 'modules' => $postArray['modules'], |
|
93 | 2 | 'settings' => $postArray['settings'], |
|
94 | 2 | 'filter' => $postArray['filter'], |
|
95 | 2 | 'status' => $postArray['status'] |
|
96 | ]; |
||
97 | $updateLiteArray = |
||
98 | [ |
||
99 | 2 | 'name' => $postArray['name'], |
|
100 | 2 | 'alias' => $postArray['alias'], |
|
101 | 2 | 'description' => $postArray['description'] |
|
102 | ]; |
||
103 | 2 | if ($this->_update($postArray['id'], $postArray['id'] > 1 ? $updateFullArray : $updateLiteArray)) |
|
104 | { |
||
105 | 2 | return $this->_success( |
|
106 | [ |
||
107 | 2 | 'route' => $this->_getSuccessRoute($postArray), |
|
108 | 2 | 'timeout' => 2 |
|
109 | ]); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /* handle error */ |
||
114 | |||
115 | 1 | return $this->_error( |
|
116 | [ |
||
117 | 1 | 'route' => $this->_getErrorRoute($postArray) |
|
118 | ]); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * sanitize the post |
||
123 | * |
||
124 | * @since 4.0.0 |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | |||
129 | 10 | protected function _sanitizePost() : array |
|
130 | { |
||
131 | 10 | $aliasFilter = new Filter\Alias(); |
|
132 | 10 | $numberFilter = new Filter\Number(); |
|
133 | 10 | $textFilter = new Filter\Text(); |
|
134 | 10 | $toggleFilter = new Filter\Toggle(); |
|
135 | |||
136 | /* sanitize post */ |
||
137 | |||
138 | return |
||
139 | [ |
||
140 | 10 | 'id' => $numberFilter->sanitize($this->_request->getPost('id')), |
|
141 | 10 | 'name' => $textFilter->sanitize($this->_request->getPost('name')), |
|
142 | 10 | 'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')), |
|
143 | 10 | 'description' => $textFilter->sanitize($this->_request->getPost('description')), |
|
144 | 10 | 'categories' => json_encode($this->_request->getPost('categories')), |
|
145 | 10 | 'articles' => json_encode($this->_request->getPost('articles')), |
|
146 | 10 | 'extras' => json_encode($this->_request->getPost('extras')), |
|
147 | 10 | 'comments' => json_encode($this->_request->getPost('comments')), |
|
148 | 10 | 'groups' => json_encode($this->_request->getPost('groups')), |
|
149 | 10 | 'users' => json_encode($this->_request->getPost('users')), |
|
150 | 10 | 'modules' => json_encode($this->_request->getPost('modules')), |
|
151 | 10 | 'settings' => $numberFilter->sanitize($this->_request->getPost('settings')), |
|
152 | 10 | 'filter' => $toggleFilter->sanitize($this->_request->getPost('filter')), |
|
153 | 10 | 'status' => $toggleFilter->sanitize($this->_request->getPost('status')) |
|
154 | ]; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * validate the post |
||
159 | * |
||
160 | * @since 4.0.0 |
||
161 | * |
||
162 | * @param array $postArray array of the post |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | |||
167 | 10 | protected function _validatePost(array $postArray = []) : array |
|
198 | |||
199 | /** |
||
200 | * create the group |
||
201 | * |
||
202 | * @since 4.0.0 |
||
203 | * |
||
204 | * @param array $createArray array of the create |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | |||
209 | 1 | protected function _create(array $createArray = []) : bool |
|
214 | |||
215 | /** |
||
216 | * update the group |
||
217 | * |
||
218 | * @since 4.0.0 |
||
219 | * |
||
220 | * @param int $groupId identifier of the group |
||
221 | * @param array $updateArray array of the update |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | |||
226 | 2 | protected function _update(int $groupId = null, array $updateArray = []) : bool |
|
231 | |||
232 | /** |
||
233 | * get success route |
||
234 | * |
||
235 | * @since 4.0.0 |
||
236 | * |
||
237 | * @param array $postArray array of the post |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | |||
242 | 3 | protected function _getSuccessRoute(array $postArray = []) : string |
|
260 | |||
261 | /** |
||
262 | * get error route |
||
263 | * |
||
264 | * @since 4.0.0 |
||
265 | * |
||
266 | * @param array $postArray array of the post |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | |||
271 | 7 | protected function _getErrorRoute(array $postArray = []) : string |
|
283 | } |
||
284 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.