Passed
Push — master ( 626b7a...e5fcff )
by mcfog
02:39
created

JsonView::renderJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Voltage;
6
7
use Psr\Http\Message\ResponseInterface;
8
9
class JsonView extends AbstractView
10
{
11
    const JSON_ROOT = self::class;
12
    protected $jsonOption = 0;
13
14
    /**
15
     * @param array $data
16
     * @return ResponseInterface
17
     */
18 3
    public function render(array $data = []): ResponseInterface
19
    {
20 3
        $jsonData = array_key_exists(static::JSON_ROOT, $data) ? $data[static::JSON_ROOT] : $data;
21 3
        $this->getEmptyBody()->write(json_encode($jsonData, $this->jsonOption));
22
23 3
        return $this->response
24 3
            ->withHeader('Content-Type', 'application/json');
25
    }
26
27 1
    public function renderJson($value): ResponseInterface
28
    {
29 1
        return $this->render([static::JSON_ROOT => $value]);
30
    }
31
32
    /**
33
     * @return int
34
     */
35 1
    public function getJsonOption()
36
    {
37 1
        return $this->jsonOption;
38
    }
39
40
    /**
41
     *
42
     * @param int $jsonOption
43
     * @return $this
44
     */
45 1
    public function setJsonOption($jsonOption)
46
    {
47 1
        $this->jsonOption = $jsonOption;
48
49 1
        return $this;
50
    }
51
}
52