1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saltwater\App\Provider; |
4
|
|
|
|
5
|
|
|
use Saltwater\Server as S; |
6
|
|
|
use Saltwater\Salt\Provider; |
7
|
|
|
|
8
|
|
|
class Response extends Provider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Redirect the client to a different URL |
12
|
|
|
* |
13
|
|
|
* @param $url |
14
|
|
|
*/ |
15
|
|
|
public function redirect($url) |
16
|
|
|
{ |
17
|
|
|
header('HTTP/1.1 307 Temporary Redirect'); |
18
|
|
|
|
19
|
|
|
header('Location: ' . $url); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Output data as JSON |
24
|
|
|
* |
25
|
|
|
* @param object|array $data |
26
|
|
|
*/ |
27
|
|
|
public function json($data) |
28
|
|
|
{ |
29
|
|
|
header('HTTP/1.0 200 OK'); |
30
|
|
|
|
31
|
|
|
header('Content-type: application/json'); |
32
|
|
|
|
33
|
|
|
header('X-Execution-Time: ' . $this->executionTime() . 'ms'); |
34
|
|
|
|
35
|
|
|
return json_encode($this->prepareOutput($data)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Output data as a plain text or JSON, depending on its type |
40
|
|
|
* |
41
|
|
|
* @param object|object[]|string $data |
42
|
|
|
*/ |
43
|
|
|
public function response($data) |
44
|
|
|
{ |
45
|
|
|
if (is_object($data) || is_array($data)) { |
46
|
|
|
return $this->json($data); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->plain($data); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Output data as plain text |
54
|
|
|
* |
55
|
|
|
* @param string $data |
56
|
|
|
*/ |
57
|
|
|
public function plain($data) |
58
|
|
|
{ |
59
|
|
|
header('HTTP/1.0 200 OK'); |
60
|
|
|
|
61
|
|
|
header('X-Execution-Time: ' . $this->executionTime() . 'ms'); |
62
|
|
|
|
63
|
|
|
return $data; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function executionTime() |
67
|
|
|
{ |
68
|
|
|
$time = microtime(true); |
69
|
|
|
|
70
|
|
|
return round(($time - S::$start) * 1000, 2); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Ensure we are encoding numeric properties as numbers, not strings |
75
|
|
|
* |
76
|
|
|
* @param object|array $input |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function prepareOutput($input) |
81
|
|
|
{ |
82
|
|
|
if (is_array($input)) { |
83
|
|
|
return $this->outputArray($input); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->output($input); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function output($input) |
90
|
|
|
{ |
91
|
|
|
return $this->convertNumeric($input); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function outputArray($input) |
95
|
|
|
{ |
96
|
|
|
$return = array(); |
97
|
|
|
foreach ($input as $k => $v) { |
98
|
|
|
$return[$k] = $this->output($v); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $return; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Convert all numeric properties of an object into floats or integers |
106
|
|
|
* |
107
|
|
|
* @param object $object |
108
|
|
|
* |
109
|
|
|
* @return object |
110
|
|
|
*/ |
111
|
|
|
protected function convertNumeric($object) |
112
|
|
|
{ |
113
|
|
|
if ($object instanceof \RedBean_OODBBean) { |
114
|
|
|
$object = $object->export(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
foreach (get_object_vars($object) as $k => $v) { |
118
|
|
|
if (is_numeric($v)) { |
119
|
|
|
$object->$k = $this->stringToNum($v); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $object; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function stringToNum($value) |
127
|
|
|
{ |
128
|
|
|
if (strpos($value, '.') !== false) { |
129
|
|
|
return (float) $value; |
130
|
|
|
} else { |
131
|
|
|
return (int) $value; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|