Passed
Push — master ( 42571c...f8295f )
by stéphane
09:20
created

Yaml   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 98
ccs 16
cts 16
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 6 2
A parseFile() 0 6 2
A dumpFile() 0 6 2
A dump() 0 6 2
1
<?php
2
3
namespace Dallgoot;
4
5
/**
6
 * Library that :
7
 * - reads YAML as PHP types
8
 * - writes PHP types as YAML content
9
 *
10
 * @author  Stéphane Rebai <[email protected]>
11
 * @license Apache 2.0
12
 * @link    https://github.com/dallgoot/yaml
13
 *
14
 * @see YamlObject
15
 * @see Compact
16
 * @see Tag
17
 */
18
final class Yaml
19
{
20
    const VERSION_SUPPORT = "1.2";
21
    /**
22
     * Parse the given Yaml string to either :
23
     * - a YamlObject
24
     * - an array of YamlObject
25
     *
26
     * @param string   $someYaml Some yaml
27
     * @param int|null $options  from Loader class, bitwise combination of
28
     *                           Loader::IGNORE_DIRECTIVES
29
     *                           Loader::IGNORE_COMMENTS
30
     *                           Loader::NO_PARSING_EXCEPTIONS
31
     *                           Loader::NO_OBJECT_FOR_DATE
32
     * @param int|null $debug    define the level of debugging (true = default)
33
     *
34
     * @return Yaml\YamlObject|array|null a Yaml document as YamlObject OR multiple documents as an array of YamlObject,
35
     *                               NULL if Error and option Loader::NO_PARSING_EXCEPTIONS is set.
36
     * @throws \Exception coming from Dallgoot\Yaml\Loader
37
     * @see    Dallgoot\Yaml\Loader
38
     *
39
     * @todo transpose Loader::NO_PARSING_EXCEPTIONS in this class
40
     */
41 2
    public static function parse(string $someYaml, $options = null, $debug = null)
42
    {
43
        try {
44 2
            return (new Yaml\Loader(null, $options, $debug))->parse($someYaml);
45 1
        } catch (\Throwable $e) {
46 1
            throw new \Exception(__CLASS__." Error while parsing YAML string", 1, $e);
47
        }
48
    }
49
50
    /**
51
     * Load the given file and parse its content (assumed YAML) to either :
52
     * - a YamlObject
53
     * - an array of YamlObject
54
     *
55
     * @param string   $fileName Some file path name
56
     * @param int|null $options  from Loader class, bitwise combination of
57
     *                           Loader::IGNORE_DIRECTIVES
58
     *                           Loader::IGNORE_COMMENTS
59
     *                           Loader::NO_PARSING_EXCEPTIONS
60
     *                           Loader::NO_OBJECT_FOR_DATE
61
     * @param int|null $debug    define the level of debugging (true = default)
62
     *
63
     * @return Yaml\YamlObject|array|null a Yaml document as YamlObject OR multiple documents as an array of YamlObject,
64
     *                               NULL if Error
65
     * @throws \Exception coming from Dallgoot\Yaml\Loader
66
     * @see    Dallgoot\Yaml\Loader
67
     */
68 2
    public static function parseFile(string $fileName, $options = null, $debug = null)
69
    {
70
        try {
71 2
            return (new Yaml\Loader($fileName, $options, (int) $debug))->parse();
72 1
        } catch (\Throwable $e) {
73 1
            throw new \Exception(__CLASS__." Error during parsing '$fileName'", 1, $e);
74
        }
75
76
    }
77
78
    /**
79
     * Returns the YAML representation corresponding to given PHP variable
80
     *
81
     * @param mixed    $somePhpVar Some php variable
82
     * @param int|null $options    enable/disable some options see Dumper
83
     *
84
     * @return string  ( the representation of $somePhpVar as a YAML content (single or multiple document accordingly) )
85
     * @throws \Exception on errors during building YAML string coming from Dumper class
86
     * @see    Dumper
87
     */
88 2
    public static function dump($somePhpVar, $options = null):string
89
    {
90
        try {
91 2
            return (new Yaml\Dumper($options))->toString($somePhpVar);
92 1
        } catch (\Throwable $e) {
93 1
            throw new \Exception(__CLASS__." Error dumping", 1, $e);
94
        }
95
    }
96
97
98
    /**
99
     * Builds the YAML representation corresponding to given PHP variable ($somePhpVar)
100
     * AND save it as file with the $fileName provided.
101
     *
102
     * @param string   $fileName   The file name
103
     * @param mixed    $somePhpVar Some php variable
104
     * @param int|null $options    Dumper::constants as options
105
     *
106
     * @return boolean  true if YAML built and saved , false if error during writing file
107
     * @throws \Exception on errors (from Dumper::toString) during building YAML string
108
     * @see    Dumper
109
     */
110 2
    public static function dumpFile(string $fileName, $somePhpVar, $options = null):bool
111
    {
112
        try {
113 2
            return (new Yaml\Dumper($options))->toFile($fileName, $somePhpVar);
114 1
        } catch (\Throwable $e) {
115 1
            throw new \Exception(__CLASS__." Error during dumping '$fileName'", 1, $e);
116
        }
117
    }
118
}