-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSight.Response.php
executable file
·121 lines (107 loc) · 2.3 KB
/
Sight.Response.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Sight;
class Response {
public $data = NULL;
public $httpCode = "200 OK";
public $document = NULL;
function __construct() {
$this->headers = array();
}
function send($text=NULL) {
header("HTTP/1.1 " . $this->httpCode);
foreach($this->headers as $header)
header($header);
if($text !== NULL)
echo $text;
if($this->document !== NULL)
$this->document->send($this->data);
}
}
class EnhancedStdClass {
function copy() {
$clone = clone $this;
var_dump($clone);
die();
}
function __toString() {
$result = "";
foreach($this as $key=>$value) {
try {
$result .= (string)$key;
} catch(Exception $e) {
$result .= "{unknown key}";
}
$result .= " => ";
try {
$result .= (string)$value;
} catch(Exception $e) {
$result .= "{unknown value}";
}
$result .= "\n";
}
return $result;
}
function __clone() {
$result = new EnhancedStdClass();
foreach($this as $key=>$value) {
if(is_object($value)) {
if(is_callable($value)) {
$result->$key = $value;
} else {
$result->$key = clone $value;
}
} else {
$result->$key = $value;
}
}
return $result;
}
}
class ResponseData {
function __construct() {
$this->data = new EnhancedStdClass();
}
function set($path,$value) {
$dataPath = explode(".",$path);
$current = $this->data;
for($i=0; $i<count($dataPath)-1; $i++) {
$path = $dataPath[$i];
if(!isset($current->$path) || !is_object($current->$path)) {
$current->$path = new EnhancedStdClass();
}
$current = $current->$path;
}
$path = $dataPath[$i];
$current->$path = $value;
}
function get($path) {
$dataPath = explode(".",$path);
$current = $this->data;
for($i=0; $i<count($dataPath)-1; $i++) {
$path = $dataPath[$i];
$current = $this->getValueAt($current,$path);
if(is_null($current))
return null;
}
$path = $dataPath[$i];
return $this->getValueAt($current,$path);
}
function copy() {
return clone $this;
}
private function getValueAt($obj,$path) {
if(is_object($obj)) {
if(isset($obj->$path))
return $obj->$path;
} elseif(is_array($obj)) {
if(isset($obj[$path]))
return $obj[$path];
}
return null;
}
function __clone() {
$responseData = new ResponseData();
$responseData->data = clone $this->data;
return $responseData;
}
}