JsonWithoutBracesProcessor::dump()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
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 $string): array
18
    {
19
        if (false !== strpos($string, '":')) {
20
            $string = '{'.$string.'}';
21
        }
22
23
        return (array) json_decode($string, true);
24
    }
25
26
    public function dump(array $data): string
27
    {
28
        if (empty($data)) {
29
            return '';
30
        }
31
32
        $result = (string) json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
33
34
        if ('{' === substr($result, 0, 1) && '}' === substr($result, -1)) {
35
            $result = substr($result, 1, -1);
36
        }
37
38
        return $result;
39
    }
40
}
41