|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* TODO |
|
7
|
|
|
* @category tag in class comment |
|
|
|
|
|
|
8
|
|
|
* @package tag in class comment |
|
|
|
|
|
|
9
|
|
|
* @author tag in class comment |
|
|
|
|
|
|
10
|
|
|
* @license tag in class comment |
|
|
|
|
|
|
11
|
|
|
*/ |
|
|
|
|
|
|
12
|
|
|
final class Yaml |
|
13
|
|
|
{ |
|
14
|
|
|
const BLANK = 1; |
|
15
|
|
|
const COMMENT = 2; |
|
16
|
|
|
const COMPACT_MAPPING = 4; |
|
17
|
|
|
const COMPACT_SEQUENCE = 8; |
|
18
|
|
|
const DIRECTIVE = 16; |
|
19
|
|
|
const DOC_END = 32; |
|
20
|
|
|
const DOC_START = 64; |
|
21
|
|
|
const ITEM = 128; |
|
22
|
|
|
const JSON = 256; |
|
23
|
|
|
const KEY = 512; |
|
24
|
|
|
const LITT = 1024;//litteral |
|
25
|
|
|
const LITT_FOLDED = 2048;//litteral |
|
26
|
|
|
const MAPPING = 4096; |
|
27
|
|
|
const PARTIAL = 8192; |
|
28
|
|
|
const QUOTED = 16384; |
|
29
|
|
|
const RAW = 32768; |
|
30
|
|
|
const REF_CALL = 65536;//reference |
|
31
|
|
|
const REF_DEF = 131072;//reference |
|
32
|
|
|
const ROOT = 262144; |
|
33
|
|
|
const SCALAR = 524288; |
|
34
|
|
|
const SEQUENCE = 1048576; |
|
35
|
|
|
const SET = 2097152; |
|
36
|
|
|
const SET_KEY = 4194304; |
|
37
|
|
|
const SET_VALUE = 8388608; |
|
38
|
|
|
const TAG = 16777216; |
|
39
|
|
|
|
|
40
|
|
|
const LITTERALS = self::LITT|self::LITT_FOLDED; |
|
41
|
|
|
|
|
42
|
|
|
/* @var null|array */ |
|
43
|
|
|
public static $TYPE_NAMES = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Gets the name for a given constant declared in the Dallgoot\Yaml namespace |
|
47
|
|
|
* @param integer $typeInteger The constant value |
|
|
|
|
|
|
48
|
|
|
* |
|
49
|
|
|
* @return string The name. |
|
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
|
|
public static function getName(int $typeInteger):string |
|
52
|
|
|
{ |
|
53
|
|
|
if (is_null(self::$TYPE_NAMES)) { |
|
54
|
|
|
$oClass = new \ReflectionClass(__CLASS__); |
|
55
|
|
|
self::$TYPE_NAMES = array_flip($oClass->getConstants()); |
|
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, $options = null, $debug = null) |
|
71
|
|
|
{ |
|
72
|
|
|
return (new Loader(null, $options, $debug))->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 Yaml\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, $options = null, $debug = null) |
|
84
|
|
|
{ |
|
85
|
|
|
return (new Loader($fileName, $options, $debug))->parse(); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the YAML representation corresponding to given PHP variable |
|
90
|
|
|
* |
|
91
|
|
|
* @param mixed $somePhpVar Some php variable |
|
|
|
|
|
|
92
|
|
|
* @param int|null $options Dumper::constants as options |
|
|
|
|
|
|
93
|
|
|
* |
|
94
|
|
|
* @return string ( the representation of $somePhpVar as a YAML content (single or multiple document according to argument) ) |
|
|
|
|
|
|
95
|
|
|
* @throws Exception on errors during building YAML string |
|
|
|
|
|
|
96
|
|
|
* @see Dumper::toString |
|
|
|
|
|
|
97
|
|
|
*/ |
|
98
|
|
|
public static function dump($somePhpVar, $options = null):string |
|
99
|
|
|
{ |
|
100
|
|
|
return Dumper::toString($somePhpVar, $options); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Builds the YAML representation corresponding to given PHP variable ($somePhpVar) |
|
105
|
|
|
* AND save it as file with the $fileName provided. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $fileName The file name |
|
|
|
|
|
|
108
|
|
|
* @param mixed $somePhpVar Some php variable |
|
|
|
|
|
|
109
|
|
|
* @param int|null $options Dumper::constants as options |
|
|
|
|
|
|
110
|
|
|
* |
|
111
|
|
|
* @return boolean true if YAML built and saved , false otherwise |
|
|
|
|
|
|
112
|
|
|
* @throws Exception on errors during building YAML string |
|
|
|
|
|
|
113
|
|
|
*/ |
|
114
|
|
|
public static function dumpFile(string $fileName, $somePhpVar, $options = null):bool |
|
115
|
|
|
{ |
|
116
|
|
|
return Dumper::toFile($fileName, $somePhpVar, $options); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|