Completed
Push — master ( eda34b...67914c )
by Matthias
02:34
created

RequestAbstract::getFullRoute()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 17
rs 8.8571
cc 5
eloc 9
nc 8
nop 1
1
<?php
2
/*
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2015 zepi
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 *
25
 */
26
27
/**
28
 * The abstract RequestAbstract is the base for all request types
29
 * 
30
 * @package Zepi\Turbo\Request
31
 * @author Matthias Zobrist <[email protected]>
32
 * @copyright Copyright (c) 2015 zepi
33
 */
34
35
namespace Zepi\Turbo\Request;
36
37
/**
38
 * The abstract RequestAbstract is the base for all request types
39
 * 
40
 * @author Matthias Zobrist <[email protected]>
41
 * @copyright Copyright (c) 2015 zepi
42
 */
43
abstract class RequestAbstract
44
{
45
    /**
46
     * @access protected
47
     * @var string
48
     */
49
    protected $_route;
50
    
51
    /**
52
     * @access protected
53
     * @var array
54
     */
55
    protected $_params = array();
56
    
57
    /**
58
     * @access protected
59
     * @var array
60
     */
61
    protected $_routeParams = array();
62
    
63
    /**
64
     * @access protected
65
     * @var string
66
     */
67
    protected $_base;
68
    
69
    /**
70
     * @access protected
71
     * @var string
72
     */
73
    protected $_locale;
74
    
75
    /**
76
     * @access protected
77
     * @var array
78
     */
79
    protected $_data = array();
80
    
81
    /**
82
     * Constructs the object
83
     * 
84
     * @access public
85
     * @param string $route
86
     * @param array params
87
     * @param string $base
88
     * @param string $locale
89
     * @param array $data
90
     */
91
    public function __construct($route, $params, $base, $locale, $data = array())
92
    {
93
        $this->_route = $route;
94
        $this->_locale = $locale;
95
        
96
        if (is_array($params)) {
97
            $this->_params = $params; 
98
        }
99
        
100
        if (substr($base, -1) === '/') {
101
            $base = substr($base, 0, -1);
102
        }
103
        $this->_base = $base;
104
        
105
        if (is_array($data)) {
106
            $this->_data = $data;
107
        }
108
    }
109
    
110
    /**
111
     * Returns the route of the request.
112
     * 
113
     * @access public
114
     * @return string
115
     */
116
    public function getRoute()
117
    {
118
        return $this->_route;
119
    }
120
    
121
    /**
122
     * Sets the route of the request
123
     * 
124
     * @access public
125
     * @param string $route
126
     */
127
    public function setRoute($route)
128
    {
129
        $this->_route = $route;
130
    }
131
    
132
    /**
133
     * Returns true if the given param key exists.
134
     * 
135
     * @access public
136
     * @param string $key
137
     * @return string
138
     */
139
    public function hasParam($key)
140
    {
141
        return (isset($this->_params[$key]));
142
    }
143
    
144
    /**
145
     * Returns the value for the given param key. If the
146
     * key does not exists the function will return false.
147
     * 
148
     * @access public
149
     * @param string $key
150
     * @return mixed
151
     */
152
    public function getParam($key)
153
    {
154
        if (!$this->hasParam($key)) {
155
            return false;
156
        }
157
        
158
        return $this->_params[$key];
159
    }
160
    
161
    /**
162
     * Returns all params of the request
163
     * 
164
     * @access public
165
     * @return array
166
     */
167
    public function getParams()
168
    {
169
        return $this->_params;
170
    }
171
    
172
    /**
173
     * Adds an parameter which is detected in the route.
174
     * 
175
     * @access public
176
     * @param mixed $param
177
     */
178
    public function addRouteParam($param)
179
    {
180
        $this->_routeParams[] = $param;
181
    }
182
    
183
    /**
184
     * Sets the array of route params for the request
185
     * 
186
     * @access public
187
     * @param array $params
188
     * @return boolean
189
     */
190
    public function setRouteParams($params)
191
    {
192
        if (!is_array($params)) {
193
            return false;
194
        }
195
        
196
        $this->_routeParams = $params;
197
        
198
        return true;
199
    }
200
    
201
    /**
202
     * Returns the route param for the given index.
203
     * 
204
     * @access public
205
     * @param integer $index
206
     * @return string|boolean
207
     */
208
    public function getRouteParam($index)
209
    {
210
        if (!isset($this->_routeParams[$index])) {
211
            return false;
212
        }
213
        
214
        return $this->_routeParams[$index];
215
    }
216
    
217
    /**
218
     * Returns the delimitier, which is used to split the route
219
     * into parts.
220
     * 
221
     * @access public
222
     * @return string
223
     */
224
    abstract public function getRouteDelimiter();
225
    
226
    /**
227
     * Returns the correct url for the given url part
228
     * 
229
     * @access public
230
     * @param string $routePart
231
     * @return string
232
     */
233
    public function getFullRoute($routePart = '')
234
    {
235
        if ($routePart == '') {
236
            $routePart = $this->_route;
237
        }
238
        
239
        $delimiter = $this->getRouteDelimiter();
240
        if (substr($routePart, 0, strlen($delimiter)) !== $delimiter) {
241
            $routePart = $delimiter . $routePart;
242
        }
243
        
244
        if (substr($routePart, -1) !== '/' && strrpos($routePart, '.') < strrpos($routePart, '/')) {
245
            $routePart .= '/';
246
        }
247
        
248
        return $this->_base . $routePart;
249
    }
250
    
251
    /**
252
     * Returns the locale of the request
253
     * 
254
     * @access public
255
     * @return string
256
     */
257
    public function getLocale()
258
    {
259
        return $this->_locale;
260
    }
261
}
262