/
JsonMutator.php
92 lines (77 loc) · 2.4 KB
/
JsonMutator.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
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
<?php
namespace Rougin\Weasley\Mutators;
use Psr\Http\Message\ResponseInterface;
use Rougin\Weasley\Contract\Mutator;
/**
* JSON Transformer
*
* @package Weasley
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class JsonMutator implements Mutator
{
/**
* @var array<integer, string>
*/
protected $errors = array();
/**
* @var integer
*/
protected $options = 0;
/**
* @var \Psr\Http\Message\ResponseInterface
*/
protected $response;
/**
* @param \Psr\Http\Message\ResponseInterface $response
* @param integer $options
*/
public function __construct(ResponseInterface $response, $options = 0)
{
$this->errors[] = 'No errors';
$this->errors[] = 'Maximum stack depth exceeded';
$this->errors[] = 'Underflow or the modes mismatch';
$this->errors[] = 'Unexpected control character found';
$this->errors[] = 'Syntax error, malformed JSON';
$this->errors[] = 'Malformed UTF-8 characters, possibly incorrectly encoded';
$this->errors[] = 'One or more recursive references in the value to be encoded';
$this->errors[] = 'One or more NAN or INF values in the value to be encoded';
$this->errors[] = 'A value of a type that cannot be encoded was given';
$this->errors[] = 'A property name that cannot be encoded was given';
$this->errors[] = 'Malformed UTF-16 characters, possibly incorrectly encoded';
$this->options = $options;
$this->response = $response;
}
/**
* @deprecated since ~0.7, use "mutate" instead.
* @codeCoverageIgnore
*
* Transforms the contents of the result.
*
* @param mixed $data
* @return mixed
*/
public function transform($data)
{
return $this->mutate($data);
}
/**
* Mutates the contents of the result.
*
* @param mixed $data
* @return mixed
*/
public function mutate($data)
{
$response = $this->response;
/** @var string */
$stream = @json_encode($data, $this->options);
if (json_last_error() !== JSON_ERROR_NONE)
{
$stream = $this->errors[json_last_error()];
$response = $response->withStatus(400);
}
$response->getBody()->write($stream);
return $response->withHeader('Content-Type', 'application/json');
}
}