Completed
Push — master ( 306a3f...490f2b )
by ARCANEDEV
02:35 queued 02:31
created

Route::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\RouteViewer\Entities;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
use Illuminate\Contracts\Support\Jsonable;
5
use JsonSerializable;
6
7
/**
8
 * Class     Route
9
 *
10
 * @package  Arcanedev\RouteViewer\Entities
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class Route implements Arrayable, Jsonable, JsonSerializable
14
{
15
    /* -----------------------------------------------------------------
16
     |  Properties
17
     | -----------------------------------------------------------------
18
     */
19
20
    /** @var array */
21
    public $methods = [];
22
23
    /** @var string */
24
    public $uri;
25
26
    /** @var array */
27
    public $params = [];
28
29
    /** @var string */
30
    public $action;
31
32
    /** @var string */
33
    public $name;
34
35
    /** @var array */
36
    public $middleware;
37
38
    /** @var string|null */
39
    public $domain;
40
41
    /* -----------------------------------------------------------------
42
     |  Constructor
43
     | -----------------------------------------------------------------
44
     */
45
46
    /**
47
     * Route constructor.
48
     *
49
     * @param  array        $methods
50
     * @param  string       $uri
51
     * @param  string|null  $name
52
     * @param  string|null  $action
53
     * @param  array        $middleware
54
     * @param  string|null  $domain
55
     */
56 24
    public function __construct(array $methods, $uri, $action, $name, array $middleware, $domain = null)
57
    {
58 24
        $this->methods    = $methods;
59 24
        $this->setUri($uri);
60 24
        $this->action     = $action;
61 24
        $this->name       = $name;
62 24
        $this->middleware = $middleware;
63 24
        $this->domain     = $domain;
64 24
    }
65
66
    /* -----------------------------------------------------------------
67
     |  Getters & Setters
68
     | -----------------------------------------------------------------
69
     */
70
71
    /**
72
     * Set the route URI.
73
     *
74
     * @param  string  $uri
75
     *
76
     * @return self
77
     */
78 24
    private function setUri($uri)
79
    {
80 24
        $this->uri = $uri;
81
82 24
        preg_match_all('/({[^}]+})/', $this->uri, $matches);
83 24
        $this->params = $matches[0];
84
85 24
        return $this;
86
    }
87
88
    /**
89
     * Get the action namespace.
90
     *
91
     * @return string
92
     */
93 6
    public function getActionNamespace()
94
    {
95 6
        return $this->isClosure() ? '' : explode('@', $this->action)[0];
96
    }
97
98
    /**
99
     * Get the action method.
100
     *
101
     * @return string
102
     */
103 6
    public function getActionMethod()
104
    {
105 6
        return $this->isClosure() ? '' : explode('@', $this->action)[1];
106
    }
107
108
    /* -----------------------------------------------------------------
109
     |  Check Methods
110
     | -----------------------------------------------------------------
111
     */
112
113
    /**
114
     * Check if the route has name.
115
     *
116
     * @return bool
117
     */
118 6
    public function hasName()
119
    {
120 6
        return ! is_null($this->name);
121
    }
122
123
    /**
124
     * Check if the route has domain.
125
     *
126
     * @return bool
127
     */
128 3
    public function hasDomain()
129
    {
130 3
        return ! is_null($this->domain);
131
    }
132
133
    /**
134
     * Check if the route has middleware.
135
     *
136
     * @return bool
137
     */
138 3
    public function hasMiddleware()
139
    {
140 3
        return ! empty($this->middleware);
141
    }
142
143
    /**
144
     * Check if the action is a closure function.
145
     *
146
     * @return bool
147
     */
148 9
    public function isClosure()
149
    {
150 9
        return $this->action === 'Closure';
151
    }
152
153
    /* -----------------------------------------------------------------
154
     |  Other Methods
155
     | -----------------------------------------------------------------
156
     */
157
158
    /**
159
     * Get the instance as an array.
160
     *
161
     * @return array
162
     */
163 6
    public function toArray()
164
    {
165
        return [
166 6
            'methods'    => $this->methods,
167 6
            'uri'        => $this->uri,
168 6
            'params'     => $this->params,
169 6
            'action'     => $this->action,
170 6
            'name'       => $this->name,
171 6
            'middleware' => $this->middleware,
172 6
            'domain'     => $this->domain,
173 2
        ];
174
    }
175
176
    /**
177
     * Convert the object into something JSON serializable.
178
     *
179
     * @return array
180
     */
181 3
    public function jsonSerialize()
182
    {
183 3
        return $this->toArray();
184
    }
185
186
    /**
187
     * Convert the object to its JSON representation.
188
     *
189
     * @param  int  $options
190
     *
191
     * @return string
192
     */
193 3
    public function toJson($options = 0)
194
    {
195 3
        return json_encode($this->jsonSerialize(), $options);
196
    }
197
}
198