|
1
|
|
|
<?php |
|
2
|
|
|
namespace Dallgoot\Yaml; |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
use Dallgoot\Yaml as Y; |
|
5
|
|
|
// declaring constants for Dallgoot\Yaml |
|
6
|
|
|
$TYPES = ['DIRECTIVE', |
|
7
|
|
|
'DOC_START', |
|
8
|
|
|
'DOC_END', |
|
9
|
|
|
'COMMENT', |
|
10
|
|
|
'BLANK', |
|
11
|
|
|
'ROOT', |
|
12
|
|
|
'KEY', |
|
13
|
|
|
'ITEM', |
|
14
|
|
|
'MAPPING', |
|
15
|
|
|
'SEQUENCE', |
|
16
|
|
|
'MAPPING_SHORT', |
|
17
|
|
|
'SEQUENCE_SHORT', |
|
18
|
|
|
'PARTIAL', |
|
19
|
|
|
'LITT', //litteral |
|
20
|
|
|
'LITT_FOLDED',//litteral |
|
21
|
|
|
'SCALAR', |
|
22
|
|
|
'TAG', |
|
23
|
|
|
'JSON', |
|
24
|
|
|
'QUOTED', |
|
25
|
|
|
'RAW', |
|
26
|
|
|
'REF_DEF', //reference |
|
27
|
|
|
'REF_CALL', //reference |
|
28
|
|
|
'SET', |
|
29
|
|
|
'SET_KEY', |
|
30
|
|
|
'SET_VALUE']; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
foreach ($TYPES as $power => $name) { |
|
34
|
|
|
define(__NAMESPACE__."\\$name", 2**$power); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
const LITTERALS = Y\LITT | Y\LITT_FOLDED; |
|
|
|
|
|
|
38
|
|
|
// print_r(get_defined_constants(true)['user']); |
|
39
|
|
|
|
|
40
|
|
|
namespace Dallgoot; |
|
41
|
|
|
|
|
42
|
|
|
class Yaml |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
/* @var null|array */ |
|
45
|
|
|
private static $TYPE_NAMES = null; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Gets the name for a given constant declared in the Dallgoot\Yaml namespace |
|
49
|
|
|
* @param integer $typeInteger The constant value |
|
|
|
|
|
|
50
|
|
|
* |
|
51
|
|
|
* @return string The name. |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
|
|
public static function getName($typeInteger) |
|
54
|
|
|
{ |
|
55
|
|
|
if(is_null(self::$TYPE_NAMES)) { |
|
|
|
|
|
|
56
|
|
|
$f = function ($v) { return str_replace('Dallgoot\Yaml\\', '', $v);}; |
|
|
|
|
|
|
57
|
|
|
self::$TYPE_NAMES = array_map($f, array_flip(get_defined_constants(true)['user'])); |
|
58
|
|
|
} |
|
59
|
|
|
return self::$TYPE_NAMES[$typeInteger]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Parse the given Yaml string to a PHP type |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $someYaml Some yaml |
|
|
|
|
|
|
66
|
|
|
* |
|
67
|
|
|
* @return YamlObject|array ( return a PHP type representation with Yaml document as YamlObject and multiple |
|
|
|
|
|
|
68
|
|
|
* documents as an array of YamlObject ) |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function parse(string $someYaml) |
|
71
|
|
|
{ |
|
72
|
|
|
return (new Yaml\Loader)->parse($someYaml); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
|
|
|
|
|
76
|
|
|
* Load the given file and parse its content (assumed YAML) to a PHP type |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $someYaml Some yaml |
|
|
|
|
|
|
79
|
|
|
* |
|
80
|
|
|
* @return YamlObject|array ( return a PHP type representation with Yaml document as YamlObject and multiple |
|
|
|
|
|
|
81
|
|
|
* documents as an array of YamlObject ) |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function parseFile(string $fileName) |
|
84
|
|
|
{ |
|
85
|
|
|
return (new Yaml\Loader($fileName))->parse(); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the YAML representation corresponding to given PHP variable |
|
90
|
|
|
* |
|
91
|
|
|
* @param mixed $somePhpVar Some php variable |
|
|
|
|
|
|
92
|
|
|
* |
|
93
|
|
|
* @return string ( the representation of $somePhpVar as a YAML content (single or multiple document according to argument) ) |
|
|
|
|
|
|
94
|
|
|
* @throws Exception on errors during building YAML string |
|
|
|
|
|
|
95
|
|
|
* @see Dumper::toString |
|
|
|
|
|
|
96
|
|
|
*/ |
|
97
|
|
|
public static function dump($somePhpVar):string |
|
98
|
|
|
{ |
|
99
|
|
|
return Yaml\Dumper::toString($somePhpVar); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Builds the YAML representation corresponding to given PHP variable ($somePhpVar) |
|
104
|
|
|
* AND save it as file with the $fileName provided. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $fileName The file name |
|
|
|
|
|
|
107
|
|
|
* @param mixed $somePhpVar Some php variable |
|
|
|
|
|
|
108
|
|
|
* |
|
109
|
|
|
* @return boolean true if YAML built and saved , false otherwise |
|
|
|
|
|
|
110
|
|
|
* @throws Exception on errors during building YAML string |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
|
|
public static function dumpFile(string $fileName, $somePhpVar):boolean |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
return Yaml\Dumper::toFile($fileName, $somePhpVar); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|