-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.php
More file actions
107 lines (94 loc) · 3.25 KB
/
Copy pathResponse.php
File metadata and controls
107 lines (94 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* Response class file.
*
* @package Mantle
*/
namespace Mantle\Http;
use ArrayObject;
use InvalidArgumentException;
use JsonSerializable;
use Mantle\Contracts\Support\Arrayable;
use Mantle\Contracts\Support\Htmlable;
use Mantle\Contracts\Support\Jsonable;
use Mantle\Support\Traits\Macroable;
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
/**
* HTTP Response
*/
class Response extends HttpFoundationResponse {
use Macroable;
/**
* The original content of the response.
*
* @var mixed
*/
public $original;
/**
* Constructor.
*
* @param mixed $content Response content.
* @param int $status Response status code.
* @param array<string, string> $headers Response headers.
*
* @throws InvalidArgumentException When the HTTP status code is not valid.
*/
public function __construct( $content = '', int $status = 200, array $headers = [] ) {
$this->headers = new ResponseHeaderBag( $headers );
$this->setContent( $content );
$this->setStatusCode( $status );
$this->setProtocolVersion( '1.0' );
}
/**
* Set the content on the response.
*
* @param mixed $content
*
* @throws InvalidArgumentException When the HTTP status code is not valid.
*/
#[\Override]
public function setContent( mixed $content ): static {
$this->original = $content;
// If the content is "JSONable" we will set the appropriate header and convert
// the content to JSON. This is useful when returning something like models
// from routes that will be automatically transformed to their JSON form.
if ( $this->should_be_json( $content ) ) {
$this->headers->set( 'Content-Type', 'application/json' );
$content = $this->morph_to_json( $content );
if ( false === $content ) {
throw new InvalidArgumentException( json_last_error_msg() );
}
} elseif ( $content instanceof Htmlable ) {
$content = $content->to_html();
}
parent::setContent( $content );
return $this;
}
/**
* Determine if the given content should be turned into JSON.
*
* @param Arrayable|Jsonable|ArrayObject|JsonSerializable|array|mixed $content
*/
protected function should_be_json( mixed $content ): bool {
return $content instanceof Arrayable ||
$content instanceof Jsonable ||
$content instanceof ArrayObject ||
$content instanceof JsonSerializable ||
is_array( $content );
}
/**
* Morph the given content into JSON.
*
* @param Arrayable|Jsonable|ArrayObject|JsonSerializable|array|mixed $content
*/
protected function morph_to_json( $content ): string|false {
return match ( true ) {
$content instanceof JsonSerializable => json_encode( $content->jsonSerialize() ), // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
$content instanceof ArrayObject => json_encode( $content->getArrayCopy() ), // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
$content instanceof Jsonable => $content->to_json(),
$content instanceof Arrayable => json_encode( $content->to_array() ), // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
default => json_encode( $content ), // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
};
}
}