@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | private static function buildNodeList(NodeList $node, &$parent) |
| 45 | 45 | { |
| 46 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
| 46 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
| 47 | 47 | return self::buildLitteral($node, $node->type); |
| 48 | 48 | } |
| 49 | 49 | $p = $parent; |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | case Y::MAPPING: // fall through |
| 52 | 52 | // case Y::SET_KEY: // fall through |
| 53 | 53 | case Y::SET: $p = new \StdClass; var_dump('in set'); break; |
| 54 | - case Y::SEQUENCE: $p = [];break; |
|
| 54 | + case Y::SEQUENCE: $p = []; break; |
|
| 55 | 55 | // case Y::KEY: $p = $parent;break; |
| 56 | 56 | } |
| 57 | 57 | $out = null; |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | private static function buildNode(Node $node, &$parent) |
| 80 | 80 | { |
| 81 | 81 | extract((array) $node, EXTR_REFS); |
| 82 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
| 82 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
| 83 | 83 | if (is_object($value)) { |
| 84 | 84 | $tmp = self::build($value, $parent) ?? $parent; |
| 85 | 85 | } else { |
@@ -200,15 +200,15 @@ discard block |
||
| 200 | 200 | { |
| 201 | 201 | self::$_root = new YamlObject(); |
| 202 | 202 | $childTypes = $list->getTypes(); |
| 203 | - $isaMapping = (bool) (Y::KEY | Y::MAPPING) & $childTypes; |
|
| 203 | + $isaMapping = (bool) (Y::KEY|Y::MAPPING)&$childTypes; |
|
| 204 | 204 | $isaSequence = (bool) Y::ITEM & $childTypes; |
| 205 | 205 | $isaSet = (bool) Y::SET_VALUE & $childTypes; |
| 206 | 206 | if ($isaMapping && $isaSequence) { |
| 207 | 207 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
| 208 | 208 | } else { |
| 209 | 209 | switch (true) { |
| 210 | - case $isaSequence: $list->type = Y::SEQUENCE;break; |
|
| 211 | - case $isaSet: $list->type = Y::SET;break; |
|
| 210 | + case $isaSequence: $list->type = Y::SEQUENCE; break; |
|
| 211 | + case $isaSet: $list->type = Y::SET; break; |
|
| 212 | 212 | default: $list->type = Y::MAPPING; |
| 213 | 213 | } |
| 214 | 214 | } |
@@ -243,22 +243,22 @@ discard block |
||
| 243 | 243 | $lines[] = self::buildLitteral($child->value, $type); |
| 244 | 244 | } else { |
| 245 | 245 | $prefix = ''; |
| 246 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 246 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 247 | 247 | $prefix = "\n"; |
| 248 | 248 | } |
| 249 | 249 | if (!($child->type & (Y::SCALAR|Y::BLANK))) { |
| 250 | 250 | switch ($child->type) { |
| 251 | - case Y::ITEM: $child->value = '- '.$child->value;break; |
|
| 252 | - case Y::COMMENT: $child->value = '# '.$child->value;break; |
|
| 251 | + case Y::ITEM: $child->value = '- '.$child->value; break; |
|
| 252 | + case Y::COMMENT: $child->value = '# '.$child->value; break; |
|
| 253 | 253 | default: die(Y::getName($child->type)); |
| 254 | 254 | } |
| 255 | 255 | } |
| 256 | 256 | $lines[] = $prefix.$child->value; |
| 257 | 257 | } |
| 258 | 258 | } |
| 259 | - if ($type & Y::RAW) return implode('', $lines); |
|
| 260 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
| 261 | - if ($type & Y::LITT_FOLDED) return implode(' ', $lines); |
|
| 259 | + if ($type&Y::RAW) return implode('', $lines); |
|
| 260 | + if ($type&Y::LITT) return implode("\n", $lines); |
|
| 261 | + if ($type&Y::LITT_FOLDED) return implode(' ', $lines); |
|
| 262 | 262 | return ''; |
| 263 | 263 | } |
| 264 | 264 | |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | private function buildSetKey(Node $node, &$parent) |
| 274 | 274 | { |
| 275 | 275 | $built = self::build($node->value, $parent); |
| 276 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
| 276 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
| 277 | 277 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 278 | 278 | if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
| 279 | 279 | $parent->{trim($key, '\'" ')} = null; |
@@ -29,7 +29,9 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | private static function build(object $node, &$parent = null) |
| 31 | 31 | { |
| 32 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
| 32 | + if ($node instanceof NodeList) { |
|
| 33 | + return self::buildNodeList($node, $parent); |
|
| 34 | + } |
|
| 33 | 35 | return self::buildNode($node, $parent); |
| 34 | 36 | } |
| 35 | 37 | |
@@ -85,7 +87,9 @@ discard block |
||
| 85 | 87 | } else { |
| 86 | 88 | $tmp = $node->getPhpValue(); |
| 87 | 89 | } |
| 88 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 90 | + if ($type === Y::REF_DEF) { |
|
| 91 | + self::$_root->addReference($identifier, $tmp); |
|
| 92 | + } |
|
| 89 | 93 | return self::$_root->getReference($identifier); |
| 90 | 94 | } |
| 91 | 95 | $typesActions = [Y::COMMENT => 'buildComment', |
@@ -176,10 +180,14 @@ discard block |
||
| 176 | 180 | } |
| 177 | 181 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 178 | 182 | foreach ($_root->value as $child) { |
| 179 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 183 | + if ($child->type & Y::DOC_START) { |
|
| 184 | + $totalDocStart++; |
|
| 185 | + } |
|
| 180 | 186 | //if 0 or 1 DOC_START = we are still in first document |
| 181 | 187 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 182 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 188 | + if (!isset($documents[$currentDoc])) { |
|
| 189 | + $documents[$currentDoc] = new NodeList(); |
|
| 190 | + } |
|
| 183 | 191 | $documents[$currentDoc]->push($child); |
| 184 | 192 | } |
| 185 | 193 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
@@ -256,9 +264,15 @@ discard block |
||
| 256 | 264 | $lines[] = $prefix.$child->value; |
| 257 | 265 | } |
| 258 | 266 | } |
| 259 | - if ($type & Y::RAW) return implode('', $lines); |
|
| 260 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
| 261 | - if ($type & Y::LITT_FOLDED) return implode(' ', $lines); |
|
| 267 | + if ($type & Y::RAW) { |
|
| 268 | + return implode('', $lines); |
|
| 269 | + } |
|
| 270 | + if ($type & Y::LITT) { |
|
| 271 | + return implode("\n", $lines); |
|
| 272 | + } |
|
| 273 | + if ($type & Y::LITT_FOLDED) { |
|
| 274 | + return implode(' ', $lines); |
|
| 275 | + } |
|
| 262 | 276 | return ''; |
| 263 | 277 | } |
| 264 | 278 | |
@@ -275,7 +289,9 @@ discard block |
||
| 275 | 289 | $built = self::build($node->value, $parent); |
| 276 | 290 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
| 277 | 291 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 278 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 292 | + if (empty($key)) { |
|
| 293 | + throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 294 | + } |
|
| 279 | 295 | $parent->{trim($key, '\'" ')} = null; |
| 280 | 296 | } |
| 281 | 297 | |
@@ -314,8 +330,11 @@ discard block |
||
| 314 | 330 | } |
| 315 | 331 | //TODO: have somewhere a list of common tags and their treatment |
| 316 | 332 | if (in_array($node->identifier, ['!binary', '!str'])) { |
| 317 | - if ($node->value->value instanceof NodeList) $node->value->value->type = Y::RAW; |
|
| 318 | - else $node->value->type = Y::RAW; |
|
| 333 | + if ($node->value->value instanceof NodeList) { |
|
| 334 | + $node->value->value->type = Y::RAW; |
|
| 335 | + } else { |
|
| 336 | + $node->value->type = Y::RAW; |
|
| 337 | + } |
|
| 319 | 338 | } |
| 320 | 339 | $val = is_null($node->value) ? null : self::build(/** @scrutinizer ignore-type */ $node->value, $node); |
| 321 | 340 | return new Tag($node->identifier, $val); |
@@ -18,17 +18,17 @@ discard block |
||
| 18 | 18 | /* @var null|string */ |
| 19 | 19 | public $error; |
| 20 | 20 | |
| 21 | - public const EXCLUDE_DIRECTIVES = 1;//DONT include_directive |
|
| 22 | - public const IGNORE_COMMENTS = 2;//DONT include_comments |
|
| 23 | - public const NO_PARSING_EXCEPTIONS = 4;//THROW Exception on parsing Errors |
|
| 24 | - public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
|
| 21 | + public const EXCLUDE_DIRECTIVES = 1; //DONT include_directive |
|
| 22 | + public const IGNORE_COMMENTS = 2; //DONT include_comments |
|
| 23 | + public const NO_PARSING_EXCEPTIONS = 4; //THROW Exception on parsing Errors |
|
| 24 | + public const NO_OBJECT_FOR_DATE = 8; //DONT import date strings as dateTime Object |
|
| 25 | 25 | //privates |
| 26 | 26 | /* @var null|string */ |
| 27 | 27 | private $content; |
| 28 | 28 | /* @var null|string */ |
| 29 | 29 | private $filePath; |
| 30 | 30 | /* @var integer */ |
| 31 | - private $debug = 0;///TODO: determine levels |
|
| 31 | + private $debug = 0; ///TODO: determine levels |
|
| 32 | 32 | /* @var integer */ |
| 33 | 33 | private $options = 0; |
| 34 | 34 | //Exceptions messages |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $previous = $root = new Node(); |
| 91 | 91 | $emptyLines = []; |
| 92 | 92 | try { |
| 93 | - $gen = function () use($source) { |
|
| 93 | + $gen = function() use($source) { |
|
| 94 | 94 | foreach ($source as $key => $value) { |
| 95 | 95 | yield ++$key => $value; |
| 96 | 96 | } |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | break; |
| 110 | 110 | case 0: $target = $previous->getParent(); |
| 111 | 111 | break; |
| 112 | - default: $target = $previous->type & Y::ITEM && $n->type != Y::SET_VALUE? $previous->getDeepestNode()->getParent($n->indent) : $previous; |
|
| 112 | + default: $target = $previous->type & Y::ITEM && $n->type != Y::SET_VALUE ? $previous->getDeepestNode()->getParent($n->indent) : $previous; |
|
| 113 | 113 | } |
| 114 | 114 | if ($this->onContextType($n, $target, $lineString)) continue; |
| 115 | 115 | $target->add($n); |
@@ -86,7 +86,9 @@ discard block |
||
| 86 | 86 | { |
| 87 | 87 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
| 88 | 88 | //TODO : be more permissive on $strContent values |
| 89 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 89 | + if (!is_array($source) || !count($source)) { |
|
| 90 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 91 | + } |
|
| 90 | 92 | $previous = $root = new Node(); |
| 91 | 93 | $emptyLines = []; |
| 92 | 94 | try { |
@@ -98,7 +100,9 @@ discard block |
||
| 98 | 100 | foreach ($gen() as $lineNb => $lineString) { |
| 99 | 101 | $n = new Node($lineString, $lineNb); |
| 100 | 102 | if ($n->type & (Y::LITTERALS|Y::BLANK|Y::COMMENT)) { |
| 101 | - if ($this->onSpecialType($n, $previous, $emptyLines)) continue; |
|
| 103 | + if ($this->onSpecialType($n, $previous, $emptyLines)) { |
|
| 104 | + continue; |
|
| 105 | + } |
|
| 102 | 106 | } |
| 103 | 107 | foreach ($emptyLines as $blankNode) { |
| 104 | 108 | $blankNode->getParent()->add($blankNode); |
@@ -111,7 +115,9 @@ discard block |
||
| 111 | 115 | break; |
| 112 | 116 | default: $target = $previous->type & Y::ITEM && $n->type != Y::SET_VALUE? $previous->getDeepestNode()->getParent($n->indent) : $previous; |
| 113 | 117 | } |
| 114 | - if ($this->onContextType($n, $target, $lineString)) continue; |
|
| 118 | + if ($this->onContextType($n, $target, $lineString)) { |
|
| 119 | + continue; |
|
| 120 | + } |
|
| 115 | 121 | $target->add($n); |
| 116 | 122 | $previous = $n; |
| 117 | 123 | } |
@@ -140,8 +146,12 @@ discard block |
||
| 140 | 146 | { |
| 141 | 147 | $deepest = $previous->getDeepestNode(); |
| 142 | 148 | if ($n->type & Y::BLANK) { |
| 143 | - if ($previous->type & Y::SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 144 | - if ($deepest->type & Y::LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
| 149 | + if ($previous->type & Y::SCALAR) { |
|
| 150 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 151 | + } |
|
| 152 | + if ($deepest->type & Y::LITTERALS) { |
|
| 153 | + $emptyLines[] = $n->setParent($deepest); |
|
| 154 | + } |
|
| 145 | 155 | return true; |
| 146 | 156 | } |
| 147 | 157 | //comment is fullline : forces 'root' as parent if not inside a LITTERAL |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | public function add(Node $child) |
| 88 | 88 | { |
| 89 | - if($this->type & (Y::SCALAR|Y::QUOTED)) { |
|
| 89 | + if ($this->type & (Y::SCALAR|Y::QUOTED)) { |
|
| 90 | 90 | $this->getParent()->add($child); |
| 91 | 91 | return; |
| 92 | 92 | } |
@@ -171,8 +171,8 @@ discard block |
||
| 171 | 171 | if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
| 172 | 172 | if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
| 173 | 173 | // Note : php don't like '?' as an array key -_- |
| 174 | - if($first === '?') return [Y::SET_KEY, empty(trim($v)) ? null : new Node(ltrim($v), $this->line)]; |
|
| 175 | - $characters = [ '#' => [Y::COMMENT, ltrim($v)], |
|
| 174 | + if ($first === '?') return [Y::SET_KEY, empty(trim($v)) ? null : new Node(ltrim($v), $this->line)]; |
|
| 175 | + $characters = ['#' => [Y::COMMENT, ltrim($v)], |
|
| 176 | 176 | "-" => $this->onHyphen($nodeValue), |
| 177 | 177 | '%' => [Y::DIRECTIVE, ltrim($v)], |
| 178 | 178 | ':' => [Y::SET_VALUE, empty(trim($v)) ? null : new Node(ltrim($v), $this->line)], |
@@ -291,8 +291,8 @@ discard block |
||
| 291 | 291 | { |
| 292 | 292 | $v = &$this->value; |
| 293 | 293 | if (is_null($v)) return null; |
| 294 | - if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
|
| 295 | - if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 294 | + if ($this->type & (Y::REF_CALL|Y::SCALAR)) return self::getScalar($v); |
|
| 295 | + if ($this->type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 296 | 296 | $expected = [Y::JSON => json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
| 297 | 297 | Y::QUOTED => substr($v, 1, -1), |
| 298 | 298 | Y::RAW => strval($v)]; |
@@ -354,7 +354,7 @@ discard block |
||
| 354 | 354 | //TODO : this should handle references present inside the string |
| 355 | 355 | $out = new Compact(); |
| 356 | 356 | if ($type === Y::COMPACT_SEQUENCE) { |
| 357 | - $f = function ($e) { return self::getScalar(trim($e));}; |
|
| 357 | + $f = function($e) { return self::getScalar(trim($e)); }; |
|
| 358 | 358 | //TODO : that's not robust enough, improve it |
| 359 | 359 | foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) { |
| 360 | 360 | $out[$key] = $value; |
@@ -68,7 +68,9 @@ discard block |
||
| 68 | 68 | if ($this->type === Y::ROOT) { |
| 69 | 69 | return $this; |
| 70 | 70 | } |
| 71 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
| 71 | + if (!is_int($indent)) { |
|
| 72 | + return $this->parent ?? $this; |
|
| 73 | + } |
|
| 72 | 74 | $cursor = $this; |
| 73 | 75 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
| 74 | 76 | $cursor = $cursor->parent; |
@@ -103,13 +105,21 @@ discard block |
||
| 103 | 105 | $this->value->push($current); |
| 104 | 106 | } |
| 105 | 107 | //modify type according to child |
| 106 | - if ($current->type & Y::SET_KEY) $this->value->type = Y::SET; |
|
| 107 | - if ($current->type & Y::KEY) $this->value->type = Y::MAPPING; |
|
| 108 | - if ($current->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
|
| 108 | + if ($current->type & Y::SET_KEY) { |
|
| 109 | + $this->value->type = Y::SET; |
|
| 110 | + } |
|
| 111 | + if ($current->type & Y::KEY) { |
|
| 112 | + $this->value->type = Y::MAPPING; |
|
| 113 | + } |
|
| 114 | + if ($current->type & Y::ITEM) { |
|
| 115 | + $this->value->type = Y::SEQUENCE; |
|
| 116 | + } |
|
| 109 | 117 | } |
| 110 | 118 | $this->value->push($child); |
| 111 | 119 | |
| 112 | - if ($this->type & Y::LITTERALS) $this->value->type = $this->type; |
|
| 120 | + if ($this->type & Y::LITTERALS) { |
|
| 121 | + $this->value->type = $this->type; |
|
| 122 | + } |
|
| 113 | 123 | } |
| 114 | 124 | } |
| 115 | 125 | |
@@ -168,10 +178,16 @@ discard block |
||
| 168 | 178 | $type = R::isProperlyQuoted($nodeValue) ? Y::QUOTED : Y::PARTIAL; |
| 169 | 179 | return [$type, $nodeValue]; |
| 170 | 180 | } |
| 171 | - if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
|
| 172 | - if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
|
| 181 | + if (in_array($first, ['{', '['])) { |
|
| 182 | + return $this->onObject($nodeValue); |
|
| 183 | + } |
|
| 184 | + if (in_array($first, ['!', '&', '*'])) { |
|
| 185 | + return $this->onNodeAction($nodeValue); |
|
| 186 | + } |
|
| 173 | 187 | // Note : php don't like '?' as an array key -_- |
| 174 | - if($first === '?') return [Y::SET_KEY, empty(trim($v)) ? null : new Node(ltrim($v), $this->line)]; |
|
| 188 | + if($first === '?') { |
|
| 189 | + return [Y::SET_KEY, empty(trim($v)) ? null : new Node(ltrim($v), $this->line)]; |
|
| 190 | + } |
|
| 175 | 191 | $characters = [ '#' => [Y::COMMENT, ltrim($v)], |
| 176 | 192 | "-" => $this->onHyphen($nodeValue), |
| 177 | 193 | '%' => [Y::DIRECTIVE, ltrim($v)], |
@@ -224,9 +240,15 @@ discard block |
||
| 224 | 240 | private function onObject($value):array |
| 225 | 241 | { |
| 226 | 242 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 227 | - if (json_last_error() === JSON_ERROR_NONE) return [Y::JSON, $value]; |
|
| 228 | - if (preg_match(R::MAPPING, $value)) return [Y::COMPACT_MAPPING, $value]; |
|
| 229 | - if (preg_match(R::SEQUENCE, $value)) return [Y::COMPACT_SEQUENCE, $value]; |
|
| 243 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
| 244 | + return [Y::JSON, $value]; |
|
| 245 | + } |
|
| 246 | + if (preg_match(R::MAPPING, $value)) { |
|
| 247 | + return [Y::COMPACT_MAPPING, $value]; |
|
| 248 | + } |
|
| 249 | + if (preg_match(R::SEQUENCE, $value)) { |
|
| 250 | + return [Y::COMPACT_SEQUENCE, $value]; |
|
| 251 | + } |
|
| 230 | 252 | return [Y::PARTIAL, $value]; |
| 231 | 253 | } |
| 232 | 254 | |
@@ -242,7 +264,9 @@ discard block |
||
| 242 | 264 | { |
| 243 | 265 | if (substr($nodeValue, 0, 3) === '---') { |
| 244 | 266 | $rest = trim(substr($nodeValue, 3)); |
| 245 | - if (empty($rest)) return [Y::DOC_START, null]; |
|
| 267 | + if (empty($rest)) { |
|
| 268 | + return [Y::DOC_START, null]; |
|
| 269 | + } |
|
| 246 | 270 | $n = new Node($rest, $this->line); |
| 247 | 271 | $n->indent = $this->indent + 4; |
| 248 | 272 | return [Y::DOC_START, $n->setParent($this)]; |
@@ -290,9 +314,15 @@ discard block |
||
| 290 | 314 | public function getPhpValue() |
| 291 | 315 | { |
| 292 | 316 | $v = &$this->value; |
| 293 | - if (is_null($v)) return null; |
|
| 294 | - if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
|
| 295 | - if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 317 | + if (is_null($v)) { |
|
| 318 | + return null; |
|
| 319 | + } |
|
| 320 | + if ($this->type & (Y::REF_CALL | Y::SCALAR)) { |
|
| 321 | + return self::getScalar($v); |
|
| 322 | + } |
|
| 323 | + if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) { |
|
| 324 | + return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 325 | + } |
|
| 296 | 326 | $expected = [Y::JSON => json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
| 297 | 327 | Y::QUOTED => substr($v, 1, -1), |
| 298 | 328 | Y::RAW => strval($v)]; |
@@ -321,9 +351,15 @@ discard block |
||
| 321 | 351 | '-.inf' => -INF, |
| 322 | 352 | '.nan' => NAN |
| 323 | 353 | ]; |
| 324 | - if (isset($types[strtolower($v)])) return $types[strtolower($v)]; |
|
| 325 | - if (R::isDate($v)) return date_create($v); |
|
| 326 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 354 | + if (isset($types[strtolower($v)])) { |
|
| 355 | + return $types[strtolower($v)]; |
|
| 356 | + } |
|
| 357 | + if (R::isDate($v)) { |
|
| 358 | + return date_create($v); |
|
| 359 | + } |
|
| 360 | + if (R::isNumber($v)) { |
|
| 361 | + return self::getNumber($v); |
|
| 362 | + } |
|
| 327 | 363 | return strval($v); |
| 328 | 364 | } |
| 329 | 365 | |
@@ -336,8 +372,12 @@ discard block |
||
| 336 | 372 | */ |
| 337 | 373 | private static function getNumber(string $v) |
| 338 | 374 | { |
| 339 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
| 340 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
| 375 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
| 376 | + return intval(base_convert($v, 8, 10)); |
|
| 377 | + } |
|
| 378 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
| 379 | + return intval(base_convert($v, 16, 10)); |
|
| 380 | + } |
|
| 341 | 381 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
| 342 | 382 | } |
| 343 | 383 | |
@@ -6,14 +6,14 @@ |
||
| 6 | 6 | /** |
| 7 | 7 | * Display some use cases for Yaml library |
| 8 | 8 | */ |
| 9 | -const JSON_OPTIONS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_LINE_TERMINATORS | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_PARTIAL_OUTPUT_ON_ERROR; |
|
| 9 | +const JSON_OPTIONS = JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_LINE_TERMINATORS|JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION|JSON_PARTIAL_OUTPUT_ON_ERROR; |
|
| 10 | 10 | |
| 11 | 11 | /* USE CASE 1 |
| 12 | 12 | * load and parse if file exists |
| 13 | 13 | */ |
| 14 | 14 | ini_set("auto_detect_line_endings", 1); |
| 15 | 15 | // $yaml = Y::parseFile('./tests/cases/parsing/blockChompingWithInsideBlank.yml', null, 0); //->parse(); |
| 16 | -$content = file_get_contents('./tests/cases/parsing/complex_mapping_in_item.yml');//var_dump($content); |
|
| 16 | +$content = file_get_contents('./tests/cases/parsing/complex_mapping_in_item.yml'); //var_dump($content); |
|
| 17 | 17 | $yaml = Y::parse($content, null, 2); //->parse(); |
| 18 | 18 | // $yaml = Y::parseFile('./references/Example 2.27.yml', null, 1); |
| 19 | 19 | var_dump($yaml); |