1
|
|
|
<?php namespace Comodojo\Dispatcher\Response; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\ToString as ToStringTrait; |
4
|
|
|
use \Comodojo\Dispatcher\Components\HttpStatusCodes; |
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @package Comodojo Dispatcher |
9
|
|
|
* @author Marco Giovinazzi <[email protected]> |
10
|
|
|
* @author Marco Castiello <[email protected]> |
11
|
|
|
* @license GPL-3.0+ |
12
|
|
|
* |
13
|
|
|
* LICENSE: |
14
|
|
|
* |
15
|
|
|
* This program is free software: you can redistribute it and/or modify |
16
|
|
|
* it under the terms of the GNU Affero General Public License as |
17
|
|
|
* published by the Free Software Foundation, either version 3 of the |
18
|
|
|
* License, or (at your option) any later version. |
19
|
|
|
* |
20
|
|
|
* This program is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU Affero General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* You should have received a copy of the GNU Affero General Public License |
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
class Status { |
30
|
|
|
|
31
|
|
|
use ToStringTrait; |
32
|
|
|
|
33
|
|
|
private $status_code = 200; |
34
|
|
|
|
35
|
|
|
private $codes; |
36
|
|
|
|
37
|
|
|
public function __construct() { |
38
|
|
|
|
39
|
|
|
$this->codes = new HttpStatusCodes; |
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function get() { |
44
|
|
|
|
45
|
|
|
return $this->status_code; |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function set($code) { |
50
|
|
|
|
51
|
|
|
if ( !$this->codes->exists($code) ) { |
52
|
|
|
|
53
|
|
|
throw new Exception("Invalid HTTP Status Code $code"); |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->status_code = $code; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function description($code=null) { |
64
|
|
|
|
65
|
|
|
if ( is_null($code) ) return $this->codes->getMessage($this->status_code); |
66
|
|
|
|
67
|
|
|
return $this->codes->getMessage($code); |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|