Passed
Push — master ( 82fa8b...914114 )
by Henri
01:17
created

CheckWhere::checkWhere()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 12
c 1
b 0
f 1
nc 5
nop 2
dl 0
loc 22
rs 9.2222
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 checkExistParam($param, $data)
17
    {
18
        if(!array_key_exists($param,$data)){
19
            throw new Exception('Test parameter not available on the route in question.');
20
        }
21
    }
22
23
    protected function checkWhereParams($params)
24
    {
25
        if(count($params) === 0){
26
            throw new Exception('The route in question has no parameters to be tested.');
27
        }
28
    }
29
30
    protected function checkWhere($route, $request): bool
31
    {
32
        $pass = true;
33
34
        if(!is_array($route['where'])){
35
            return $pass;
36
        }
37
38
        $routeURI = explode('/',$route['url']);
39
        $params = [];
40
        foreach($routeURI as $p => $part){
41
            if(substr($part,0,1) === '{' && substr($part,-1) === '}'){
42
                $param = substr($part,1,-1);
43
                $params[$param] = $route['where'][$param];
44
45
                if(!preg_match("/^{$params[$param]}$/",$request[$p])){
46
                    $pass = false;
47
                }
48
            }
49
        }
50
        
51
        return $pass;
52
    }
53
54
}
55