Completed
Push — master ( 800138...f42006 )
by Matthias
02:36
created

RequestAbstract::getOperatingSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
    const OS_LINUX = 'linux';
46
    const OS_WINDOWS = 'windows';
47
    const OS_UNKNOWN = 'unknown';
48
    
49
    /**
50
     * @access protected
51
     * @var string
52
     */
53
    protected $_route;
54
    
55
    /**
56
     * @access protected
57
     * @var array
58
     */
59
    protected $_params = array();
60
    
61
    /**
62
     * @access protected
63
     * @var array
64
     */
65
    protected $_routeParams = array();
66
    
67
    /**
68
     * @access protected
69
     * @var string
70
     */
71
    protected $_base;
72
    
73
    /**
74
     * @access protected
75
     * @var string
76
     */
77
    protected $_locale;
78
    
79
    /**
80
     * @access protected
81
     * @var string
82
     */
83
    protected $_operatingSystem;
84
    
85
    /**
86
     * @access protected
87
     * @var array
88
     */
89
    protected $_data = array();
90
    
91
    /**
92
     * Constructs the object
93
     * 
94
     * @access public
95
     * @param string $route
96
     * @param array params
97
     * @param string $base
98
     * @param string $locale
99
     * @param string $operatingSystem
100
     * @param array $data
101
     */
102
    public function __construct($route, $params, $base, $locale, $operatingSystem, $data = array())
103
    {
104
        $this->_route = $route;
105
        $this->_locale = $locale;
106
        $this->_operatingSystem = $operatingSystem;
107
        
108
        if (is_array($params)) {
109
            $this->_params = $params; 
110
        }
111
        
112
        if (substr($base, -1) === '/') {
113
            $base = substr($base, 0, -1);
114
        }
115
        $this->_base = $base;
116
        
117
        if (is_array($data)) {
118
            $this->_data = $data;
119
        }
120
    }
121
    
122
    /**
123
     * Returns the route of the request.
124
     * 
125
     * @access public
126
     * @return string
127
     */
128
    public function getRoute()
129
    {
130
        return $this->_route;
131
    }
132
    
133
    /**
134
     * Sets the route of the request
135
     * 
136
     * @access public
137
     * @param string $route
138
     */
139
    public function setRoute($route)
140
    {
141
        $this->_route = $route;
142
    }
143
    
144
    /**
145
     * Returns true if the given param key exists.
146
     * 
147
     * @access public
148
     * @param string $key
149
     * @return string
150
     */
151
    public function hasParam($key)
152
    {
153
        return (isset($this->_params[$key]));
154
    }
155
    
156
    /**
157
     * Returns the value for the given param key. If the
158
     * key does not exists the function will return false.
159
     * 
160
     * @access public
161
     * @param string $key
162
     * @return mixed
163
     */
164
    public function getParam($key)
165
    {
166
        if (!$this->hasParam($key)) {
167
            return false;
168
        }
169
        
170
        return $this->_params[$key];
171
    }
172
    
173
    /**
174
     * Returns all params of the request
175
     * 
176
     * @access public
177
     * @return array
178
     */
179
    public function getParams()
180
    {
181
        return $this->_params;
182
    }
183
    
184
    /**
185
     * Adds an parameter which is detected in the route.
186
     * 
187
     * @access public
188
     * @param mixed $param
189
     */
190
    public function addRouteParam($param)
191
    {
192
        $this->_routeParams[] = $param;
193
    }
194
    
195
    /**
196
     * Sets the array of route params for the request
197
     * 
198
     * @access public
199
     * @param array $params
200
     * @return boolean
201
     */
202
    public function setRouteParams($params)
203
    {
204
        if (!is_array($params)) {
205
            return false;
206
        }
207
        
208
        $this->_routeParams = $params;
209
        
210
        return true;
211
    }
212
    
213
    /**
214
     * Returns the route param for the given index.
215
     * 
216
     * @access public
217
     * @param integer $index
218
     * @return string|boolean
219
     */
220
    public function getRouteParam($index)
221
    {
222
        if (!isset($this->_routeParams[$index])) {
223
            return false;
224
        }
225
        
226
        return $this->_routeParams[$index];
227
    }
228
    
229
    /**
230
     * Returns the delimitier, which is used to split the route
231
     * into parts.
232
     * 
233
     * @access public
234
     * @return string
235
     */
236
    abstract public function getRouteDelimiter();
237
    
238
    /**
239
     * Returns the correct url for the given url part
240
     * 
241
     * @access public
242
     * @param string $routePart
243
     * @return string
244
     */
245
    public function getFullRoute($routePart = '')
246
    {
247
        if ($routePart == '') {
248
            $routePart = $this->_route;
249
        }
250
        
251
        $delimiter = $this->getRouteDelimiter();
252
        if (substr($routePart, 0, strlen($delimiter)) !== $delimiter) {
253
            $routePart = $delimiter . $routePart;
254
        }
255
        
256
        $posPoint = strrpos($routePart, '.');
257
        if (substr($routePart, -1) !== '/' && ($posPoint === false || $posPoint < strrpos($routePart, '/'))) {
258
            $routePart .= '/';
259
        }
260
261
        return $this->_base . $routePart;
262
    }
263
    
264
    /**
265
     * Returns the locale of the request
266
     * 
267
     * @access public
268
     * @return string
269
     */
270
    public function getLocale()
271
    {
272
        return $this->_locale;
273
    }
274
    
275
    /**
276
     * Returns the operating system of the machine
277
     *
278
     * @access public
279
     * @return string
280
     */
281
    public function getOperatingSystem()
282
    {
283
        return $this->_operatingSystem;
284
    }
285
}
286