Converter::convertIniToArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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