Passed
Push — master ( 022aa2...8d4f2f )
by Henri
04:23 queued 47s
created

WhereTrait   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 11
Bugs 0 Features 7
Metric Value
eloc 46
dl 0
loc 97
rs 10
c 11
b 0
f 7
wmc 24

7 Methods

Rating   Name   Duplication   Size   Complexity  
A checkCount() 0 7 2
A replaceParam() 0 14 4
A checkValueRequire() 0 4 3
A matchParam() 0 5 4
A groupWhere() 0 13 4
A checkData() 0 28 6
A where() 0 6 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace HnrAzevedo\Router;
6
7
trait WhereTrait
8
{
9
    use Helper;
10
11
    private array $parameters = [];
12
13
    public static function where(array $wheres): Router
14
    {
15
        $route = self::getInstance()->inSave();
16
        $route['where'] = array_merge($route['where'], $wheres);
17
        self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes()));
18
        return self::getInstance();
19
    }
20
21
    public static function groupWhere(array $wheres, array $excepts): Router
22
    {
23
        $group = self::getInstance()->inSave()['group'];
24
        foreach(self::getInstance()->getRoutes() as $r => $route){
25
            if($route['group'] !== $group || in_array($route['name'], $excepts)){
26
                continue;
27
            }
28
29
            $route['where'] = array_merge($route['where'], $wheres);
30
            self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes()));
31
        }
32
        
33
        return self::getInstance();
34
    }
35
36
    protected function checkData(array $route, string $uriPath): void
37
    {
38
        $this->checkCount($route['uri']->getPath(), $uriPath);
39
    
40
        $this->parameters = [];
41
42
        $uriPath .= (substr($uriPath, strlen($uriPath)-1) !== '/') ? '/' : '';
43
44
        $routePath = explode('/', urldecode($route['uri']->getPath()));
45
        unset($routePath[0]);
46
        $uriPath = explode('/', urldecode($uriPath));
47
        unset($uriPath[0]);
48
49
        $corretRoute = true;
50
        foreach ($routePath as $r => $routeFrag){
51
            $where = is_array($route['where']) ? $route['where'] : [];
52
            $routeFrag = $this->replaceParam($where, $routeFrag, $uriPath[$r]);
53
54
            if($routeFrag !== $uriPath[$r]){
55
                $corretRoute = false;
56
            }
57
        }
58
59
        if(!$corretRoute){
60
            throw new \Exception('continue');
61
        }
62
63
        $_REQUEST = array_merge($_REQUEST,$this->parameters);
64
    }
65
66
    private function replaceParam(array $where, string $ref, string $value): string
67
    {
68
        if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) {
69
            $this->parameters[str_replace(['{:','{','}'],'',$ref)] = $value;
70
71
            $this->checkValueRequire($ref,$value);
72
73
            if(array_key_exists(str_replace(['{:','{','}'],'',$ref),$where)){
74
                $this->matchParam($where, $ref, $value);
75
            }
76
77
            return $value;
78
        } 
79
        return $ref;
80
    }
81
82
    private function checkValueRequire(string $ref, string $value): void
83
    {
84
        if(substr($ref,0,2) !== '{:' && strlen($value) === 0){
85
            throw new \Exception('continue');
86
        }
87
    }
88
89
    private function checkCount(string $routePath, string $uriPath): void
90
    {
91
        $countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:');
92
        $countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:');
93
94
        if($countRequest !== $countRoute){
95
            throw new \Exception('continue');
96
        }
97
    }
98
99
    private function matchParam(array $where, string $ref, string $value): void
100
    {
101
        if(substr($ref,0,2) === '{' || $value !== ''){
102
            if(!preg_match("/^{$where[str_replace(['{:','{','}'],'',$ref)]}$/",$value)){
103
                throw new \Exception('continue');
104
            }
105
        }
106
    }
107
108
}
109