Passed
Push — master ( bbb624...9986b4 )
by mcfog
03:45
created

JsonView   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 41
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setJsonOption() 0 5 1
A render() 0 7 2
A renderJson() 0 3 1
A getJsonOption() 0 3 1
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
    public function render(array $data = []): ResponseInterface
19
    {
20
        $jsonData = array_key_exists(static::JSON_ROOT, $data) ? $data[static::JSON_ROOT] : $data;
21
        $this->getEmptyBody()->write(json_encode($jsonData, $this->jsonOption));
22
23
        return $this->response
24
            ->withHeader('Content-Type', 'application/json');
25
    }
26
27
    public function renderJson($value): ResponseInterface
28
    {
29
        return $this->render([static::JSON_ROOT => $value]);
30
    }
31
32
    /**
33
     * @return int
34
     */
35
    public function getJsonOption()
36
    {
37
        return $this->jsonOption;
38
    }
39
40
    /**
41
     *
42
     * @param int $jsonOption
43
     * @return $this
44
     */
45
    public function setJsonOption($jsonOption)
46
    {
47
        $this->jsonOption = $jsonOption;
48
49
        return $this;
50
    }
51
}
52