Passed
Branch master (7482d2)
by Henri
09:41 queued 08:07
created

CheckTrait::isInPseudGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace HnrAzevedo\Router;
6
7
trait CheckTrait
8
{
9
    use Helper;
10
    
11
    protected array $routesName = [];
12
    
13
    protected function hasRouteName(string $name): void
14
    {
15
        if(!isset($this->routesName[$name])){
16
            throw new \RuntimeException("There is no route named with {$name}");
17
        }
18
    }
19
20
    protected function isInNameGroup(): void
21
    {
22
        if(!is_null($this->getGroup())){
23
            throw new \RuntimeException("It is not allowed to assign names to groups");
24
        }
25
    }
26
27
    protected function isInPseudGroup(): void
28
    {
29
        if(!is_null($this->getGroup())){
30
            throw new \RuntimeException("To assign actions before or after the execution of the route, use beforeGroup or afterGroup");
31
        }
32
    }
33
34
    protected function existRouteName(string $name): void
35
    {
36
        if(isset($this->routesName[$name])){
37
            throw new \RuntimeException("There is already a route named with {$name}");
38
        }
39
    }
40
41
    protected function checkMethod(array $route, $method): void
42
    {
43
        $hasMethod = false;
44
        foreach(explode('|',$route['method']) as $routeMethod){
45
            if(@preg_match("/{$routeMethod}/",$method) !== 0 || $method === '*'){
46
                $hasMethod = true;
47
            }
48
        }
49
        if(!$hasMethod){
50
            throw new \Exception('This route is not released for the accessed method');
51
        } 
52
    }
53
54
    protected function throwCallable($value): void
55
    {
56
        if(is_callable($value)){
57
            throw new \Exception('Passing functions as attributes is not allowed');
58
        }
59
    }
60
61
}
62