Passed
Push — master ( 5b5c00...4c0c12 )
by Julien
04:52
created

Params::getParam()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
cc 3
nc 4
nop 4
crap 12
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])
0 ignored issues
show
Bug introduced by
The property filter does not exist on Zemit\Mvc\Controller\Params. Did you mean filter;?
Loading history...
40
            : $this->dispatcher->getParam($key, $filters, $default);
0 ignored issues
show
Bug introduced by
The property dispatcher does not exist on Zemit\Mvc\Controller\Params. Did you mean dispatcher;?
Loading history...
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']);
0 ignored issues
show
Bug introduced by
The property request does not exist on Zemit\Mvc\Controller\Params. Did you mean request;?
Loading history...
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