WhereTrait   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 15
Bugs 0 Features 10
Metric Value
eloc 47
c 15
b 0
f 10
dl 0
loc 99
rs 10
wmc 25

7 Methods

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