Result::getStatus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Route
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Route;
16
17
use Phossa2\Shared\Base\ObjectAbstract;
18
use Phossa2\Route\Interfaces\RouteInterface;
19
use Phossa2\Route\Interfaces\ResultInterface;
20
21
/**
22
 * Result
23
 *
24
 * Mathed result
25
 *
26
 * @package Phossa2\Route
27
 * @author  Hong Zhang <[email protected]>
28
 * @see     ObjectAbstract
29
 * @see     ResultInterface
30
 * @version 2.0.1
31
 * @since   2.0.0 added
32
 */
33
class Result extends ObjectAbstract implements ResultInterface
34
{
35
    /**
36
     * http status
37
     *
38
     * @var    int
39
     * @access protected
40
     */
41
    protected $status = Status::NOT_FOUND;
42
43
    /**
44
     * parsed parameters
45
     *
46
     * @var    array
47
     * @access protected
48
     */
49
    protected $parameters = [];
50
51
    /**
52
     * the handler
53
     *
54
     * @var    mixed
55
     * @access protected
56
     */
57
    protected $handler;
58
59
    /**
60
     * the matched route
61
     *
62
     * @var    RouteInterface
63
     * @access protected
64
     */
65
    protected $route;
66
67
    /**
68
     * URI path
69
     *
70
     * @var    string
71
     * @access protected
72
     */
73
    protected $path;
74
75
    /**
76
     * HTTP method
77
     *
78
     * @var    string
79
     * @access protected
80
     */
81
    protected $method;
82
83
    /**
84
     * @param  string $httpMethod HTTP method
85
     * @param  string $uriPath the URI path to match with
86
     * @access public
87
     */
88
    public function __construct(
89
        /*# string */ $httpMethod,
90
        /*# string */ $uriPath
91
    ) {
92
        $this->method = $httpMethod;
93
        $this->setPath($uriPath);
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function getPath()/*# : string */
100
    {
101
        return $this->path;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function getMethod()/*# : string */
108
    {
109
        return $this->method;
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function getStatus()/*# : int */
116
    {
117
        return $this->status;
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123
    public function setStatus(/*# int */ $status)
124
    {
125
        $this->status = (int) $status;
126
        return $this;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function getParameters()/*# : array */
133
    {
134
        return $this->parameters;
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140
    public function setParameters(array $params)
141
    {
142
        if (!empty($params)) {
143
            $this->parameters = array_replace($this->parameters, $params);
144
        }
145
        return $this;
146
    }
147
148
    /**
149
     * {@inheritDoc}
150
     */
151
    public function setHandler($handler)
152
    {
153
        $this->handler = $handler;
154
        return $this;
155
    }
156
157
    /**
158
     * {@inheritDoc}
159
     */
160
    public function getHandler()
161
    {
162
        return $this->handler;
163
    }
164
165
    /**
166
     * {@inheritDoc}
167
     */
168
    public function setRoute(RouteInterface $route)
169
    {
170
        $this->route = $route;
171
        return $this;
172
    }
173
174
    /**
175
     * {@inheritDoc}
176
     */
177
    public function getRoute()
178
    {
179
        return $this->route;
180
    }
181
182
    /**
183
     * Set the path
184
     * @param  string $uriPath
185
     * @access protected
186
     */
187
    protected function setPath(/*# string */ $uriPath)
188
    {
189
        $pos = strpos($uriPath, '?');
190
        if (false !== $pos) {
191
            $path = substr($uriPath, 0, $pos);
192
            parse_str(substr($uriPath, $pos + 1), $this->parameters);
193
        } else {
194
            $path = $uriPath;
195
        }
196
197
        // remove trailing '/'
198
        $this->path = rtrim($path, '/');
199
    }
200
}
201