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 string $apiVersion number of version |
61
|
|
|
* @param string $context name of context |
62
|
|
|
* @param string $format json|array|object |
63
|
|
|
*/ |
64
|
|
|
public function __construct($apiVersion = '1.0', $context = 'api', $format = 'json') |
65
|
|
|
{ |
66
|
|
|
$this->apiVersion = $apiVersion; |
67
|
|
|
$this->context = $context; |
68
|
|
|
$this->format = $format; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Generate error response |
73
|
|
|
* |
74
|
|
|
* @param integer $code number of error |
75
|
|
|
* @param string $msg message to identify error |
76
|
|
|
* @param array $extra extra data |
77
|
|
|
* |
78
|
|
|
* @return array|object|string |
79
|
|
|
*/ |
80
|
6 |
|
public function error($code, $msg, $extra = []) |
81
|
|
|
{ |
82
|
|
|
$error = [ |
83
|
|
|
'error' => [ |
84
|
6 |
|
'code' => $code, |
85
|
2 |
|
'message' => $msg |
86
|
4 |
|
] |
87
|
4 |
|
]; |
88
|
|
|
|
89
|
6 |
|
if (!empty($extra)) { |
90
|
3 |
|
$extraErrors = (array_key_exists('errors', $extra)) ? $extra['errors'] : $extra; |
91
|
3 |
|
$error['error']['errors'] = $extraErrors; |
92
|
2 |
|
} |
93
|
|
|
|
94
|
6 |
|
$response = $this->basicStructure() + $error; |
95
|
6 |
|
return $this->output($response); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Generate success response |
100
|
|
|
* |
101
|
|
|
* @param array $data data |
102
|
|
|
* |
103
|
|
|
* @return array|object|string |
104
|
|
|
*/ |
105
|
9 |
|
public function success($data) |
106
|
|
|
{ |
107
|
9 |
|
$response = $this->basicStructure() + $data; |
108
|
9 |
|
return $this->output($response); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Generate basic structure |
113
|
|
|
* |
114
|
|
|
* This structure is used in all the responses |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
15 |
|
private function basicStructure() |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
15 |
|
'apiVersion' => $this->apiVersion, |
122
|
15 |
|
'context' => $this->context |
123
|
10 |
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Generate output |
128
|
|
|
* |
129
|
|
|
* @param array|object $data output data |
130
|
|
|
* |
131
|
|
|
* @return array|object|string|false |
132
|
|
|
*/ |
133
|
15 |
|
private function output($data) |
134
|
|
|
{ |
135
|
15 |
|
switch ($this->format) { |
136
|
15 |
|
case 'array': |
137
|
|
|
return (array)$data; |
138
|
|
|
break; |
|
|
|
|
139
|
15 |
|
case 'object': |
140
|
|
|
return (object)$data; |
141
|
|
|
break; |
|
|
|
|
142
|
15 |
|
case 'json': |
143
|
15 |
|
return json_encode($data); |
144
|
|
|
break; |
|
|
|
|
145
|
|
|
default: |
146
|
|
|
return json_encode($data); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.