Passed
Push — analysis-gORMyw ( f3151d )
by Arnaud
05:19 queued 13s
created

Converter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Converter;
12
13
use Cecil\Builder;
14
use Cecil\Exception\Exception;
15
use Symfony\Component\Yaml\Exception\ParseException;
16
use Symfony\Component\Yaml\Yaml;
17
18
/**
19
 * Class Converter.
20
 */
21
class Converter implements ConverterInterface
22
{
23
    /** @var Builder */
24
    protected $builder;
25
26
    /**
27
     * @param Builder $builder
28
     */
29
    public function __construct(Builder $builder)
30
    {
31
        $this->builder = $builder;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function convertFrontmatter(string $string, string $type = 'yaml'): array
38
    {
39
        switch ($type) {
40
            case 'ini':
41
                $result = parse_ini_string($string);
42
                if (!$result) {
43
                    throw new Exception('Can\'t parse INI front matter');
44
                }
45
46
                return $result;
47
            case 'yaml':
48
            default:
49
                try {
50
                    $result = Yaml::parse((string) $string);
51
                    if (!is_array($result)) {
52
                        throw new Exception('Parse result of YAML front matter is not an array');
53
                    }
54
55
                    return $result;
56
                } catch (ParseException $e) {
57
                    throw new Exception($e->getMessage());
58
                }
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function convertBody(string $string): string
66
    {
67
        $parsedown = new Parsedown($this->builder);
68
69
        return $parsedown->text($string);
70
    }
71
}
72