Completed
Push — master ( 5636df...2c655c )
by Martin
04:07
created

JsonWithoutBracesProcessor::dump()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 7
nc 3
nop 1
1
<?php
2
3
/*
4
 * This is part of the webuni/front-matter package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\FrontMatter\Processor;
14
15
final class JsonWithoutBracesProcessor implements ProcessorInterface
16
{
17
    public function parse($string)
18
    {
19
        if (false !== strpos($string, '":')) {
20
            $string = '{'.$string.'}';
21
        }
22
23
        return json_decode($string, true);
24
    }
25
26
    public function dump($data)
27
    {
28
        if (is_array($data) && empty($data)) {
29
            return '';
30
        }
31
32
        $result = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
33
34
        if ('{' === $result[0] && '}' === substr($result, -1)) {
35
            $result = substr($result, 1, -1);
36
        }
37
38
        return $result;
39
    }
40
}
41