Passed
Push — master ( dc471b...c13d2f )
by Julien
21:34
created

Params::getParams()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 37
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 1
b 0
f 0
nc 20
nop 1
dl 0
loc 37
ccs 0
cts 16
cp 0
crap 72
rs 8.4444
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\Traits;
13
14
use Phalcon\Filter\Exception;
15
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractInjectable;
16
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractParams;
17
18
trait Params
19
{
20
    use AbstractParams;
21
    
22
    use AbstractInjectable;
23
    
24
    /**
25
     * Get a specific parameter value by key.
26
     *
27
     * @param string $key The key of the parameter.
28
     * @param array|string|null $filters Optional. The filters to apply to the parameter value. Defaults to null.
29
     * @param mixed $default Optional. The default value if the parameter does not exist. Defaults to null.
30
     * @param array|null $params Optional. The array of parameters to search from. Defaults to null.
31
     *
32
     * @return mixed The value of the specified parameter, after applying the filters if provided. If the parameter does not exist,
33
     *               then the default value is returned if provided. If both the parameter and the default value are missing,
34
     *               then the value from the dispatcher's parameter is returned.
35
     * @throws Exception
36
     */
37
    public function getParam(string $key, array|string|null $filters = null, mixed $default = null, array $params = null): mixed
38
    {
39
        $params ??= $this->getParams();
40
        
41
        return isset($params[$key])
42
            ? ($filters ? $this->filter->sanitize($params[$key], $filters) : $params[$key])
43
            : $this->dispatcher->getParam($key, $filters, $default);
44
    }
45
    
46
    /**
47
     * Retrieves the request parameters.
48
     *
49
     * @param array|null $filters An optional array of filters to apply to the parameters.
50
     * @return array The request parameters.
51
     */
52
    public function getParams(array $filters = null): array
53
    {
54
        if (!empty($filters)) {
55
            foreach ($filters as $filter) {
56
                $this->request->setParameterFilters($filter['name'], $filter['filters'], $filter['scope']);
57
            }
58
        }
59
        
60
        if ($this->request->isGet()) {
61
            $params = $this->request->getFilteredQuery();
62
        }
63
        else if ($this->request->isPost()) {
64
            $params = $this->request->getFilteredPost();
65
        }
66
        else if ($this->request->isPatch()) {
67
            $params = $this->request->getFilteredPatch();
68
        }
69
        else if ($this->request->isPut()) {
70
            $params = $this->request->getFilteredPut();
71
        }
72
        else {
73
            $params = [];
74
        }
75
76
//        $params = array_merge_recursive(
77
//            $this->request->getFilteredQuery(), // $_GET
78
////            $this->request->getFilteredPatch(), // $_PATCH
79
//            $this->request->getFilteredPut(), // $_PUT
80
//            $this->request->getFilteredPost(), // $_POST
81
//        );
82
        
83
        // remove default phalcon _url param
84
        if (isset($params['_url'])) {
85
            unset($params['_url']);
86
        }
87
        
88
        return $params;
89
    }
90
    
91
}
92