1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Controller; |
13
|
|
|
|
14
|
|
|
use Phalcon\Filter\Exception; |
15
|
|
|
use Zemit\Mvc\Controller\AbstractTrait\AbstractInjectable; |
16
|
|
|
|
17
|
|
|
trait Params |
18
|
|
|
{ |
19
|
|
|
use AbstractInjectable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get a specific parameter value by key. |
23
|
|
|
* |
24
|
|
|
* @param string $key The key of the parameter. |
25
|
|
|
* @param string|array|null $filters Optional. The filters to apply to the parameter value. Defaults to null. |
26
|
|
|
* @param string|null $default Optional. The default value if the parameter does not exist. Defaults to null. |
27
|
|
|
* @param array|null $params Optional. The array of parameters to search from. Defaults to null. |
28
|
|
|
* |
29
|
|
|
* @return mixed The value of the specified parameter, after applying the filters if provided. If the parameter does not exist, |
30
|
|
|
* then the default value is returned if provided. If both the parameter and the default value are missing, |
31
|
|
|
* then the value from the dispatcher's parameter is returned. |
32
|
|
|
* @throws Exception |
33
|
|
|
*/ |
34
|
|
|
public function getParam(string $key, string|array $filters = null, string $default = null, array $params = null): mixed |
35
|
|
|
{ |
36
|
|
|
$params ??= $this->getParams(); |
37
|
|
|
|
38
|
|
|
return isset($params[$key]) |
39
|
|
|
? ($filters ? $this->filter->sanitize($params[$key], $filters) : $params[$key]) |
|
|
|
|
40
|
|
|
: $this->dispatcher->getParam($key, $filters, $default); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Retrieves the request parameters. |
45
|
|
|
* |
46
|
|
|
* @param array|null $filters An optional array of filters to apply to the parameters. |
47
|
|
|
* @return array The request parameters. |
48
|
|
|
*/ |
49
|
|
|
protected function getParams(array $filters = null): array |
50
|
|
|
{ |
51
|
|
|
if (!empty($filters)) { |
52
|
|
|
foreach ($filters as $filter) { |
53
|
|
|
$this->request->setParameterFilters($filter['name'], $filter['filters'], $filter['scope']); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$params = array_merge_recursive( |
58
|
|
|
$this->request->getFilteredQuery(), // $_GET |
59
|
|
|
$this->request->getFilteredPut(), // $_PUT |
60
|
|
|
$this->request->getFilteredPost(), // $_POST |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
// remove default phalcon _url param |
64
|
|
|
if (isset($params['_url'])) { |
65
|
|
|
unset($params['_url']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $params; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|