-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSendComponent.php
More file actions
67 lines (55 loc) · 1.37 KB
/
JSendComponent.php
File metadata and controls
67 lines (55 loc) · 1.37 KB
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
<?php
App::uses('Component', 'Controller');
class JSendComponent extends Component {
public $allowedTypes = array(
'success',
'fail',
'error'
);
public function startup(Controller $controller) {
$this->controller = $controller;
}
/**
* To be used when the data could be processed by the model
* @param [type] $data [description]
*/
public function setJsonSuccess($data){
return array(
'status' => 'success',
'data' => $data
);
}
/**
* To be used when the data didn't pass the validation,
* will usually receive the validation error from the model
* @param [type] $data [description]
*/
public function setJsonFail($data){
return array(
'status' => 'fail',
'data' => $data
);
}
/**
* To be used when something wrong happened, wrong method accessed or user doesn't have permission
* @param [type] $message [description]
*/
public function setJsonError($message){
return array(
'status' => 'error',
'message' => $message
);
}
public function setJson($data, $type = 'success'){
if (in_array($type, $this->allowedTypes)) {
$method = "setJson" . ucfirst($type);
return $this->controller->set('jsonResponse', $this->{$method}($data));
}
throw new NotFoundException('only three json types allowed');
}
public function setSerialize(){
$this->controller->set(array(
'_serialize' => 'jsonResponse'
));
}
}