Completed
Push — 1.x ( 6b83a8...5da9b9 )
by Akihito
21s queued 13s
created

PrettyJsonRenderer::render()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 9
nc 4
nop 1
crap 3
1
<?php
2
/**
3
 * This file is part of the BEAR.Resource package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Resource;
8
9
final class PrettyJsonRenderer implements RenderInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 3
    public function render(ResourceObject $ro)
15
    {
16 3
        if (! array_key_exists('content-type', $ro->headers)) {
17 2
            $ro->headers['content-type'] = 'application/json';
18
        }
19 3
        $ro->view = json_encode($ro, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL;
20 3
        $e = json_last_error();
21 3
        if ($e) {
22
            // @codeCoverageIgnoreStart
23
            error_log('json_encode error: ' . json_last_error_msg() . ' in ' . __METHOD__);
24
25
            return '';
26
            // @codeCoverageIgnoreEnd
27
        }
28
29 3
        return $ro->view;
30
    }
31
}
32