@@ -77,7 +77,9 @@ discard block |
||
| 77 | 77 | { |
| 78 | 78 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
| 79 | 79 | //TODO : be more permissive on $strContent values |
| 80 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 80 | + if (!is_array($source) || !count($source)) { |
|
| 81 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 82 | + } |
|
| 81 | 83 | return function () use($source) { |
| 82 | 84 | foreach ($source as $key => $value) { |
| 83 | 85 | yield ++$key => $value; |
@@ -105,7 +107,9 @@ discard block |
||
| 105 | 107 | $n = new Node($lineString, $lineNb); |
| 106 | 108 | $deepest = $previous->getDeepestNode(); |
| 107 | 109 | if ($n->type & (Y::LITTERALS|Y::BLANK|Y::COMMENT|Y::TAG) || $deepest->type & Y::PARTIAL) { |
| 108 | - if ($this->onSpecialType($n, $previous, $emptyLines, $lineString)) continue; |
|
| 110 | + if ($this->onSpecialType($n, $previous, $emptyLines, $lineString)) { |
|
| 111 | + continue; |
|
| 112 | + } |
|
| 109 | 113 | } |
| 110 | 114 | //Note: 6.6 comments: Note that outside scalar content, a line containing only white space characters is taken to be a comment line. |
| 111 | 115 | foreach ($emptyLines as $blankNode) { |
@@ -134,7 +138,9 @@ discard block |
||
| 134 | 138 | } |
| 135 | 139 | } |
| 136 | 140 | } |
| 137 | - if ($this->onContextType($n, $target, $lineString)) continue; |
|
| 141 | + if ($this->onContextType($n, $target, $lineString)) { |
|
| 142 | + continue; |
|
| 143 | + } |
|
| 138 | 144 | $target->add($n); |
| 139 | 145 | $previous = $n; |
| 140 | 146 | } |
@@ -171,8 +177,12 @@ discard block |
||
| 171 | 177 | return true; |
| 172 | 178 | } |
| 173 | 179 | if ($n->type & Y::BLANK) { |
| 174 | - if ($previous->type & Y::SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 175 | - if ($deepest->type & Y::LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
| 180 | + if ($previous->type & Y::SCALAR) { |
|
| 181 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 182 | + } |
|
| 183 | + if ($deepest->type & Y::LITTERALS) { |
|
| 184 | + $emptyLines[] = $n->setParent($deepest); |
|
| 185 | + } |
|
| 176 | 186 | return true; |
| 177 | 187 | } elseif ($n->type & Y::COMMENT |
| 178 | 188 | && !($previous->getParent()->value->type & Y::LITTERALS) |
@@ -71,7 +71,9 @@ discard block |
||
| 71 | 71 | if ($this->type === Y::ROOT) { |
| 72 | 72 | return $this; |
| 73 | 73 | } |
| 74 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
| 74 | + if (!is_int($indent)) { |
|
| 75 | + return $this->parent ?? $this; |
|
| 76 | + } |
|
| 75 | 77 | $cursor = $this; |
| 76 | 78 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
| 77 | 79 | if ($cursor->indent === $indent && $cursor->type !== $type) { |
@@ -103,7 +105,7 @@ discard block |
||
| 103 | 105 | if (is_null($current)) { |
| 104 | 106 | $this->value = $child; |
| 105 | 107 | return; |
| 106 | - }elseif ($current instanceof Node) { |
|
| 108 | + } elseif ($current instanceof Node) { |
|
| 107 | 109 | $this->value = new NodeList(); |
| 108 | 110 | if ($current->type & Y::LITTERALS) { |
| 109 | 111 | $this->value->type = $current->type; |
@@ -113,9 +115,15 @@ discard block |
||
| 113 | 115 | } |
| 114 | 116 | //modify type according to child |
| 115 | 117 | if (is_null($this->value->type)) { |
| 116 | - if ($child->type & Y::SET_KEY) $this->value->type = Y::SET; |
|
| 117 | - if ($child->type & Y::KEY) $this->value->type = Y::MAPPING; |
|
| 118 | - if ($child->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
|
| 118 | + if ($child->type & Y::SET_KEY) { |
|
| 119 | + $this->value->type = Y::SET; |
|
| 120 | + } |
|
| 121 | + if ($child->type & Y::KEY) { |
|
| 122 | + $this->value->type = Y::MAPPING; |
|
| 123 | + } |
|
| 124 | + if ($child->type & Y::ITEM) { |
|
| 125 | + $this->value->type = Y::SEQUENCE; |
|
| 126 | + } |
|
| 119 | 127 | } |
| 120 | 128 | $this->value->push($child); |
| 121 | 129 | } |
@@ -40,10 +40,14 @@ discard block |
||
| 40 | 40 | } |
| 41 | 41 | $_root->value instanceof NodeList && $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 42 | 42 | foreach ($_root->value as $child) { |
| 43 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 43 | + if ($child->type & Y::DOC_START) { |
|
| 44 | + $totalDocStart++; |
|
| 45 | + } |
|
| 44 | 46 | //if 0 or 1 DOC_START = we are still in first document |
| 45 | 47 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 46 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 48 | + if (!isset($documents[$currentDoc])) { |
|
| 49 | + $documents[$currentDoc] = new NodeList(); |
|
| 50 | + } |
|
| 47 | 51 | $documents[$currentDoc]->push($child); |
| 48 | 52 | } |
| 49 | 53 | $content = []; |
@@ -68,7 +72,9 @@ discard block |
||
| 68 | 72 | */ |
| 69 | 73 | private static function build(object $node, &$parent = null) |
| 70 | 74 | { |
| 71 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
| 75 | + if ($node instanceof NodeList) { |
|
| 76 | + return self::buildNodeList($node, $parent); |
|
| 77 | + } |
|
| 72 | 78 | return self::buildNode($node, $parent); |
| 73 | 79 | } |
| 74 | 80 | |
@@ -131,13 +137,17 @@ discard block |
||
| 131 | 137 | } else { |
| 132 | 138 | $tmp = Node2PHP::get($node); |
| 133 | 139 | } |
| 134 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 140 | + if ($type === Y::REF_DEF) { |
|
| 141 | + self::$_root->addReference($identifier, $tmp); |
|
| 142 | + } |
|
| 135 | 143 | return self::$_root->getReference($identifier); |
| 136 | 144 | } |
| 137 | 145 | if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
| 138 | 146 | return self::buildNodeList($node->value, $parent); |
| 139 | 147 | } |
| 140 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
| 148 | + if ($type & Y::COMMENT) { |
|
| 149 | + self::$_root->addComment($node->line, $node->value); |
|
| 150 | + } |
|
| 141 | 151 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
| 142 | 152 | Y::ITEM => 'buildItem', |
| 143 | 153 | Y::KEY => 'buildKey', |
@@ -250,18 +260,24 @@ discard block |
||
| 250 | 260 | } |
| 251 | 261 | $result = ''; |
| 252 | 262 | $separator = ''; |
| 253 | - if ($type & Y::LITT) $separator = "\n"; |
|
| 254 | - if ($type & Y::LITT_FOLDED) $separator = ' '; |
|
| 263 | + if ($type & Y::LITT) { |
|
| 264 | + $separator = "\n"; |
|
| 265 | + } |
|
| 266 | + if ($type & Y::LITT_FOLDED) { |
|
| 267 | + $separator = ' '; |
|
| 268 | + } |
|
| 255 | 269 | foreach ($list as $child) { |
| 256 | 270 | if ($child->value instanceof NodeList) { |
| 257 | 271 | $result .= self::buildLitteral($child->value, $type).$separator; |
| 258 | 272 | } else { |
| 259 | 273 | $val = $child->type & (Y::SCALAR|Y::BLANK) ? $child->value : substr($child->raw, $refIndent); |
| 260 | 274 | if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
| 261 | - if ($result[-1] === $separator) |
|
| 262 | - $result[-1] = "\n"; |
|
| 263 | - if ($result[-1] === "\n") |
|
| 264 | - $result .= $val; |
|
| 275 | + if ($result[-1] === $separator) { |
|
| 276 | + $result[-1] = "\n"; |
|
| 277 | + } |
|
| 278 | + if ($result[-1] === "\n") { |
|
| 279 | + $result .= $val; |
|
| 280 | + } |
|
| 265 | 281 | continue; |
| 266 | 282 | } |
| 267 | 283 | $result .= $val.$separator; |
@@ -283,7 +299,9 @@ discard block |
||
| 283 | 299 | $built = is_object($node->value) ? self::build($node->value) : null; |
| 284 | 300 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
| 285 | 301 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 286 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 302 | + if (empty($key)) { |
|
| 303 | + throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 304 | + } |
|
| 287 | 305 | $parent->{trim($key, '\'" ')} = null; |
| 288 | 306 | } |
| 289 | 307 | |
@@ -30,8 +30,12 @@ |
||
| 30 | 30 | public function jsonSerialize():array |
| 31 | 31 | { |
| 32 | 32 | $prop = get_object_vars($this); |
| 33 | - if (count($prop) > 0) return $prop; |
|
| 34 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 33 | + if (count($prop) > 0) { |
|
| 34 | + return $prop; |
|
| 35 | + } |
|
| 36 | + if (count($this) > 0) { |
|
| 37 | + return iterator_to_array($this); |
|
| 38 | + } |
|
| 35 | 39 | return $this;//new \Stdclass; |
| 36 | 40 | } |
| 37 | 41 | |
@@ -23,9 +23,15 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | public static function get(Node $n) |
| 25 | 25 | { |
| 26 | - if (is_null($n->value)) return null; |
|
| 27 | - if ($n->type & (Y::REF_CALL|Y::SCALAR)) return self::getScalar((string) $n->value); |
|
| 28 | - if ($n->type & Y::JSON) return $n->value; |
|
| 26 | + if (is_null($n->value)) { |
|
| 27 | + return null; |
|
| 28 | + } |
|
| 29 | + if ($n->type & (Y::REF_CALL|Y::SCALAR)) { |
|
| 30 | + return self::getScalar((string) $n->value); |
|
| 31 | + } |
|
| 32 | + if ($n->type & Y::JSON) { |
|
| 33 | + return $n->value; |
|
| 34 | + } |
|
| 29 | 35 | $expected = [Y::QUOTED => trim((string) $n->value, "\"'"), |
| 30 | 36 | Y::RAW => strval((string) $n->value)]; |
| 31 | 37 | return $expected[$n->type] ?? null; |
@@ -41,8 +47,12 @@ discard block |
||
| 41 | 47 | */ |
| 42 | 48 | private static function getScalar(string $v) |
| 43 | 49 | { |
| 44 | - if (R::isDate($v)) return self::$dateAsString ? $v : date_create($v); |
|
| 45 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 50 | + if (R::isDate($v)) { |
|
| 51 | + return self::$dateAsString ? $v : date_create($v); |
|
| 52 | + } |
|
| 53 | + if (R::isNumber($v)) { |
|
| 54 | + return self::getNumber($v); |
|
| 55 | + } |
|
| 46 | 56 | $types = ['yes' => true, |
| 47 | 57 | 'no' => false, |
| 48 | 58 | 'true' => true, |
@@ -64,8 +74,12 @@ discard block |
||
| 64 | 74 | */ |
| 65 | 75 | private static function getNumber(string $v) |
| 66 | 76 | { |
| 67 | - if (preg_match(R::OCTAL_NUM, $v)) return intval(base_convert($v, 8, 10)); |
|
| 68 | - if (preg_match(R::HEX_NUM, $v)) return intval(base_convert($v, 16, 10)); |
|
| 77 | + if (preg_match(R::OCTAL_NUM, $v)) { |
|
| 78 | + return intval(base_convert($v, 8, 10)); |
|
| 79 | + } |
|
| 80 | + if (preg_match(R::HEX_NUM, $v)) { |
|
| 81 | + return intval(base_convert($v, 16, 10)); |
|
| 82 | + } |
|
| 69 | 83 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
| 70 | 84 | } |
| 71 | 85 | } |
| 72 | 86 | \ No newline at end of file |