|
1
|
|
|
<?php namespace Comodojo\Dispatcher\Response; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Traits\ToStringTrait; |
|
4
|
|
|
use \Exception; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @package Comodojo Dispatcher |
|
8
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
9
|
|
|
* @author Marco Castiello <[email protected]> |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* |
|
12
|
|
|
* LICENSE: |
|
13
|
|
|
* |
|
14
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
15
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
16
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
17
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
18
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
19
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
20
|
|
|
* THE SOFTWARE. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
class Content { |
|
24
|
|
|
|
|
25
|
|
|
use ToStringTrait; |
|
26
|
|
|
|
|
27
|
|
|
private $content; |
|
28
|
|
|
|
|
29
|
|
|
private $type = "text/plain"; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
private $charset = "utf-8"; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
6 |
|
public function get() { |
|
34
|
|
|
|
|
35
|
6 |
|
return $this->content; |
|
36
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
7 |
|
public function set($content = null) { |
|
40
|
|
|
|
|
41
|
7 |
|
if (!is_scalar($content) && $content != null) { |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
throw new Exception("Invalid HTTP content"); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
7 |
|
$this->content = $content; |
|
48
|
|
|
|
|
49
|
7 |
|
return $this; |
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
public function getType() { |
|
54
|
|
|
|
|
55
|
2 |
|
return $this->type; |
|
56
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function setType($type) { |
|
60
|
|
|
|
|
61
|
1 |
|
$this->type = $type; |
|
62
|
|
|
|
|
63
|
1 |
|
return $this; |
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
public function type($type = null) { |
|
68
|
|
|
|
|
69
|
2 |
|
return is_null($type) ? $this->getType() : $this->setType($type); |
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
public function getCharset() { |
|
74
|
|
|
|
|
75
|
2 |
|
return $this->charset; |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public function setCharset($charset) { |
|
80
|
|
|
|
|
81
|
1 |
|
$this->charset = $charset; |
|
82
|
|
|
|
|
83
|
1 |
|
return $this; |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
public function charset($charset = null) { |
|
88
|
|
|
|
|
89
|
2 |
|
return is_null($charset) ? $this->getCharset() : $this->setCharset($charset); |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
public function getLength() { |
|
94
|
|
|
|
|
95
|
2 |
|
return strlen($this->content); |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
public function length() { |
|
100
|
|
|
|
|
101
|
2 |
|
return $this->getLength(); |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'or with double quotes"literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\') and the backslash (\\). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is ValueIf your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.