Completed
Push — master ( 6b0e91...baa484 )
by ARCANEDEV
03:10
created

Route::setUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php namespace Arcanesoft\Foundation\Services\RoutesViewer\Entities;
2
3
/**
4
 * Class     Route
5
 *
6
 * @package  Arcanesoft\Foundation\Services\RoutesViewer\Entities
7
 * @author   ARCANEDEV <[email protected]>
8
 */
9
class Route
10
{
11
    /* ------------------------------------------------------------------------------------------------
12
     |  Properties
13
     | ------------------------------------------------------------------------------------------------
14
     */
15
    /** @var array */
16
    public $methods = [];
17
18
    /** @var string */
19
    public $uri;
20
21
    /** @var array */
22
    public $params = [];
23
24
    /** @var string */
25
    public $name;
26
27
    /** @var string */
28
    public $action;
29
30
    /** @var array */
31
    public $middleware;
32
33
    /** @var string|null */
34
    public $domain;
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Constructor
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Route constructor.
42
     *
43
     * @param  array        $methods
44
     * @param  string       $uri
45
     * @param  string|null  $name
46
     * @param  string|null  $action
47
     * @param  array        $middleware
48
     * @param  string|null  $domain
49
     */
50
    public function __construct(array $methods, $uri, $action, $name, array $middleware, $domain)
51
    {
52
        $this->methods    = $methods;
53
        $this->setUri($uri);
54
        $this->name       = $name;
55
        $this->action     = $action;
56
        $this->middleware = $middleware;
57
        $this->domain     = $domain;
58
    }
59
60
    /* ------------------------------------------------------------------------------------------------
61
     |  Getters & Setters
62
     | ------------------------------------------------------------------------------------------------
63
     */
64
    /**
65
     * Set the route URI.
66
     *
67
     * @param  string  $uri
68
     *
69
     * @return self
70
     */
71
    private function setUri($uri)
72
    {
73
        $this->uri = $uri;
74
75
        preg_match_all('/({[^}]+})/', $this->uri, $matches);
76
        $this->params = $matches[0];
77
78
        return $this;
79
    }
80
81
    /**
82
     * Get the action namespace.
83
     *
84
     * @return string
85
     */
86
    public function getActionNamespace()
87
    {
88
        return $this->isClosure() ? '' : explode('@', $this->action)[0];
89
    }
90
91
    /**
92
     * Get the action method.
93
     *
94
     * @return string
95
     */
96
    public function getActionMethod()
97
    {
98
        return $this->isClosure() ? '' : explode('@', $this->action)[1];
99
    }
100
101
    /* ------------------------------------------------------------------------------------------------
102
     |  Check Functions
103
     | ------------------------------------------------------------------------------------------------
104
     */
105
    /**
106
     * Check if the route has name.
107
     *
108
     * @return bool
109
     */
110
    public function hasName()
111
    {
112
        return ! is_null($this->name);
113
    }
114
115
    /**
116
     * Check if the route has domain.
117
     *
118
     * @return bool
119
     */
120
    public function hasDomain()
121
    {
122
        return ! is_null($this->domain);
123
    }
124
125
    /**
126
     * Check if the action is a closure function.
127
     *
128
     * @return bool
129
     */
130
    public function isClosure()
131
    {
132
        return $this->action === 'Closure';
133
    }
134
}
135