1 | <?php |
||
34 | class Response |
||
35 | { |
||
36 | /** |
||
37 | * Api Version |
||
38 | * |
||
39 | * @var string number of version |
||
40 | */ |
||
41 | private $apiVersion; |
||
42 | |||
43 | /** |
||
44 | * Api Context |
||
45 | * |
||
46 | * @var string name of context |
||
47 | */ |
||
48 | private $context; |
||
49 | |||
50 | /** |
||
51 | * Output Format |
||
52 | * |
||
53 | * @var string json|array|object |
||
54 | */ |
||
55 | private $format; |
||
56 | |||
57 | /** |
||
58 | * Response constructor. |
||
59 | * |
||
60 | * @param array $opts Options |
||
61 | */ |
||
62 | public function __construct($opts = null) |
||
63 | { |
||
64 | $this->apiVersion = (isset($opts['apiVersion'])) ? $opts['apiVersion'] : '1.0'; |
||
65 | $this->context = (isset($opts['context'])) ? $opts['context'] : 'api'; |
||
66 | $this->format = (isset($opts['format'])) ? $opts['format'] : 'json'; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Generate error response |
||
71 | * |
||
72 | * @param integer $code number of error |
||
73 | * @param string $msg message to identify error |
||
74 | * @param array $extra extra data |
||
75 | * |
||
76 | * @return array|object|string |
||
77 | */ |
||
78 | 6 | public function error($code, $msg, $extra = []) |
|
95 | |||
96 | /** |
||
97 | * Generate success response |
||
98 | * |
||
99 | * @param array $data data |
||
100 | * |
||
101 | * @return array|object|string |
||
102 | */ |
||
103 | 9 | public function success($data) |
|
108 | |||
109 | /** |
||
110 | * Generate basic structure |
||
111 | * |
||
112 | * This structure is used in all the responses |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | 15 | private function basicStructure() |
|
123 | |||
124 | /** |
||
125 | * Generate output |
||
126 | * |
||
127 | * @param array|object $data output data |
||
128 | * |
||
129 | * @return array|object|string|false |
||
130 | */ |
||
131 | 15 | private function output($data) |
|
144 | } |
||
145 |