Passed
Push — converter ( a025de...a3efd0 )
by Arnaud
03:09
created

Converter::convertJsonToArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 9
ccs 0
cts 0
cp 0
crap 20
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Converter;
15
16
use Cecil\Builder;
17
use Cecil\Exception\RuntimeException;
18
use Symfony\Component\Yaml\Exception\ParseException;
19
use Symfony\Component\Yaml\Yaml;
20
use Yosymfony\Toml\Exception\ParseException as TomlParseException;
21
use Yosymfony\Toml\Toml;
22
23
/**
24
 * Class Converter.
25
 */
26
class Converter implements ConverterInterface
27
{
28
    /** @var Builder */
29
    protected $builder;
30
31 1
    public function __construct(Builder $builder)
32
    {
33 1
        $this->builder = $builder;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @throws RuntimeException
40
     */
41 1
    public function convertFrontmatter(string $string, string $format = 'yaml'): array
42
    {
43 1
        if (!in_array($format, ['yaml', 'ini', 'toml', 'json'])) {
44
            throw new RuntimeException(\sprintf('The front matter format "%s" is not supported ("yaml", "ini", "toml" or "json").', $format));
45 1
        }
46
        $method = \sprintf('convert%sToArray', ucfirst($format));
47
48
        return self::$method($string);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53 1
     */
54
    public function convertBody(string $string): string
55
    {
56
        $parsedown = new Parsedown($this->builder);
57
58
        return $parsedown->text($string);
59
    }
60
61
    /**
62
     * Converts YAML string to array.
63
     *
64
     * @see https://wikipedia.org/wiki/YAML
65 1
     */
66
    private static function convertYamlToArray(string $string): array
67
    {
68
        try {
69
            $result = Yaml::parse((string) $string) ?? [];
70
            if (!is_array($result)) {
71
                throw new RuntimeException('Can\'t parse YAML front matter.');
72
            }
73
74
            return $result;
75
        } catch (ParseException $e) {
76
            throw new RuntimeException($e->getMessage(), $e->getParsedFile(), $e->getParsedLine());
77
        } catch (\Exception $e) {
78
            throw new RuntimeException($e->getMessage());
79 1
        }
80
    }
81
82 1
    /**
83 1
     * Converts INI string to array.
84
     *
85
     * @see https://wikipedia.org/wiki/INI_file
86
     */
87 1
    private static function convertIniToArray(string $string): array
88 1
    {
89 1
        $result = parse_ini_string($string, true);
90
        if ($result === false) {
91
            throw new RuntimeException('Can\'t parse INI front matter.');
92
        }
93
94
        return $result;
95
    }
96
97
    /**
98
     * Converts TOML string to array.
99 1
     *
100
     * @see https://wikipedia.org/wiki/TOML
101 1
     */
102
    private static function convertTomlToArray(string $string): array
103 1
    {
104
        try {
105
            $result = Toml::Parse((string) $string) ?? [];
106
            if (!is_array($result)) {
107
                throw new RuntimeException('Can\'t parse TOML front matter.');
108
            }
109
110
            return $result;
111
        } catch (TomlParseException $e) {
112
            throw new RuntimeException($e->getMessage(), $e->getParsedFile(), $e->getParsedLine());
113
        } catch (\Exception $e) {
114
            throw new RuntimeException($e->getMessage());
115
        }
116
    }
117
118
    /**
119
     * Converts JSON string to array.
120
     *
121
     * @see https://wikipedia.org/wiki/JSON
122
     */
123
    private static function convertJsonToArray(string $string): array
124
    {
125
        try {
126
            $result = json_decode($string, true);
127
            if ($result === null && json_last_error() !== JSON_ERROR_NONE) {
128
                throw new \Exception('JSON error.');
129
            }
130
        } catch (\Exception $e) {
131
            throw new RuntimeException('Can\'t parse JSON front matter.');
132
        }
133
    }
134
}
135