1
|
|
|
<?php |
2
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of the Gjson library. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* PHP Version 5, 7 |
11
|
|
|
* |
12
|
|
|
* LICENSE: This source file is subject to the MIT license that is available |
13
|
|
|
* through the world-wide-web at the following URI: |
14
|
|
|
* http://opensource.org/licenses/mit-license.php |
15
|
|
|
* |
16
|
|
|
* @category Src |
17
|
|
|
* @package Normeno\Gjson |
18
|
|
|
* @author Nicolas Ormeno <[email protected]> |
19
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
20
|
|
|
* @link https://github.com/normeno/gjson |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Normeno\Gjson; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Tests for Format |
27
|
|
|
* |
28
|
|
|
* @category Src |
29
|
|
|
* @package Normeno\Gjson |
30
|
|
|
* @author Nicolas Ormeno <[email protected]> |
31
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
32
|
|
|
* @link https://github.com/normeno/gjson |
33
|
|
|
*/ |
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 = []) |
79
|
|
|
{ |
80
|
|
|
$error = [ |
81
|
|
|
'error' => [ |
82
|
6 |
|
'code' => $code, |
83
|
2 |
|
'message' => $msg |
84
|
4 |
|
] |
85
|
4 |
|
]; |
86
|
|
|
|
87
|
6 |
|
if (!empty($extra)) { |
88
|
3 |
|
$extraErrors = (array_key_exists('errors', $extra)) ? $extra['errors'] : $extra; |
89
|
3 |
|
$error['error']['errors'] = $extraErrors; |
90
|
2 |
|
} |
91
|
|
|
|
92
|
6 |
|
$response = $this->basicStructure() + $error; |
93
|
6 |
|
return $this->output($response); |
94
|
|
|
} |
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) |
104
|
|
|
{ |
105
|
9 |
|
$response = $this->basicStructure() + $data; |
106
|
9 |
|
return $this->output($response); |
107
|
|
|
} |
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() |
117
|
|
|
{ |
118
|
|
|
return [ |
119
|
15 |
|
'apiVersion' => $this->apiVersion, |
120
|
15 |
|
'context' => $this->context |
121
|
10 |
|
]; |
122
|
|
|
} |
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) |
132
|
|
|
{ |
133
|
15 |
|
switch ($this->format) { |
134
|
15 |
|
case 'array': |
135
|
|
|
return (array)$data; |
136
|
15 |
|
case 'object': |
137
|
|
|
return (object)$data; |
138
|
15 |
|
case 'json': |
139
|
15 |
|
return json_encode($data); |
140
|
|
|
default: |
141
|
|
|
return json_encode($data); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|