@@ -39,7 +39,7 @@ |
||
39 | 39 | if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
40 | 40 | self::$options = is_int($options) ? $options : self::OPTIONS; |
41 | 41 | self::$result = new DLL; |
42 | - self::$result->setIteratorMode(DLL::IT_MODE_FIFO | DLL::IT_MODE_DELETE); |
|
42 | + self::$result->setIteratorMode(DLL::IT_MODE_FIFO|DLL::IT_MODE_DELETE); |
|
43 | 43 | if ($dataType instanceof YamlObject) { |
44 | 44 | self::dumpYamlObject($dataType); |
45 | 45 | } elseif (is_array($dataType) && $dataType[0] instanceof YamlObject) { |
@@ -21,7 +21,9 @@ discard block |
||
21 | 21 | |
22 | 22 | public function __construct(int $options = null) |
23 | 23 | { |
24 | - if (is_int($options)) self::$options = $options; |
|
24 | + if (is_int($options)) { |
|
25 | + self::$options = $options; |
|
26 | + } |
|
25 | 27 | } |
26 | 28 | |
27 | 29 | /** |
@@ -36,7 +38,9 @@ discard block |
||
36 | 38 | */ |
37 | 39 | public static function toString($dataType, int $options = null):string |
38 | 40 | { |
39 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
41 | + if (is_null($dataType)) { |
|
42 | + throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
43 | + } |
|
40 | 44 | self::$options = is_int($options) ? $options : self::OPTIONS; |
41 | 45 | self::$result = new DLL; |
42 | 46 | self::$result->setIteratorMode(DLL::IT_MODE_FIFO | DLL::IT_MODE_DELETE); |
@@ -74,8 +78,12 @@ discard block |
||
74 | 78 | if (is_scalar($dataType)) { |
75 | 79 | switch (gettype($dataType)) { |
76 | 80 | case 'boolean': return $dataType ? 'true' : 'false'; |
77 | - case 'float': if (is_infinite((float) $dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
|
78 | - case 'double': if (is_nan((float) $dataType)) return '.nan'; |
|
81 | + case 'float': if (is_infinite((float) $dataType)) { |
|
82 | + return $dataType > 0 ? '.inf' : '-.inf'; |
|
83 | + } |
|
84 | + case 'double': if (is_nan((float) $dataType)) { |
|
85 | + return '.nan'; |
|
86 | + } |
|
79 | 87 | default: |
80 | 88 | return $dataType; |
81 | 89 | } |
@@ -25,14 +25,14 @@ discard block |
||
25 | 25 | private static function buildNodeList(NodeList $node, &$parent) |
26 | 26 | { |
27 | 27 | $type = property_exists($node, "type") ? $node->type : null; |
28 | - if ($type & (Y\RAW | Y\LITTERALS)) { |
|
28 | + if ($type&(Y\RAW|Y\LITTERALS)) { |
|
29 | 29 | return self::litteral($node, $type); |
30 | 30 | } |
31 | 31 | $p = $parent; |
32 | 32 | switch ($type) { |
33 | 33 | case Y\MAPPING: //fall through |
34 | - case Y\SET: $p = new \StdClass;break; |
|
35 | - case Y\SEQUENCE: $p = [];break; |
|
34 | + case Y\SET: $p = new \StdClass; break; |
|
35 | + case Y\SEQUENCE: $p = []; break; |
|
36 | 36 | // case Y\KEY: $p = $parent;break; |
37 | 37 | } |
38 | 38 | $out = null; |
@@ -53,10 +53,10 @@ discard block |
||
53 | 53 | { |
54 | 54 | list($line, $type, $value, $identifier) = [$node->line, $node->type, $node->value, $node->identifier]; |
55 | 55 | switch ($type) { |
56 | - case Y\COMMENT: self::$_root->addComment($line, $value);return; |
|
57 | - case Y\DIRECTIVE: return;//TODO |
|
58 | - case Y\ITEM: self::buildItem($value, $parent);return; |
|
59 | - case Y\KEY: self::buildKey($node, $parent);return; |
|
56 | + case Y\COMMENT: self::$_root->addComment($line, $value); return; |
|
57 | + case Y\DIRECTIVE: return; //TODO |
|
58 | + case Y\ITEM: self::buildItem($value, $parent); return; |
|
59 | + case Y\KEY: self::buildKey($node, $parent); return; |
|
60 | 60 | case Y\REF_DEF: //fall through |
61 | 61 | case Y\REF_CALL://TODO: self::build returns what ? |
62 | 62 | $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
@@ -71,8 +71,8 @@ discard block |
||
71 | 71 | case Y\SET_VALUE: |
72 | 72 | $prop = array_keys(get_object_vars($parent)); |
73 | 73 | $key = end($prop); |
74 | - if (property_exists($value, "type") && ($value->type & (Y\ITEM | Y\MAPPING))) { |
|
75 | - $p = $value->type === Y\ITEM ? [] : new \StdClass; |
|
74 | + if (property_exists($value, "type") && ($value->type & (Y\ITEM|Y\MAPPING))) { |
|
75 | + $p = $value->type === Y\ITEM ? [] : new \StdClass; |
|
76 | 76 | self::build($value, $p); |
77 | 77 | } else { |
78 | 78 | $p = self::build($value, $parent->{$key}); |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | return; |
82 | 82 | case Y\TAG: |
83 | 83 | if ($parent === self::$_root) { |
84 | - $parent->addTag($identifier);return; |
|
84 | + $parent->addTag($identifier); return; |
|
85 | 85 | } else {//TODO: have somewhere a list of common tags and their treatment |
86 | 86 | if (in_array($identifier, ['!binary', '!str'])) { |
87 | 87 | if ($value->value instanceof NodeList) $value->value->type = Y\RAW; |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | if (is_null($identifier)) { |
110 | 110 | throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $node->line)); |
111 | 111 | } else { |
112 | - if ($value instanceof Node && ($value->type & (Y\KEY | Y\ITEM))) { |
|
112 | + if ($value instanceof Node && ($value->type & (Y\KEY|Y\ITEM))) { |
|
113 | 113 | $parent->{$identifier} = $value->type & Y\KEY ? new \StdClass : []; |
114 | 114 | self::build($value, $parent->{$identifier}); |
115 | 115 | } elseif (is_object($value)) { |
@@ -175,8 +175,8 @@ discard block |
||
175 | 175 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
176 | 176 | } else { |
177 | 177 | switch (true) { |
178 | - case $isSequence: $list->type = Y\SEQUENCE;break; |
|
179 | - case $isSet: $list->type = Y\SET;break; |
|
178 | + case $isSequence: $list->type = Y\SEQUENCE; break; |
|
179 | + case $isSet: $list->type = Y\SET; break; |
|
180 | 180 | default: $list->type = Y\MAPPING; |
181 | 181 | } |
182 | 182 | } |
@@ -198,10 +198,10 @@ discard block |
||
198 | 198 | $children->rewind(); |
199 | 199 | $refIndent = $children->current()->indent; |
200 | 200 | $separator = $type === Y\RAW ? '' : "\n"; |
201 | - $action = function ($c) { return $c->value; }; |
|
202 | - if ($type & Y\LITT_FOLDED) { |
|
201 | + $action = function($c) { return $c->value; }; |
|
202 | + if ($type&Y\LITT_FOLDED) { |
|
203 | 203 | $separator = ' '; |
204 | - $action = function ($c) use ($refIndent) { |
|
204 | + $action = function($c) use ($refIndent) { |
|
205 | 205 | return $c->indent > $refIndent || ($c->type & Y\BLANK) ? "\n".$c->value : $c->value; |
206 | 206 | }; |
207 | 207 | } |
@@ -10,17 +10,17 @@ discard block |
||
10 | 10 | //public |
11 | 11 | public $errors = []; |
12 | 12 | |
13 | - public const EXCLUDE_DIRECTIVES = 1;//DONT include_directive |
|
14 | - public const IGNORE_COMMENTS = 2;//DONT include_comments |
|
15 | - public const NO_PARSING_EXCEPTIONS = 4;//THROW Exception on parsing Errors |
|
16 | - public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
|
13 | + public const EXCLUDE_DIRECTIVES = 1; //DONT include_directive |
|
14 | + public const IGNORE_COMMENTS = 2; //DONT include_comments |
|
15 | + public const NO_PARSING_EXCEPTIONS = 4; //THROW Exception on parsing Errors |
|
16 | + public const NO_OBJECT_FOR_DATE = 8; //DONT import date strings as dateTime Object |
|
17 | 17 | //privates |
18 | 18 | /* @var null|string */ |
19 | 19 | private $content; |
20 | 20 | /* @var null|string */ |
21 | 21 | private $filePath; |
22 | 22 | /* @var int */ |
23 | - private $debug = 0;///TODO: determine levels |
|
23 | + private $debug = 0; ///TODO: determine levels |
|
24 | 24 | /* @var int */ |
25 | 25 | private $options = 0; |
26 | 26 | //Exceptions messages |
@@ -31,8 +31,8 @@ discard block |
||
31 | 31 | |
32 | 32 | public function __construct($absolutePath = null, $options = null, $debug = 0) |
33 | 33 | { |
34 | - $this->debug = is_int($debug) ? min($debug, 3) : 1; |
|
35 | - $this->options = is_int($options) ? $options : $this->options; |
|
34 | + $this->debug = is_int($debug) ? min($debug, 3) : 1; |
|
35 | + $this->options = is_int($options) ? $options : $this->options; |
|
36 | 36 | if (is_string($absolutePath)) { |
37 | 37 | $this->load($absolutePath); |
38 | 38 | } |
@@ -83,10 +83,10 @@ discard block |
||
83 | 83 | if (!is_array($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
84 | 84 | $previous = $root = new Node(); |
85 | 85 | $emptyLines = []; |
86 | - $specialTypes = Y\LITTERALS | Y\BLANK; |
|
86 | + $specialTypes = Y\LITTERALS|Y\BLANK; |
|
87 | 87 | try { |
88 | 88 | foreach ($source as $lineNb => $lineString) { |
89 | - $n = new Node($lineString, $lineNb + 1);//TODO: useful???-> $this->debug && var_dump($n); |
|
89 | + $n = new Node($lineString, $lineNb + 1); //TODO: useful???-> $this->debug && var_dump($n); |
|
90 | 90 | $deepest = $previous->getDeepestNode(); |
91 | 91 | if ($deepest->type & Y\PARTIAL) { |
92 | 92 | //TODO:verify this edge case |
@@ -105,8 +105,8 @@ discard block |
||
105 | 105 | } |
106 | 106 | $emptyLines = []; |
107 | 107 | switch ($n->indent <=> $previous->indent) { |
108 | - case -1: $previous->getParent($n->indent)->add($n);break; |
|
109 | - case 0: $previous->getParent()->add($n);break; |
|
108 | + case -1: $previous->getParent($n->indent)->add($n); break; |
|
109 | + case 0: $previous->getParent()->add($n); break; |
|
110 | 110 | default: |
111 | 111 | if ($this->onDeepestType($n, $previous, $lineString)) continue 2; |
112 | 112 | $previous->add($n); |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | return $out; |
123 | 123 | } catch (\Error|\Exception|\ParseError $e) { |
124 | 124 | $file = basename($this->filePath); |
125 | - $message = basename($e->getFile())."@".$e->getLine().":".$e->getMessage()." in '$file' @".($lineNb+1)."\n"; |
|
125 | + $message = basename($e->getFile())."@".$e->getLine().":".$e->getMessage()." in '$file' @".($lineNb + 1)."\n"; |
|
126 | 126 | if ($e instanceof \ParseError && ($this->options & self::NO_PARSING_EXCEPTIONS)) { |
127 | 127 | trigger_error($message, E_USER_WARNING); |
128 | 128 | $this->errors[] = $message; |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | // if ($deepest->type & Y\LITTERALS) { |
161 | 161 | // $n->value = trim($lineString);//fall through |
162 | 162 | // } |
163 | - if (($n->type & Y\SCALAR) && ($deepest->type & (Y\LITTERALS | Y\REF_DEF | Y\SET_VALUE)) && is_null($deepest->value)) { |
|
163 | + if (($n->type & Y\SCALAR) && ($deepest->type & (Y\LITTERALS|Y\REF_DEF|Y\SET_VALUE)) && is_null($deepest->value)) { |
|
164 | 164 | $previous = $deepest; |
165 | 165 | } |
166 | 166 | if (($deepest->type & Y\TAG) && is_null($deepest->value)) { |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | // // if ($previous->type & Y\ITEM && $n->type & Y\KEY) { |
170 | 170 | // // $previous |
171 | 171 | // // } |
172 | - if ($deepest->type & (Y\BLANK | Y\SCALAR)) {//|| ($previous->type & (Y\ITEM | Y\SET_KEY))) { |
|
172 | + if ($deepest->type & (Y\BLANK|Y\SCALAR)) {//|| ($previous->type & (Y\ITEM | Y\SET_KEY))) { |
|
173 | 173 | $previous = $deepest->getParent(); |
174 | 174 | // // if ($n->type === Y\SCALAR && ($deepest->getParent()->type & Y\LITTERALS)) { |
175 | 175 | // // // $deepest->type = Y\SCALAR; |
@@ -78,9 +78,13 @@ discard block |
||
78 | 78 | public function parse($strContent = null) |
79 | 79 | { |
80 | 80 | $source = $this->content; |
81 | - if (is_null($source)) $source = preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
|
81 | + if (is_null($source)) { |
|
82 | + $source = preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
|
83 | + } |
|
82 | 84 | //TODO : be more permissive on $strContent values |
83 | - if (!is_array($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
85 | + if (!is_array($source)) { |
|
86 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
87 | + } |
|
84 | 88 | $previous = $root = new Node(); |
85 | 89 | $emptyLines = []; |
86 | 90 | $specialTypes = Y\LITTERALS | Y\BLANK; |
@@ -108,7 +112,9 @@ discard block |
||
108 | 112 | case -1: $previous->getParent($n->indent)->add($n);break; |
109 | 113 | case 0: $previous->getParent()->add($n);break; |
110 | 114 | default: |
111 | - if ($this->onDeepestType($n, $previous, $lineString)) continue 2; |
|
115 | + if ($this->onDeepestType($n, $previous, $lineString)) { |
|
116 | + continue 2; |
|
117 | + } |
|
112 | 118 | $previous->add($n); |
113 | 119 | } |
114 | 120 | $previous = $n; |
@@ -147,8 +153,12 @@ discard block |
||
147 | 153 | } |
148 | 154 | } |
149 | 155 | if ($n->type & Y\BLANK) { |
150 | - if ($previous->type & Y\SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
151 | - if ($deepest->type & Y\LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
156 | + if ($previous->type & Y\SCALAR) { |
|
157 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
158 | + } |
|
159 | + if ($deepest->type & Y\LITTERALS) { |
|
160 | + $emptyLines[] = $n->setParent($deepest); |
|
161 | + } |
|
152 | 162 | return true; |
153 | 163 | } |
154 | 164 | return false; |