Toml::exportFormat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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