Passed
Push — master ( 039311...3a61a3 )
by Henri
01:17
created

CheckWhere::isWhered()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
use Exception;
6
7
trait CheckWhere{
8
        
9
    protected function checkWhereParam($data)
10
    {
11
        if(count($data) === 0){
12
            throw new Exception('It is necessary to define a condition to be tested.');
13
        }
14
    }
15
16
    protected function checkWhereParams($params)
17
    {
18
        if(count($params) === 0){
19
            throw new Exception('The route in question has no parameters to be tested.');
20
        }
21
    }
22
23
    protected function checkWhere($route, $request): bool
24
    {
25
        $pass = true;
26
27
        if(!is_array($route['where'])){
28
            return $pass;
29
        }
30
31
        $routeURI = explode('/',$route['url']);
32
        $params = [];
33
        foreach($routeURI as $p => $part){
34
            if(!$this->isWhered($part,$request[$p])){
35
                continue;
36
            }
37
                
38
            $param = substr(str_replace('?','',$part),1,-1);
39
40
            if(array_key_exists($param,$route['where'])){
41
                    
42
                $params[$param] = $route['where'][$param];
43
44
                if(!preg_match("/^{$params[$param]}$/",$request[$p])){
45
                    $pass = false;
46
                }
47
            }
48
        }
49
        
50
        return $pass;
51
    }
52
53
    private function isWhered(string $part, string $value): bool
54
    {
55
        return $this->isParameter($part) && !$this->checkParameterOptional($part,$value);
56
    }
57
58
    private function checkParameterOptional(string $part, string $value): bool
59
    {
60
        return (strpos($part,'{?') && empty($value));
61
    }
62
63
    protected function isParameter(string $part): bool
64
    {
65
        return (substr($part,0,1) === '{' && substr($part,-1) === '}');
66
    }
67
68
    protected function callWhereAdd($data)
69
    {
70
        $this->checkWhereParam($data);
71
        
72
        $data = (count($data) > 1) ? [$data[0] => $data[1]] : $data[0];
73
        
74
        $route = end($this->routers);
75
        $routeURI = explode('/',$route['url']);
76
        $params = [];
77
        foreach($routeURI as $part){
78
            if(!$this->isParameter($part)){
79
                continue;
80
            }
81
            
82
            $param = substr(str_replace('?','',$part),1,-1);
83
84
            if(array_key_exists($param,$data)){
85
                $params[$param] = $data[$param];
86
            }
87
                
88
        }
89
90
        $this->checkWhereParams($params);
91
92
        $route['where'] = (is_array($route['where'])) ? array_merge($route['where'],$params) : $params;
93
94
        $this->routers[count($this->routers)-1] = $route;
0 ignored issues
show
Bug Best Practice introduced by
The property routers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
95
    }
96
97
}
98