@@ -68,8 +68,12 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function getParent(int $indent = null, $type = 0):Node |
| 70 | 70 | { |
| 71 | - if ($this->type === Y::ROOT) return $this; |
|
| 72 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
| 71 | + if ($this->type === Y::ROOT) { |
|
| 72 | + return $this; |
|
| 73 | + } |
|
| 74 | + if (!is_int($indent)) { |
|
| 75 | + return $this->parent ?? $this; |
|
| 76 | + } |
|
| 73 | 77 | $cursor = $this; |
| 74 | 78 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
| 75 | 79 | if ($cursor->indent === $indent && $cursor->type !== $type) { |
@@ -100,7 +104,7 @@ discard block |
||
| 100 | 104 | if (is_null($this->value)) { |
| 101 | 105 | $this->value = $child; |
| 102 | 106 | return; |
| 103 | - }elseif ($this->value instanceof Node) { |
|
| 107 | + } elseif ($this->value instanceof Node) { |
|
| 104 | 108 | if ($this->value->type & Y::LITTERALS) { |
| 105 | 109 | $type = $this->value->type; |
| 106 | 110 | $this->value = new NodeList(); |
@@ -176,12 +180,17 @@ discard block |
||
| 176 | 180 | { |
| 177 | 181 | $v = ltrim(substr($nodeValue, 1)); |
| 178 | 182 | $first = $nodeValue[0]; |
| 179 | - if ($first === "-") $this->onHyphen($nodeValue); |
|
| 180 | - elseif (in_array($first, ['"', "'"])) $this->onQuoted($nodeValue); |
|
| 181 | - elseif (in_array($first, ['{', '['])) $this->onCompact($nodeValue); |
|
| 182 | - elseif (in_array($first, ['?', ':'])) $this->onSetElement($nodeValue); |
|
| 183 | - elseif (in_array($first, ['!', '&', '*'])) $this->onNodeAction($nodeValue); |
|
| 184 | - else { |
|
| 183 | + if ($first === "-") { |
|
| 184 | + $this->onHyphen($nodeValue); |
|
| 185 | + } elseif (in_array($first, ['"', "'"])) { |
|
| 186 | + $this->onQuoted($nodeValue); |
|
| 187 | + } elseif (in_array($first, ['{', '['])) { |
|
| 188 | + $this->onCompact($nodeValue); |
|
| 189 | + } elseif (in_array($first, ['?', ':'])) { |
|
| 190 | + $this->onSetElement($nodeValue); |
|
| 191 | + } elseif (in_array($first, ['!', '&', '*'])) { |
|
| 192 | + $this->onNodeAction($nodeValue); |
|
| 193 | + } else { |
|
| 185 | 194 | $characters = [ '#' => [Y::COMMENT, $v], |
| 186 | 195 | '%' => [Y::DIRECTIVE, $v], |
| 187 | 196 | '>' => [Y::LITT_FOLDED, null], |
@@ -75,13 +75,13 @@ discard block |
||
| 75 | 75 | * |
| 76 | 76 | * @return mixed The parent (object|array) or a string representing the NodeList. |
| 77 | 77 | */ |
| 78 | - private static function buildNodeList(NodeList $node, &$parent=null) |
|
| 78 | + private static function buildNodeList(NodeList $node, &$parent = null) |
|
| 79 | 79 | { |
| 80 | 80 | $node->forceType(); |
| 81 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
| 81 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
| 82 | 82 | return self::buildLitteral($node, (int) $node->type); |
| 83 | 83 | } |
| 84 | - $action = function ($child, &$parent, &$out) { |
|
| 84 | + $action = function($child, &$parent, &$out) { |
|
| 85 | 85 | self::build($child, $out); |
| 86 | 86 | }; |
| 87 | 87 | if ($node->type & (Y::COMPACT_MAPPING|Y::MAPPING|Y::SET)) { |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $out = $parent ?? []; |
| 91 | 91 | } else { |
| 92 | 92 | $out = ''; |
| 93 | - $action = function ($child, &$parent, &$out) { |
|
| 93 | + $action = function($child, &$parent, &$out) { |
|
| 94 | 94 | if ($child->type & (Y::SCALAR|Y::QUOTED)) { |
| 95 | 95 | if ($parent) { |
| 96 | 96 | $parent->setText(self::build($child)); |
@@ -129,11 +129,11 @@ discard block |
||
| 129 | 129 | ]; |
| 130 | 130 | if (isset($actions[$type])) { |
| 131 | 131 | return self::{$actions[$type]}($node, $parent); |
| 132 | - } elseif ($type & Y::COMMENT) { |
|
| 132 | + } elseif ($type&Y::COMMENT) { |
|
| 133 | 133 | self::$_root->addComment($line, $value); |
| 134 | - } elseif ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
| 134 | + } elseif ($type&(Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
| 135 | 135 | return self::buildNodeList($value, $parent); |
| 136 | - } elseif ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
| 136 | + } elseif ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
| 137 | 137 | return self::handleReference($node, $parent); |
| 138 | 138 | } elseif ($value instanceof Node) { |
| 139 | 139 | return self::buildNode($value, $parent); |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
| 160 | 160 | * @return null |
| 161 | 161 | */ |
| 162 | - private static function buildKey(Node $node, &$parent=null) |
|
| 162 | + private static function buildKey(Node $node, &$parent = null) |
|
| 163 | 163 | { |
| 164 | 164 | extract((array) $node, EXTR_REFS); |
| 165 | 165 | if (is_null($identifier)) { |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | $numKeys = array_filter(array_keys($ref), 'is_int'); |
| 207 | 207 | $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0; |
| 208 | 208 | if ($value instanceof Node) { |
| 209 | - if($value->type & Y::KEY) { |
|
| 209 | + if ($value->type & Y::KEY) { |
|
| 210 | 210 | self::buildKey($node->value, $parent); |
| 211 | 211 | return; |
| 212 | 212 | } elseif ($value->type & Y::ITEM) { |
@@ -235,13 +235,13 @@ discard block |
||
| 235 | 235 | //remove trailing blank |
| 236 | 236 | while ($list->top()->type & Y::BLANK) $list->pop(); |
| 237 | 237 | $result = ''; |
| 238 | - $separator = [ Y::RAW => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][$type]; |
|
| 238 | + $separator = [Y::RAW => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][$type]; |
|
| 239 | 239 | foreach ($list as $child) { |
| 240 | 240 | if ($child->value instanceof NodeList) { |
| 241 | 241 | $result .= self::buildLitteral($child->value, $type).$separator; |
| 242 | 242 | } else { |
| 243 | 243 | $val = $child->type & (Y::SCALAR|Y::BLANK) ? $child->value : substr($child->raw, $refIndent); |
| 244 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 244 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 245 | 245 | if ($result[-1] === $separator) |
| 246 | 246 | $result[-1] = "\n"; |
| 247 | 247 | if ($result[-1] === "\n") |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | private function buildSetKey(Node $node, &$parent) |
| 266 | 266 | { |
| 267 | 267 | $built = is_object($node->value) ? self::build($node->value) : null; |
| 268 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
| 268 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
| 269 | 269 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 270 | 270 | if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
| 271 | 271 | $parent->{trim($key, '\'" ')} = null; |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | { |
| 282 | 282 | $prop = array_keys(get_object_vars($parent)); |
| 283 | 283 | $key = end($prop); |
| 284 | - if ($node->value->type & (Y::ITEM|Y::KEY )) { |
|
| 284 | + if ($node->value->type & (Y::ITEM|Y::KEY)) { |
|
| 285 | 285 | $node->value = new NodeList($node->value); |
| 286 | 286 | } |
| 287 | 287 | $parent->{$key} = self::build($node->value); |
@@ -35,10 +35,14 @@ discard block |
||
| 35 | 35 | $documents = []; |
| 36 | 36 | $_root->value instanceof NodeList && $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 37 | 37 | foreach ($_root->value as $child) { |
| 38 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 38 | + if ($child->type & Y::DOC_START) { |
|
| 39 | + $totalDocStart++; |
|
| 40 | + } |
|
| 39 | 41 | //if 0 or 1 DOC_START = we are still in first document |
| 40 | 42 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 41 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 43 | + if (!isset($documents[$currentDoc])) { |
|
| 44 | + $documents[$currentDoc] = new NodeList(); |
|
| 45 | + } |
|
| 42 | 46 | $documents[$currentDoc]->push($child); |
| 43 | 47 | } |
| 44 | 48 | $content = []; |
@@ -63,7 +67,9 @@ discard block |
||
| 63 | 67 | */ |
| 64 | 68 | private static function build(object $node, &$parent = null) |
| 65 | 69 | { |
| 66 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
| 70 | + if ($node instanceof NodeList) { |
|
| 71 | + return self::buildNodeList($node, $parent); |
|
| 72 | + } |
|
| 67 | 73 | return self::buildNode($node, $parent); |
| 68 | 74 | } |
| 69 | 75 | |
@@ -145,7 +151,9 @@ discard block |
||
| 145 | 151 | private static function handleReference($node, $parent) |
| 146 | 152 | { |
| 147 | 153 | $tmp = is_null($node->value) ? null : self::build($node->value, $parent); |
| 148 | - if ($node->type === Y::REF_DEF) self::$_root->addReference($node->identifier, $tmp); |
|
| 154 | + if ($node->type === Y::REF_DEF) { |
|
| 155 | + self::$_root->addReference($node->identifier, $tmp); |
|
| 156 | + } |
|
| 149 | 157 | return self::$_root->getReference($node->identifier); |
| 150 | 158 | } |
| 151 | 159 | |
@@ -233,7 +241,9 @@ discard block |
||
| 233 | 241 | $list->rewind(); |
| 234 | 242 | $refIndent = $list->current()->indent; |
| 235 | 243 | //remove trailing blank |
| 236 | - while ($list->top()->type & Y::BLANK) $list->pop(); |
|
| 244 | + while ($list->top()->type & Y::BLANK) { |
|
| 245 | + $list->pop(); |
|
| 246 | + } |
|
| 237 | 247 | $result = ''; |
| 238 | 248 | $separator = [ Y::RAW => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][$type]; |
| 239 | 249 | foreach ($list as $child) { |
@@ -242,10 +252,12 @@ discard block |
||
| 242 | 252 | } else { |
| 243 | 253 | $val = $child->type & (Y::SCALAR|Y::BLANK) ? $child->value : substr($child->raw, $refIndent); |
| 244 | 254 | if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
| 245 | - if ($result[-1] === $separator) |
|
| 246 | - $result[-1] = "\n"; |
|
| 247 | - if ($result[-1] === "\n") |
|
| 248 | - $result .= $val; |
|
| 255 | + if ($result[-1] === $separator) { |
|
| 256 | + $result[-1] = "\n"; |
|
| 257 | + } |
|
| 258 | + if ($result[-1] === "\n") { |
|
| 259 | + $result .= $val; |
|
| 260 | + } |
|
| 249 | 261 | continue; |
| 250 | 262 | } |
| 251 | 263 | $result .= $val.$separator; |
@@ -267,7 +279,9 @@ discard block |
||
| 267 | 279 | $built = is_object($node->value) ? self::build($node->value) : null; |
| 268 | 280 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
| 269 | 281 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 270 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 282 | + if (empty($key)) { |
|
| 283 | + throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 284 | + } |
|
| 271 | 285 | $parent->{trim($key, '\'" ')} = null; |
| 272 | 286 | } |
| 273 | 287 | |