Completed
Pull Request — master (#115)
by Phil
02:51
created

Route::getMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace League\Route;
4
5
use League\Container\ImmutableContainerAwareInterface;
6
use League\Container\ImmutableContainerAwareTrait;
7
use League\Route\Http\RequestAwareInterface;
8
use League\Route\Http\ResponseAwareInterface;
9
use League\Route\Middleware\StackAwareInterface as MiddlewareAwareInterface;
10
use League\Route\Middleware\StackAwareTrait as MiddlewareAwareTrait;
11
use League\Route\Strategy\StrategyAwareInterface;
12
use League\Route\Strategy\StrategyAwareTrait;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use RuntimeException;
16
17
class Route implements ImmutableContainerAwareInterface, MiddlewareAwareInterface, StrategyAwareInterface
18
{
19
    use ImmutableContainerAwareTrait;
20
    use MiddlewareAwareTrait;
21
    use RouteConditionTrait;
22
    use StrategyAwareTrait;
23
24
    /**
25
     * @var string|callable
26
     */
27
    protected $callable;
28
29
    /**
30
     * @var \League\Route\RouteGroup
31
     */
32
    protected $group;
33
34
    /**
35
     * @var string[]
36
     */
37
    protected $methods = [];
38
39
    /**
40
     * @var string
41
     */
42
    protected $path;
43
44
    /**
45
     * Dispatch the route via the attached strategy.
46
     *
47
     * @param array $vars
48
     *
49
     * @return \League\Route\Middleware\ExecutionChain
50
     */
51
    public function getExecutionChain(array $vars)
52
    {
53
        return $this->getStrategy()->getExecutionChain($this, $vars);
54
    }
55
56
    /**
57
     * Get the callable.
58
     *
59
     * @throws \RuntimeException
60
     *
61
     * @return callable
62
     */
63
    public function getCallable()
64
    {
65
        $callable = $this->callable;
66
67
        if (is_string($callable) && strpos($callable, '::') !== false) {
68
            $callable = explode('::', $callable);
69
        }
70
71
        if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) {
72
            $callable = [$callable[0], $callable[1]];
73
        }
74
75
        if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) {
76
            $class = ($this->getContainer()->has($callable[0]))
77
                   ? $this->getContainer()->get($callable[0])
78
                   : new $callable[0];
79
80
            $callable = [$class, $callable[1]];
81
        }
82
83
        if (! is_callable($callable)) {
84
            throw new RuntimeException(
85
                sprintf(
86
                    'Invalid class method provided for: %s::%s',
87
                    get_class($class),
88
                    $callable[1]
89
                )
90
            );
91
        }
92
93
        return $callable;
94
    }
95
96
    /**
97
     * Set the callable.
98
     *
99
     * @param string|callable $callable
100
     *
101
     * @return \League\Route\Route
102
     */
103
    public function setCallable($callable)
104
    {
105
        $this->callable = $callable;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Get the parent group.
112
     *
113
     * @return \League\Route\RouteGroup
114
     */
115
    public function getParentGroup()
116
    {
117
        return $this->group;
118
    }
119
120
    /**
121
     * Set the parent group.
122
     *
123
     * @param \League\Route\RouteGroup $group
124
     *
125
     * @return \League\Route\Route
126
     */
127
    public function setParentGroup(RouteGroup $group)
128
    {
129
        $this->group = $group;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Get the path.
136
     *
137
     * @return string
138
     */
139
    public function getPath()
140
    {
141
        return $this->path;
142
    }
143
144
    /**
145
     * Set the path.
146
     *
147
     * @param string $path
148
     *
149
     * @return \League\Route\Route
150
     */
151
    public function setPath($path)
152
    {
153
        $this->path = $path;
154
155
        return $this;
156
    }
157
158
    /**
159
     * Get the methods.
160
     *
161
     * @return string[]
162
     */
163
    public function getMethods()
164
    {
165
        return $this->methods;
166
    }
167
168
    /**
169
     * Get the methods.
170
     *
171
     * @param string[] $methods
172
     *
173
     * @return \League\Route\Route
174
     */
175
    public function setMethods(array $methods)
176
    {
177
        $this->methods = $methods;
178
179
        return $this;
180
    }
181
}
182