-
Notifications
You must be signed in to change notification settings - Fork 4
/
active_resource.php
218 lines (170 loc) · 5.95 KB
/
active_resource.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
require dirname(__FILE__).'/lib/core_ext.php';
require dirname(__FILE__).'/lib/curl.php';
class ActiveResource {
protected $attributes;
public $site = 'http://localhost:3000';
public $user, $password;
public $timeout = 2000;
public $connection;
public $response;
public $extension = 'xml';
function __construct($attributes = array()) {
$this->attributes = $attributes;
$this->connection = new Curl;
}
/* Class Methods */
static function create($attributes = array()) {
$class = get_called_class();
$instance = new $class($attributes);
if ($id = $instance->save()) {
return $instance;
}
return false;
}
static function find($scope, $options = array()) {
$class = get_called_class();
switch ($scope) {
case 'all':
return self::find_every($class, $options);
case 'first':
$collection = self::find_every($class, $options);
return $collection[0];
case 'last':
return end(self::find_every($class, $options));
default:
return self::find_single($scope, $class, $options);
}
}
/* Instance methods */
function get($path = null, $params = array()) {
}
function post($path = null, $params = array()) {
$data = $this->request_body($this->attributes);
$this->connection->put($this->element_url(), $data);
}
function put($path = null, $params = array()) {
}
function delete($path = null, $params = array()) {
$this->collection->delete($this->element_url());
}
function exists($id = null) {
# class Method
if (!isset($this) && $class = get_called_class()) {
$obj = new $class();
$obj->id = $id;
return $obj->exists();
}
# instance Method
if (!$this->id) return false;
$class = get_class($this);
$response = $this->connection->get(self::element_url($this->id, $class));
return $response->headers["Status-Code"] == 200;
}
function destroy($id = null, $params = array()) {
# class Method
if (!isset($this) && $class = get_called_class()) {
$obj = new $class(array('id'=> $id));
return $obj->destroy();
}
# instance Method
$class = get_class($this);
if (!$this->id) return false;
$response = $this->connection->delete(self::element_url($this->id, $class));
return $response->headers["Status-Code"] == 200;
}
function save() {
$data = $this->request_body($this->attributes);
$class = get_class($this);
if (isset($this->id)) {
$this->connection->headers = array("Content-Type" => "application/xml");
$resp = $this->connection->put(self::element_url($this->id, $class), $data);
return true;
}
$this->connection->headers = array("Content-Type" => "application/xml");
$response = $this->connection->post(self::collection_url($class), $data);
# hack to get ID after its saved
$this->id = end(explode('/', $response->headers["Location"]));
return $this->id;
}
/* Magic Methids */
function __get($attr) {
$attr = str_replace('_', '-', $attr);
return $this->attributes[$attr];
}
function __set($attr, $value = null) {
$attr = str_replace('-', '_', $attr);
return $this->attributes[$attr] = $value;
}
function __isset($attr) {
return isset($this->attributes[$attr]);
}
function __callStatic($attr) {
}
private
static function find_single($scope, $class, $options = array()) {
$connection = new Curl;
$url = self::element_url($scope, $class, $options);
$response = $connection->get($url);
if ($response->headers["Status-Code"] == "404") {
throw new Exception("Object not found");
}
$xml = simplexml_load_string($response->body)->xpath("//". strtolower($class));
return new $class((array) $xml[0]);
}
static function find_every($class, $options = array()) {
$connection = new Curl;
$url = self::collection_url($class, $options);
$response = $connection->get($url);
if ($response->headers["Status-Code"] == "404") {
throw new Exception("Object not found");
}
$results = simplexml_load_string($response->body)->xpath("//". strtolower($class));
if (!is_array($results))
$results = array($results);
foreach ($results as &$result) {
$result = new $class((array) $result);
}
return $results;
}
static function config($class) {
extract(get_class_vars($class));
if ($site[strlen($site) - 1] === '/')
$site = substr($site, 0, -1);
$parts = parse_url($site);
if (isset($parts['user']) && isset($parts['password'])) {
$this->user = $parts['user'];
$this->password = $parts['password'];
}
return compact('user', 'password', 'site', 'timeout', 'extension');
}
static function collection_name($class = null) {
if (!$class)
$class = get_called_class();
return strtolower(pluralize($class));
}
static function collection_url($class, $options = array()) {
extract(self::config($class));
$collection = self::collection_name($class);
$query = !empty($options) ? '?' . http_build_query($options) : '';
$url = "{$site}/{$collection}.{$extension}{$query}";
return $url;
}
static function element_url($id, $class, $options = array()) {
extract(self::config($class));
$collection = self::collection_name($class);
$query = !empty($options) ? '?' . http_build_query($options) : '';
$url = "{$site}/{$collection}/{$id}.{$extension}{$query}";
return $url;
}
function request_body($params) {
$src = '';
$element = strtolower(get_class($this));
foreach ($params as $k => $v) {
if ($k === 'updated_at'|| $k === 'created_at') continue;
$src .= "<{$k}>". utf8_encode($v) ."</{$k}>";
}
return "<{$element}>{$src}</{$element}>";
}
}
?>