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