1
|
|
|
<?php |
2
|
|
|
namespace Germania\Responder; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\ResponseInterface; |
5
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
6
|
|
|
use Slim\Psr7\Factory\ResponseFactory; |
7
|
|
|
|
8
|
|
|
class JsonResponder implements ResponderInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
1 |
|
use ResponseFactoryTrait; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
public $json_options = 0; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Response content type |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $response_content_type = 'application/json'; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param int $json_options |
30
|
|
|
* @param ResponseFactoryInterface $response_factory |
31
|
|
|
*/ |
32
|
14 |
|
public function __construct(int $json_options = 0, ResponseFactoryInterface $response_factory = null ) |
33
|
|
|
{ |
34
|
14 |
|
$this->setResponseFactory($response_factory ?: new ResponseFactory); |
35
|
14 |
|
$this->setJsonOptions($json_options); |
36
|
14 |
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sets the JSON options. |
41
|
|
|
* |
42
|
|
|
* @param int $json_options |
43
|
|
|
*/ |
44
|
14 |
|
public function setJsonOptions( int $json_options ) |
45
|
|
|
{ |
46
|
14 |
|
$this->json_options = $json_options; |
47
|
14 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets the response content type. |
53
|
|
|
* |
54
|
|
|
* @param string $content_type |
55
|
|
|
*/ |
56
|
|
|
public function setResponseContentType( string $content_type ) |
57
|
|
|
{ |
58
|
|
|
$this->response_content_type = $content_type; |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function __invoke( $thingy, int $status = 500 ) : ResponseInterface |
69
|
|
|
{ |
70
|
|
|
return $this->createResponse( $thingy, $status); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
20 |
|
public function createResponse( $thingy, int $status = 200) : ResponseInterface |
78
|
|
|
{ |
79
|
20 |
|
if (is_resource($thingy)) { |
80
|
2 |
|
$msg = sprintf("Can't work with resource types."); |
81
|
2 |
|
throw new ResponderInvalidArgumentException($msg); |
82
|
|
|
} |
83
|
|
|
|
84
|
18 |
|
if (is_object($thingy) |
85
|
18 |
|
and !$thingy instanceOf \JsonSerializable) { |
|
|
|
|
86
|
2 |
|
$msg = sprintf("Expected JsonSerializable instance, instead got '%s'.", get_class($thingy)); |
87
|
2 |
|
throw new ResponderInvalidArgumentException($msg); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
try { |
91
|
16 |
|
$json_thingy = json_encode($thingy, $this->json_options); |
92
|
|
|
|
93
|
16 |
|
$response = $this->getResponseFactory()->createResponse() |
94
|
16 |
|
->withHeader('Content-type', $this->response_content_type) |
95
|
16 |
|
->withStatus($status); |
96
|
|
|
|
97
|
16 |
|
$response->getBody()->write($json_thingy); |
98
|
|
|
} |
99
|
|
|
catch (\Throwable $e) { |
100
|
|
|
throw new ResponderRuntimeException("Caught exception during response creation", 1, $e); |
101
|
|
|
} |
102
|
|
|
|
103
|
16 |
|
return $response; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.