JsonFixture::only()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Muzzle\Messages;
4
5
use ArrayAccess;
6
use GuzzleHttp\Psr7\Response;
7
use Illuminate\Support\Arr;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\StreamInterface;
10
use function GuzzleHttp\Psr7\stream_for;
11
12
class JsonFixture implements ResponseInterface, ArrayAccess
13
{
14
15
    use ResponseDecorator {
16
        __construct as initialize;
17
    }
18
19
    /**
20
     * @var array
21
     */
22
    private $body = [];
23
24
    /**
25
     * @param int $status                                Status code
26
     * @param array $headers                             Response headers
27
     * @param string|null|resource|StreamInterface $body Response body
28
     * @param string $version                            Protocol version
29
     * @param string|null $reason                        Reason phrase
30
     */
31
    public function __construct(
32
        $status = 200,
33
        array $headers = [],
34
        $body = null,
35
        $version = '1.1',
36
        $reason = null
37
    ) {
38
39
        $this->initialize(new Response($status, $headers, $body, $version, $reason));
40
        $this->withBody($this->response->getBody());
41
    }
42
43
    public function getBody()
44
    {
45
46
        return stream_for(json_encode($this->body));
47
    }
48
49
    public function withBody(StreamInterface $body)
50
    {
51
52
        $this->body = json_decode($body, true);
53
        return $this;
54
    }
55
56
    public function asArray() : array
57
    {
58
59
        return $this->body;
60
    }
61
62
    public function has(string $key) : bool
63
    {
64
65
        return Arr::has($this->body, $key);
66
    }
67
68
    /**
69
     * @param string $key
70
     * @param mixed|callable $default
71
     * @return mixed
72
     */
73
    public function get(string $key, $default = null)
74
    {
75
76
        return Arr::get($this->body, $key, $default);
77
    }
78
79
    public function set(string $key, $value) : JsonFixture
80
    {
81
82
        Arr::set($this->body, $key, $value);
83
84
        return $this;
85
    }
86
87
    public function forget(string $key) : void
88
    {
89
90
        Arr::forget($this->body, $key);
91
    }
92
93
    public function only(array $keys) : array
94
    {
95
96
        return Arr::only($this->body, $keys);
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function offsetExists($offset) : bool
103
    {
104
105
        return $this->has($offset);
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function offsetGet($offset)
112
    {
113
114
        return $this->get($offset);
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    public function offsetSet($offset, $value) : void
121
    {
122
123
        $this->set($offset, $value);
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129
    public function offsetUnset($offset)
130
    {
131
132
        $this->forget($offset);
133
    }
134
}
135