Completed
Push — master ( 76163c...0c83ff )
by Nashwan
03:43
created

Yaml::parse()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 12
nc 5
nop 1
1
<?php
2
/**
3
 * Konfig
4
 *
5
 * Yet another simple configuration file loader library.
6
 *
7
 * @author  Xeriab Nabil (aka KodeBurner) <[email protected]>
8
 * @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT
9
 * @link    https://xeriab.github.io/projects/konfig
10
 */
11
namespace Exen\Konfig\FileParser;
12
13
use Exception;
14
use Exen\Konfig\Exception\ParseException;
15
16
class Yaml extends AbstractFileParser
17
{
18
    /**
19
     * {@inheritDoc}
20
     * Loads a YAML/YML file as an array
21
     *
22
     * @throws ParseException If there is an error parsing YAML/YML file
23
     * @since 0.1
24
     */
25
    public function parse($path)
26
    {
27
        try {
28
            $data = null;
29
            if (! extension_loaded('yaml')) {
30
                $data = spyc_load_file($path);
31
            } else {
32
                $data = yaml_parse_file($path);
33
            }
34
        } catch (Exception $ex) {
35
            throw new ParseException(
36
                [
37
                    'message'   => 'Error parsing YAML file',
38
                    'exception' => $ex,
39
                ]
40
            );
41
        }
42
43
        return $data;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function getSupportedFileExtensions()
50
    {
51
        return ['yaml', 'yml'];
52
    }
53
}
54
55
#: END OF ./src/FileParser/Yaml.php FILE
56