@@ -6,13 +6,13 @@ |
||
| 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 | -$content = file_get_contents('./tests/cases/parsing/multidoc_sequence.yml');//var_dump($content); |
|
| 15 | +$content = file_get_contents('./tests/cases/parsing/multidoc_sequence.yml'); //var_dump($content); |
|
| 16 | 16 | // $content = file_get_contents('./tests/cases/examples/Example_2_17.yml');//var_dump($content); |
| 17 | 17 | $yaml = Y::parse($content, null, 3); |
| 18 | 18 | // $yaml = Y::parseFile('./references/Example 2.27.yml', null, 1); |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 38 | 38 | foreach ($_root->value as $child) { |
| 39 | 39 | if ($child->type & Y::DOC_START) { |
| 40 | - if(++$totalDocStart > 1){ |
|
| 40 | + if (++$totalDocStart > 1) { |
|
| 41 | 41 | $documents[] = self::buildDocument($buffer, count($documents)); |
| 42 | 42 | $buffer = new NodeList($child); |
| 43 | 43 | } |
@@ -82,13 +82,13 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @return mixed The parent (object|array) or a string representing the NodeList. |
| 84 | 84 | */ |
| 85 | - private static function buildNodeList(NodeList $node, &$parent=null) |
|
| 85 | + private static function buildNodeList(NodeList $node, &$parent = null) |
|
| 86 | 86 | { |
| 87 | 87 | $node->forceType(); |
| 88 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
| 88 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
| 89 | 89 | return self::buildLitteral($node, (int) $node->type); |
| 90 | 90 | } |
| 91 | - $action = function ($child, &$parent, &$out) { |
|
| 91 | + $action = function($child, &$parent, &$out) { |
|
| 92 | 92 | self::build($child, $out); |
| 93 | 93 | }; |
| 94 | 94 | if ($node->type & (Y::COMPACT_MAPPING|Y::MAPPING|Y::SET)) { |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | $out = $parent ?? []; |
| 98 | 98 | } else { |
| 99 | 99 | $out = ''; |
| 100 | - $action = function ($child, &$parent, &$out) { |
|
| 100 | + $action = function($child, &$parent, &$out) { |
|
| 101 | 101 | if ($child->type & (Y::SCALAR|Y::QUOTED)) { |
| 102 | 102 | if ($parent) { |
| 103 | 103 | $parent->setText(self::build($child)); |
@@ -136,11 +136,11 @@ discard block |
||
| 136 | 136 | ]; |
| 137 | 137 | if (isset($actions[$type])) { |
| 138 | 138 | return self::{$actions[$type]}($node, $parent); |
| 139 | - } elseif ($type & Y::COMMENT) { |
|
| 139 | + } elseif ($type&Y::COMMENT) { |
|
| 140 | 140 | self::$_root->addComment($line, $value); |
| 141 | - } elseif ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
| 141 | + } elseif ($type&(Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
| 142 | 142 | return self::buildNodeList($value, $parent); |
| 143 | - } elseif ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
| 143 | + } elseif ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
| 144 | 144 | return self::handleReference($node, $parent); |
| 145 | 145 | } elseif ($value instanceof Node) { |
| 146 | 146 | return self::buildNode($value, $parent); |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
| 167 | 167 | * @return null |
| 168 | 168 | */ |
| 169 | - private static function buildKey(Node $node, &$parent=null) |
|
| 169 | + private static function buildKey(Node $node, &$parent = null) |
|
| 170 | 170 | { |
| 171 | 171 | extract((array) $node, EXTR_REFS); |
| 172 | 172 | if (is_null($identifier)) { |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | $numKeys = array_filter(array_keys($ref), 'is_int'); |
| 214 | 214 | $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0; |
| 215 | 215 | if ($value instanceof Node) { |
| 216 | - if($value->type & Y::KEY) { |
|
| 216 | + if ($value->type & Y::KEY) { |
|
| 217 | 217 | self::buildKey($node->value, $parent); |
| 218 | 218 | return; |
| 219 | 219 | } elseif ($value->type & Y::ITEM) { |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | //remove trailing blank |
| 243 | 243 | while ($list->top()->type & Y::BLANK) $list->pop(); |
| 244 | 244 | $result = ''; |
| 245 | - $separator = [ Y::RAW => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][$type]; |
|
| 245 | + $separator = [Y::RAW => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][$type]; |
|
| 246 | 246 | foreach ($list as $child) { |
| 247 | 247 | if ($child->value instanceof NodeList) { |
| 248 | 248 | $result .= self::buildLitteral($child->value, $type).$separator; |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | private function setLiteralValue(Node $child, string &$result, int $refIndent, string $separator, int $type) |
| 257 | 257 | { |
| 258 | 258 | $val = $child->type & (Y::SCALAR) ? $child->value : substr($child->raw, $refIndent); |
| 259 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 259 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 260 | 260 | if ($result[-1] === $separator) |
| 261 | 261 | $result[-1] = "\n"; |
| 262 | 262 | if ($result[-1] === "\n") |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | private function buildSetKey(Node $node, &$parent) |
| 278 | 278 | { |
| 279 | 279 | $built = is_object($node->value) ? self::build($node->value) : null; |
| 280 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
| 280 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
| 281 | 281 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 282 | 282 | if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
| 283 | 283 | $parent->{trim($key, '\'" ')} = null; |
@@ -293,7 +293,7 @@ discard block |
||
| 293 | 293 | { |
| 294 | 294 | $prop = array_keys(get_object_vars($parent)); |
| 295 | 295 | $key = end($prop); |
| 296 | - if ($node->value->type & (Y::ITEM|Y::KEY )) { |
|
| 296 | + if ($node->value->type & (Y::ITEM|Y::KEY)) { |
|
| 297 | 297 | $node->value = new NodeList($node->value); |
| 298 | 298 | } |
| 299 | 299 | $parent->{$key} = self::build($node->value); |
@@ -70,7 +70,9 @@ discard block |
||
| 70 | 70 | */ |
| 71 | 71 | private static function build(object $node, &$parent = null) |
| 72 | 72 | { |
| 73 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
| 73 | + if ($node instanceof NodeList) { |
|
| 74 | + return self::buildNodeList($node, $parent); |
|
| 75 | + } |
|
| 74 | 76 | return self::buildNode($node, $parent); |
| 75 | 77 | } |
| 76 | 78 | |
@@ -152,7 +154,9 @@ discard block |
||
| 152 | 154 | private static function handleReference($node, $parent) |
| 153 | 155 | { |
| 154 | 156 | $tmp = is_null($node->value) ? null : self::build($node->value, $parent); |
| 155 | - if ($node->type === Y::REF_DEF) self::$_root->addReference($node->identifier, $tmp); |
|
| 157 | + if ($node->type === Y::REF_DEF) { |
|
| 158 | + self::$_root->addReference($node->identifier, $tmp); |
|
| 159 | + } |
|
| 156 | 160 | return self::$_root->getReference($node->identifier); |
| 157 | 161 | } |
| 158 | 162 | |
@@ -240,7 +244,9 @@ discard block |
||
| 240 | 244 | $list->rewind(); |
| 241 | 245 | $refIndent = $list->current()->indent; |
| 242 | 246 | //remove trailing blank |
| 243 | - while ($list->top()->type & Y::BLANK) $list->pop(); |
|
| 247 | + while ($list->top()->type & Y::BLANK) { |
|
| 248 | + $list->pop(); |
|
| 249 | + } |
|
| 244 | 250 | $result = ''; |
| 245 | 251 | $separator = [ Y::RAW => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][$type]; |
| 246 | 252 | foreach ($list as $child) { |
@@ -257,10 +263,12 @@ discard block |
||
| 257 | 263 | { |
| 258 | 264 | $val = $child->type & (Y::SCALAR) ? $child->value : substr($child->raw, $refIndent); |
| 259 | 265 | if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
| 260 | - if ($result[-1] === $separator) |
|
| 261 | - $result[-1] = "\n"; |
|
| 262 | - if ($result[-1] === "\n") |
|
| 263 | - $result .= $val; |
|
| 266 | + if ($result[-1] === $separator) { |
|
| 267 | + $result[-1] = "\n"; |
|
| 268 | + } |
|
| 269 | + if ($result[-1] === "\n") { |
|
| 270 | + $result .= $val; |
|
| 271 | + } |
|
| 264 | 272 | return; |
| 265 | 273 | } |
| 266 | 274 | $result .= $val.$separator; |
@@ -279,7 +287,9 @@ discard block |
||
| 279 | 287 | $built = is_object($node->value) ? self::build($node->value) : null; |
| 280 | 288 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
| 281 | 289 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 282 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 290 | + if (empty($key)) { |
|
| 291 | + throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 292 | + } |
|
| 283 | 293 | $parent->{trim($key, '\'" ')} = null; |
| 284 | 294 | } |
| 285 | 295 | |