Passed
Branch master (0fcf32)
by Henri
01:14
created

CheckTrait::check_parameters()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
use Exception;
6
7
trait CheckTrait{
8
9
    protected function check_protocol(string $expected, string $current)
10
    {
11
        if($expected !== $current){
12
            throw new Exception('Page not found.',404);
13
        }
14
    }
15
16
    protected function check_name(string $route_name){
17
        if(!array_key_exists($route_name,$this->routers)){
18
            throw new Exception('Page not found.',404);
19
        }
20
    }
21
22
    protected function check_filtering(array $route)
23
    {
24
        $filters = (is_array($route['filters'])) ? $route['filters'] : [ $route['filters'] ];
25
26
        foreach($filters as $filter){
27
            if(is_null($filter)){
28
                continue;
29
            }
30
            $this->filter->filtering($filter);
31
        }
32
    }
33
34
    protected function check_config()
35
    {
36
        if(!defined('ROUTER_CONFIG')){
37
            throw new Exception("Information for loading routes has not been defined.");
38
        }
39
    }
40
41
    protected function check_numparams(array $route_loop, array $route_request)
42
    {
43
        return (count($route_loop) !== count($route_request));
44
    }
45
46
    protected function check_parameters(array $route_loop, array $route_request)
47
    {
48
        foreach($route_loop as $rr => $param){
49
            if( (substr($param,0,1) === '{') ){
50
                $data[ substr($param,1,strlen($param)-2) ] = $route_request[$rr];    
51
            }
52
    
53
            if($this->check_parameter($param, $route_request[$rr])){
54
                return false;
55
            }
56
        }
57
    }
58
59
    protected function check_parameter(string $route_loop, string $route_request)
60
    {
61
        return !( substr($route_loop,0,1) === '{' ) and $route_loop !== $route_request;
62
    }
63
64
    protected function check_role()
65
    {
66
        if(!array_key_exists('role', $this->getData()['POST'])){
0 ignored issues
show
Bug introduced by
It seems like getData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        if(!array_key_exists('role', $this->/** @scrutinizer ignore-call */ getData()['POST'])){
Loading history...
67
            throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
68
        }
69
    }
70
71
    protected function hasProtocol(array $route, string $currentProtocol)
72
    {
73
        $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ];
74
75
        foreach($protocols as $protocol){
76
            if($protocol !== $currentProtocol){
77
                continue;
78
            }
79
        }
80
    }
81
82
}