Test Setup Failed
Push — master ( 7dea29...43ef24 )
by Php Easy Api
05:34
created

RouteAccessiblePropertyTrait::getRoutes()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 5
nop 1
dl 0
loc 27
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Router;
4
5
use Resta\Support\Utils;
6
7
trait RouteAccessiblePropertyTrait
8
{
9
    /**
10
     * get route mappers
11
     *
12
     * @return array
13
     */
14
    public static function getMappers() : array
15
    {
16
        // this feature will give you a map
17
        // to see all your route methods.
18
        return static::$mappers;
19
    }
20
21
    /**
22
     * get static path
23
     *
24
     * @return array
25
     */
26
    protected static function getPath() : array
27
    {
28
        // this feature helps you to assign
29
        // which routes to run on your routes.
30
        return static::$paths;
31
    }
32
33
    /**
34
     * get routes
35
     *
36
     * @param null|string $method
37
     * @return array
38
     */
39
    public static function getRoutes($method=null) : array
40
    {
41
        // it collects and
42
        // executes route data in an array.
43
        if(is_null($method)){
44
            return static::$routes;
45
        }
46
47
        $httpRouteList = [];
48
49
        $routes = self::getRoutes();
50
51
        if(isset($routes['data'])){
52
            foreach ($routes['data'] as $key=>$item){
53
                if($item['http']==httpMethod()){
54
                    $httpRouteList['data'][$key] = $item;
55
                }
56
            }
57
        }
58
59
        if(isset($routes['pattern'],$httpRouteList['data'])){
60
            foreach ($httpRouteList['data'] as $key=>$item){
61
                $httpRouteList['pattern'][$key] = $routes['pattern'][$key];
62
            }
63
        }
64
65
        return $httpRouteList;
66
    }
67
68
    /**
69
     * get static trace path
70
     *
71
     * @return mixed|null
72
     */
73
    protected static function getTracePath()
74
    {
75
        // detects where the route path is coming from
76
        // and returns this data in the static path.
77
        $trace = Utils::trace(2,'file');
78
79
        // the trace is returned if the variable is available
80
        // in the path data, otherwise it returns null.
81
        return static::getPath()[$trace] ?? null;
82
    }
83
}