/
abstract.php
executable file
·77 lines (55 loc) · 1.37 KB
/
abstract.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
<?php
abstract class Api_Abstract {
protected $request;
protected $response;
protected $success = false;
protected $errors = array();
protected $answer = array();
public function __construct(Api_Request $request) {
$this->request = $request;
}
abstract public function process();
public function process_request() {
try {
$this->process();
} catch (Error_Api $e) {
$this->add_error($e->getCode(), $e->getMessage());
$this->set_success(false);
}
$response_class = $this->request->get_response_class();
$this->response = new $response_class(
$this->success,
$this->errors,
$this->answer
);
return $this;
}
public function send_headers() {
$headers = $this->response->get_headers();
ob_end_clean();
foreach ($headers as $key => $header) {
header("$key: $header");
}
return $this;
}
public function get_response() {
return $this->response->get();
}
protected function add_error($code, $error = '') {
$this->errors[] = array($code, (String) $error);
}
protected function answer($data) {
foreach ($data as $key => $item) {
$this->add_answer($key, $item);
}
}
protected function add_answer($key, $data) {
$this->answer[$key] = $data;
}
protected function set_success($success) {
$this->success = (bool) $success;
}
protected function get($value = false) {
return $this->request->get($value);
}
}