-
Notifications
You must be signed in to change notification settings - Fork 34
/
JsonData.php
53 lines (43 loc) · 1.07 KB
/
JsonData.php
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
<?php declare(strict_types=1);
namespace hollodotme\FastCGI\RequestContents;
use hollodotme\FastCGI\Interfaces\ComposesRequestContent;
use RuntimeException;
use function json_encode;
use const PHP_INT_MAX;
final class JsonData implements ComposesRequestContent
{
/** @var mixed */
private $data;
/** @var int */
private $encodingOptions;
/** @var int<1, max> */
private $encodingDepth;
/**
* @param mixed $data
* @param int $options
* @param int<1, max> $depth
*/
public function __construct( $data, int $options = 0, int $depth = 512 )
{
$this->data = $data;
$this->encodingOptions = $options;
$this->encodingDepth = max( 1, min( $depth, PHP_INT_MAX ) );
}
public function getContentType() : string
{
return 'application/json';
}
/**
* @return string
* @throws RuntimeException
*/
public function getContent() : string
{
$json = json_encode( $this->data, $this->encodingOptions, $this->encodingDepth );
if ( false === $json )
{
throw new RuntimeException( 'Could not encode data to JSON.' );
}
return $json;
}
}