-
Notifications
You must be signed in to change notification settings - Fork 34
/
JsonData.php
52 lines (42 loc) · 1022 Bytes
/
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
<?php declare(strict_types=1);
namespace hollodotme\FastCGI\RequestContents;
use hollodotme\FastCGI\Interfaces\ComposesRequestContent;
use RuntimeException;
use function json_encode;
final class JsonData implements ComposesRequestContent
{
/** @var mixed */
private $data;
/** @var int */
private $encodingOptions;
/** @var int */
private $encodingDepth;
/**
* @param mixed $data
* @param int $options
* @param int $depth
*/
public function __construct( $data, int $options = 0, int $depth = 512 )
{
$this->data = $data;
$this->encodingOptions = $options;
$this->encodingDepth = $depth;
}
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;
}
}