Passed
Push — master ( aae1e6...eb8454 )
by
unknown
07:07 queued 12s
created

Converter::convertYamlToArray()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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