-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRest.php
179 lines (156 loc) · 4.68 KB
/
Rest.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php namespace Devtools;
use Exception;
class Rest extends Response
{
public $method;
public $request;
public $parameters;
public $response;
private $id;
private $log;
public function __construct(
PDORepository $repository,
Log $log
) {
$this->repository = $repository;
$this->log = $log;
$this->setMethod();
$this->setRequest();
$this->setParameters();
if (!isset($_SERVER['phpspec'])) {
$this->process();
}
}
private function setMethod()
{
$this->method = $_SERVER['REQUEST_METHOD'];
}
private function setRequest()
{
$this->request = $this->getRequest();
}
private function setParameters()
{
$parameters = !empty($_REQUEST) ? $_REQUEST : null;
$temp = array();
if (!empty($parameters)) {
foreach ($parameters as $key => $value) {
$value = explode(',', $value);
if (is_array($value)) {
$temp2 = array();
foreach ($value as $v) {
array_push($temp2, count($value)>1 ? $this->quote($v) : $v);
}
$value = count($temp2) > 1
? $temp2
: $temp2[0];
}
$temp[$key] = $value;
}
}
$this->parameters = $temp;
}
public function process()
{
if (empty($this->request)) {
throw new Exception('Invalid request.');
}
switch ($this->method) {
case 'GET':
$table = array_shift($this->request);
extract($this->buildGetSQL($table));
$result = $this->repository->query($sql, $params, true);
if (!$result) {
throw new Exception('Data not found.');
}
$this->data($result);
return $result;
break;
}
}
public static function getRoot()
{
$cwd = isset($_SERVER['phpspec']) ? 'api' : getCWD();
$first_dir = explode('/', $_SERVER['REQUEST_URI']);
$first_dir = $first_dir[1];
$path_array = explode('/', $cwd);
$path = array();
$count = count($path_array);
for ($p = 0; $p < $count; $p++) {
if ($path_array[$p] === $first_dir) {
break;
}
}
for ($p; $p < $count; $p++) {
array_push($path, $path_array[$p]);
}
return '/'.implode('/', $path).'/';
}
private function getCols()
{
if (!empty($this->request)) {
$count = count($this->request);
if (is_numeric($this->request[0])) {
$this->id = $this->request[0];
} else {
$cols = array();
for ($i=0; $i < $count; $i++) {
array_push($cols, $this->request[$i]);
}
$cols = implode(',', $cols);
}
}
return !empty($cols) ? $cols : '*';
}
private function buildGetSQL($table)
{
$cols = $this->getCols();
$sql = "SELECT {$cols}
FROM {$table}";
$params = array();
if (isset($this->id)) {
$sql .= " WHERE {$table}_id=:id";
$params['id'] = $this->id;
} else if (!empty($this->parameters)) {
$sql .= " WHERE ";
$first = true;
foreach ($this->parameters as $key=>$value) {
if (!$first) {
$sql .= " AND ";
} else {
$first = false;
}
$sql .= $key;
if (is_array($value)) {
$sql .= ' IN ('.implode(',', $value).')';
} else {
$sql .= '=:'.$key;
$params[$key] = $value;
}
}
}
return array('sql' => Format::stripWhitespace($sql), 'params' => $params);;
}
private function quote($data)
{
return is_numeric($data) ? $data : "'$data'";
}
public static function call($url)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if ($response=curl_exec($curl)) {
curl_close($curl);
return json_decode($response);
} else {
$info = curl_getinfo($curl);
curl_close($curl);
trigger_error($info);
}
}
public static function getRequest()
{
$requestURI = explode('?', $_SERVER['REQUEST_URI']);
return explode('/', substr($requestURI[0], strlen(Rest::getRoot())));
}
}