Completed
Pull Request — develop (#120)
by Matthew
02:45
created

Yaml::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Noodlehaus\Parser;
4
5
use Exception;
6
use Symfony\Component\Yaml\Yaml as YamlParser;
7
use Noodlehaus\Exception\ParseException;
8
9
/**
10
 * YAML parser
11
 *
12
 * @package    Config
13
 * @author     Jesus A. Domingo <[email protected]>
14
 * @author     Hassan Khan <[email protected]>
15
 * @author     Filip Š <[email protected]>
16
 * @link       https://github.com/noodlehaus/config
17
 * @license    MIT
18
 */
19
class Yaml implements ParserInterface
20
{
21
    /**
22
     * {@inheritDoc}
23
     * Loads a YAML/YML file as an array
24
     *
25
     * @throws ParseException If there is an error parsing the YAML file
26
     */
27 6
    public function parseFile($filename)
28
    {
29
        try {
30 6
            $data = YamlParser::parseFile($filename, YamlParser::PARSE_CONSTANT);
31 5
        } catch (Exception $exception) {
32 3
            throw new ParseException(
33
                [
34 3
                    'message'   => 'Error parsing YAML file',
35 3
                    'exception' => $exception,
36
                ]
37 2
            );
38
        }
39
40 3
        $parsed = $this->parse($data);
41
42 3
        return $parsed === null ? [] : $parsed;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     * Loads a YAML/YML string as an array
48
     *
49
     * @throws ParseException If If there is an error parsing the YAML string
50
     */
51 6
    public function parseString($config)
52
    {
53
        try {
54 6
            $data = YamlParser::parse($config, YamlParser::PARSE_CONSTANT);
55 5
        } catch (Exception $exception) {
56 3
            throw new ParseException(
57
                [
58 3
                    'message'   => 'Error parsing YAML string',
59 3
                    'exception' => $exception,
60
                ]
61 2
            );
62
        }
63
64 3
        $parsed = $this->parse($data);
65
66 3
        return $parsed === null ? [] : $parsed;
67
    }
68
69
    /**
70
     * Completes parsing of YAML/YML data
71
     *
72
     * @param  array $data
73
     *
74
     * @return array|null
75
     */
76 6
    protected function parse($data = null)
77
    {
78 6
        return $data;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84 3
    public static function getSupportedExtensions()
85
    {
86 3
        return ['yaml', 'yml'];
87
    }
88
}
89