Issues (493)

lib/SP/Http/JsonResponse.php (1 issue)

1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Http;
26
27
use JsonSerializable;
28
use stdClass;
29
30
/**
31
 * Class Json para definir la estructura de una respuesta en formato JSON
32
 *
33
 * @package SP\Http
34
 */
35
final class JsonResponse implements JsonSerializable
36
{
37
    const JSON_SUCCESS = 0;
38
    const JSON_SUCCESS_STICKY = 100;
39
    const JSON_ERROR = 1;
40
    const JSON_ERROR_STICKY = 101;
41
    const JSON_WARNING = 2;
42
    const JSON_WARNING_STICKY = 102;
43
    const JSON_LOGOUT = 10;
44
45
    /**
46
     * @var int
47
     */
48
    protected $status = 1;
49
    /**
50
     * @var string
51
     */
52
    protected $description = '';
53
    /**
54
     * @var string
55
     */
56
    protected $action = '';
57
    /**
58
     * @var array
59
     */
60
    protected $data = [];
61
    /**
62
     * @var array
63
     */
64
    protected $messages = [];
65
    /**
66
     * @var string
67
     */
68
    protected $container = '';
69
    /**
70
     * @var string
71
     */
72
    protected $csrf = '';
73
74
    /**
75
     * JsonResponse constructor.
76
     *
77
     * @param string $description
78
     */
79
    public function __construct(string $description = null)
80
    {
81
        $this->description = $description;
82
    }
83
84
    /**
85
     * @return int
86
     */
87
    public function getStatus()
88
    {
89
        return $this->status;
90
    }
91
92
    /**
93
     * @param int $status
94
     *
95
     * @return JsonResponse
96
     */
97
    public function setStatus($status)
98
    {
99
        $this->status = (int)$status;
100
101
        return $this;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getDescription()
108
    {
109
        return $this->description;
110
    }
111
112
    /**
113
     * @param string $description
114
     *
115
     * @return JsonResponse
116
     */
117
    public function setDescription($description)
118
    {
119
        $this->description = __($description);
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function getAction()
128
    {
129
        return $this->action;
130
    }
131
132
    /**
133
     * @param string $action
134
     *
135
     * @return JsonResponse
136
     */
137
    public function setAction($action)
138
    {
139
        $this->action = $action;
140
141
        return $this;
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function getData()
148
    {
149
        return $this->data;
150
    }
151
152
    /**
153
     * @param array|stdClass $data
154
     *
155
     * @return JsonResponse
156
     */
157
    public function setData($data)
158
    {
159
        $this->data = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data can also be of type stdClass. However, the property $data is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    public function getMessages()
168
    {
169
        return $this->messages;
170
    }
171
172
    /**
173
     * @param array $messages
174
     *
175
     * @return JsonResponse
176
     */
177
    public function setMessages(array $messages)
178
    {
179
        $this->messages = array_map('__', $messages);
180
181
        return $this;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getContainer()
188
    {
189
        return $this->container;
190
    }
191
192
    /**
193
     * @param string $container
194
     *
195
     * @return JsonResponse
196
     */
197
    public function setContainer($container)
198
    {
199
        $this->container = $container;
200
201
        return $this;
202
    }
203
204
    /**
205
     * @return string
206
     */
207
    public function getCsrf()
208
    {
209
        return $this->csrf;
210
    }
211
212
    /**
213
     * @param string $csrf
214
     *
215
     * @return JsonResponse
216
     */
217
    public function setCsrf($csrf)
218
    {
219
        $this->csrf = $csrf;
220
221
        return $this;
222
    }
223
224
    /**
225
     * @param $message
226
     *
227
     * @return JsonResponse
228
     */
229
    public function addMessage($message)
230
    {
231
        $this->messages[] = __($message);
232
        return $this;
233
    }
234
235
    /**
236
     * @param $param
237
     *
238
     * @return $this
239
     */
240
    public function addParam($param)
241
    {
242
        if (is_numeric($param)) {
243
            $param = (int)$param;
244
        }
245
246
        $this->data[] = $param;
247
248
        return $this;
249
    }
250
251
    /**
252
     * Specify data which should be serialized to JSON
253
     *
254
     * @link  http://php.net/manual/en/jsonserializable.jsonserialize.php
255
     * @return mixed data which can be serialized by <b>json_encode</b>,
256
     * which is a value of any type other than a resource.
257
     * @since 5.4.0
258
     */
259
    public function jsonSerialize()
260
    {
261
        return $this->getJsonArray();
262
    }
263
264
    /**
265
     * Devolver un array con las propiedades del objeto
266
     *
267
     * @return array
268
     */
269
    public function getJsonArray()
270
    {
271
        $out = [];
272
273
        foreach ($this as $key => $value) {
274
            $out[$key] = $value;
275
        }
276
277
        return $out;
278
    }
279
280
    /**
281
     * Establecer los valores por defecto
282
     *
283
     * @return JsonResponse
284
     */
285
    public function clear()
286
    {
287
        $this->status = 0;
288
        $this->action = '';
289
        $this->data = [];
290
        $this->messages = [];
291
        $this->container = '';
292
        $this->csrf = '';
293
294
        return $this;
295
    }
296
}