Completed
Push — master ( 33953d...4a13d7 )
by Xeriab
03:37
created

Toml::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Konfig.
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * PHP version 5
9
 *
10
 * @category Library
11
 * @package  Konfig
12
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
13
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
14
 * @link     https://xeriab.github.io/projects/konfig
15
 */
16
17
namespace Exen\Konfig\FileParser;
18
19
use Exception;
20
use Exen\Konfig\Exception\ParseException;
21
use Exen\Konfig\Utils;
22
use Yosymfony\Toml\Toml as TomlLib;
23
24
/**
25
 * Toml
26
 * Konfig's TOML parser class.
27
 *
28
 * @category FileParser
29
 * @package  Konfig
30
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
31
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
32
 * @link     https://xeriab.github.io/projects/konfig
33
 *
34
 * @implements Exen\Konfig\FileParser\AbstractFileParser
35
 */
36
class Toml extends AbstractFileParser
37
{
38
    /**
39
     * Loads a TOML file as an array.
40
     *
41
     * @param string $path File path
42
     *
43
     * @throws ParseException If there is an error parsing TOML file
44
     *
45
     * @return array The parsed data
46
     *
47
     * @since 0.1.0
48
     */
49 6
    public function parse($path)
50
    {
51 6
        $data = null;
52
53
        try {
54 6
            $data = $this->loadFile($path);
55 5
        } catch (Exception $ex) {
56 3
            throw new ParseException(
57
                [
58 3
                'message' => 'Error parsing TOML file',
59 3
                'file' => $path,
60 3
                'exception' => $ex,
61
                ]
62 2
            );
63
        }
64
65 3
        return $data;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @return array Supported extensions
72
     *
73
     * @since 0.1.0
74
     */
75 3
    public function getSupportedFileExtensions()
76
    {
77 3
        return ['toml'];
78
    }
79
80
    /**
81
     * Loads in the given file and parses it.
82
     *
83
     * @param string $file File to load
84
     *
85
     * @return array The parsed file data
86
     *
87
     * @since              0.2.4
88
     * @codeCoverageIgnore
89
     */
90
    protected function loadFile($file = null)
91
    {
92
        $this->file = $file;
93
        $contents = $this->parseVars(Utils::getContent($file));
94
95
        return TomlLib::Parse($contents);
96
    }
97
98
    /**
99
     * Returns the formatted configuration file contents.
100
     *
101
     * @param array $contents configuration array
102
     *
103
     * @return string formatted configuration file contents
104
     *
105
     * @since              0.2.4
106
     * @codeCoverageIgnore
107
     */
108
    protected function exportFormat($contents = null)
109
    {
110
        throw new Exception(
111
            'Saving configuration to `TOML` is not supported at this time'
112
        );
113
    }
114
115
    /**
116
     * __toString.
117
     *
118
     * @return             string
119
     * @since              0.1.2
120
     * @codeCoverageIgnore
121
     */
122
    public function __toString()
123
    {
124
        return 'Exen\Konfig\FileParser\Toml' . PHP_EOL;
125
    }
126
}
127
128
// END OF ./src/FileParser/Toml.php FILE
129