@@ -40,8 +40,12 @@ |
||
| 40 | 40 | { |
| 41 | 41 | $prop = get_object_vars($this); |
| 42 | 42 | unset($prop["__yaml__object__api"]); |
| 43 | - if (count($prop) > 0) return $prop; |
|
| 44 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 43 | + if (count($prop) > 0) { |
|
| 44 | + return $prop; |
|
| 45 | + } |
|
| 46 | + if (count($this) > 0) { |
|
| 47 | + return iterator_to_array($this); |
|
| 48 | + } |
|
| 45 | 49 | return $this->__yaml__object__api->value; |
| 46 | 50 | } |
| 47 | 51 | } |
@@ -27,8 +27,12 @@ |
||
| 27 | 27 | public function jsonSerialize() |
| 28 | 28 | { |
| 29 | 29 | $prop = get_object_vars($this); |
| 30 | - if (count($prop) > 0) return $prop; |
|
| 31 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 30 | + if (count($prop) > 0) { |
|
| 31 | + return $prop; |
|
| 32 | + } |
|
| 33 | + if (count($this) > 0) { |
|
| 34 | + return iterator_to_array($this); |
|
| 35 | + } |
|
| 32 | 36 | } |
| 33 | 37 | |
| 34 | 38 | public static function wrap($arrayOrObject) |
@@ -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 | } |
@@ -59,7 +59,9 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function getParent(int $indent = null):Node |
| 61 | 61 | { |
| 62 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
| 62 | + if (!is_int($indent)) { |
|
| 63 | + return $this->parent ?? $this; |
|
| 64 | + } |
|
| 63 | 65 | $cursor = $this; |
| 64 | 66 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
| 65 | 67 | $cursor = $cursor->parent; |
@@ -88,9 +90,15 @@ discard block |
||
| 88 | 90 | } |
| 89 | 91 | $this->value->push($child); |
| 90 | 92 | //modify type according to child |
| 91 | - if ($child->type & (Y::COMMENT | Y::KEY)) $this->value->type = Y::MAPPING; |
|
| 92 | - if ($child->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
|
| 93 | - if ($this->type & Y::LITTERALS) $this->value->type = $this->type; |
|
| 93 | + if ($child->type & (Y::COMMENT | Y::KEY)) { |
|
| 94 | + $this->value->type = Y::MAPPING; |
|
| 95 | + } |
|
| 96 | + if ($child->type & Y::ITEM) { |
|
| 97 | + $this->value->type = Y::SEQUENCE; |
|
| 98 | + } |
|
| 99 | + if ($this->type & Y::LITTERALS) { |
|
| 100 | + $this->value->type = $this->type; |
|
| 101 | + } |
|
| 94 | 102 | } |
| 95 | 103 | } |
| 96 | 104 | |
@@ -143,10 +151,16 @@ discard block |
||
| 143 | 151 | $type = R::isProperlyQuoted($nodeValue) ? Y::QUOTED : Y::PARTIAL; |
| 144 | 152 | return [$type, $nodeValue]; |
| 145 | 153 | } |
| 146 | - if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
|
| 147 | - if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
|
| 154 | + if (in_array($first, ['{', '['])) { |
|
| 155 | + return $this->onObject($nodeValue); |
|
| 156 | + } |
|
| 157 | + if (in_array($first, ['!', '&', '*'])) { |
|
| 158 | + return $this->onNodeAction($nodeValue); |
|
| 159 | + } |
|
| 148 | 160 | // Note : php don't like '?' as an array key -_- |
| 149 | - if($first === '?') return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 161 | + if($first === '?') { |
|
| 162 | + return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 163 | + } |
|
| 150 | 164 | $characters = [ '#' => [Y::COMMENT, ltrim($v)], |
| 151 | 165 | "-" => $this->onHyphen($nodeValue), |
| 152 | 166 | '%' => [Y::DIRECTIVE, ltrim($v)], |
@@ -198,9 +212,15 @@ discard block |
||
| 198 | 212 | private function onObject($value):array |
| 199 | 213 | { |
| 200 | 214 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 201 | - if (json_last_error() === JSON_ERROR_NONE) return [Y::JSON, $value]; |
|
| 202 | - if (preg_match(R::MAPPING, $value)) return [Y::COMPACT_MAPPING, $value]; |
|
| 203 | - if (preg_match(R::SEQUENCE, $value)) return [Y::COMPACT_SEQUENCE, $value]; |
|
| 215 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
| 216 | + return [Y::JSON, $value]; |
|
| 217 | + } |
|
| 218 | + if (preg_match(R::MAPPING, $value)) { |
|
| 219 | + return [Y::COMPACT_MAPPING, $value]; |
|
| 220 | + } |
|
| 221 | + if (preg_match(R::SEQUENCE, $value)) { |
|
| 222 | + return [Y::COMPACT_SEQUENCE, $value]; |
|
| 223 | + } |
|
| 204 | 224 | return [Y::PARTIAL, $value]; |
| 205 | 225 | } |
| 206 | 226 | |
@@ -215,7 +235,9 @@ discard block |
||
| 215 | 235 | { |
| 216 | 236 | if (substr($nodeValue, 0, 3) === '---') { |
| 217 | 237 | $rest = trim(substr($nodeValue, 3)); |
| 218 | - if (empty($rest)) return [Y::DOC_START, null]; |
|
| 238 | + if (empty($rest)) { |
|
| 239 | + return [Y::DOC_START, null]; |
|
| 240 | + } |
|
| 219 | 241 | $n = new Node($rest, $this->line); |
| 220 | 242 | $n->indent = $this->indent + 4; |
| 221 | 243 | return [Y::DOC_START, $n->setParent($this)]; |
@@ -257,9 +279,15 @@ discard block |
||
| 257 | 279 | public function getPhpValue() |
| 258 | 280 | { |
| 259 | 281 | $v = &$this->value; |
| 260 | - if (is_null($v)) return null; |
|
| 261 | - if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
|
| 262 | - if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 282 | + if (is_null($v)) { |
|
| 283 | + return null; |
|
| 284 | + } |
|
| 285 | + if ($this->type & (Y::REF_CALL | Y::SCALAR)) { |
|
| 286 | + return self::getScalar($v); |
|
| 287 | + } |
|
| 288 | + if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) { |
|
| 289 | + return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 290 | + } |
|
| 263 | 291 | $expected = [Y::JSON => json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
| 264 | 292 | Y::QUOTED => substr($v, 1, -1), |
| 265 | 293 | Y::RAW => strval($v)]; |
@@ -288,9 +316,15 @@ discard block |
||
| 288 | 316 | '-.inf' => -INF, |
| 289 | 317 | '.nan' => NAN |
| 290 | 318 | ]; |
| 291 | - if (isset($types[strtolower($v)])) return $types[strtolower($v)]; |
|
| 292 | - if (R::isDate($v)) return date_create($v); |
|
| 293 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 319 | + if (isset($types[strtolower($v)])) { |
|
| 320 | + return $types[strtolower($v)]; |
|
| 321 | + } |
|
| 322 | + if (R::isDate($v)) { |
|
| 323 | + return date_create($v); |
|
| 324 | + } |
|
| 325 | + if (R::isNumber($v)) { |
|
| 326 | + return self::getNumber($v); |
|
| 327 | + } |
|
| 294 | 328 | return strval($v); |
| 295 | 329 | } |
| 296 | 330 | |
@@ -303,8 +337,12 @@ discard block |
||
| 303 | 337 | */ |
| 304 | 338 | private static function getNumber(string $v) |
| 305 | 339 | { |
| 306 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
| 307 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
| 340 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
| 341 | + return intval(base_convert($v, 8, 10)); |
|
| 342 | + } |
|
| 343 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
| 344 | + return intval(base_convert($v, 16, 10)); |
|
| 345 | + } |
|
| 308 | 346 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
| 309 | 347 | } |
| 310 | 348 | |
@@ -87,7 +87,9 @@ discard block |
||
| 87 | 87 | { |
| 88 | 88 | $source = $this->content ?? preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
| 89 | 89 | //TODO : be more permissive on $strContent values |
| 90 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 90 | + if (!is_array($source) || !count($source)) { |
|
| 91 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 92 | + } |
|
| 91 | 93 | $previous = $root = new Node(); |
| 92 | 94 | $emptyLines = []; |
| 93 | 95 | try { |
@@ -99,7 +101,9 @@ discard block |
||
| 99 | 101 | foreach ($gen() as $lineNb => $lineString) { |
| 100 | 102 | $n = new Node($lineString, $lineNb); |
| 101 | 103 | if ($n->type & (Y::LITTERALS|Y::BLANK)) { |
| 102 | - if ($this->onSpecialType($n, $previous, $emptyLines)) continue; |
|
| 104 | + if ($this->onSpecialType($n, $previous, $emptyLines)) { |
|
| 105 | + continue; |
|
| 106 | + } |
|
| 103 | 107 | } else { |
| 104 | 108 | foreach ($emptyLines as $blankNode) { |
| 105 | 109 | $blankNode->getParent()->add($blankNode); |
@@ -113,11 +117,15 @@ discard block |
||
| 113 | 117 | break; |
| 114 | 118 | default: $target = $previous->type & Y::SCALAR ? $previous->getParent() : $previous; |
| 115 | 119 | } |
| 116 | - if ($this->onContextType($n, $target, $lineString)) continue; |
|
| 120 | + if ($this->onContextType($n, $target, $lineString)) { |
|
| 121 | + continue; |
|
| 122 | + } |
|
| 117 | 123 | $target->add($n); |
| 118 | 124 | $previous = $n; |
| 119 | 125 | } |
| 120 | - if ($this->debug === 2) echo "\033[33mParsed Structure\033[0m\n",var_export($root, true); |
|
| 126 | + if ($this->debug === 2) { |
|
| 127 | + echo "\033[33mParsed Structure\033[0m\n",var_export($root, true); |
|
| 128 | + } |
|
| 121 | 129 | $out = Builder::buildContent($root, $this->debug); |
| 122 | 130 | return $out; |
| 123 | 131 | } catch (\Error|\Exception|\ParseError $e) { |
@@ -137,8 +145,12 @@ discard block |
||
| 137 | 145 | { |
| 138 | 146 | $deepest = $previous->getDeepestNode(); |
| 139 | 147 | if ($n->type & Y::BLANK) { |
| 140 | - if ($previous->type & Y::SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 141 | - if ($deepest->type & Y::LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
| 148 | + if ($previous->type & Y::SCALAR) { |
|
| 149 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 150 | + } |
|
| 151 | + if ($deepest->type & Y::LITTERALS) { |
|
| 152 | + $emptyLines[] = $n->setParent($deepest); |
|
| 153 | + } |
|
| 142 | 154 | return true; |
| 143 | 155 | } |
| 144 | 156 | return false; |
@@ -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 | |
@@ -84,7 +86,9 @@ discard block |
||
| 84 | 86 | } else { |
| 85 | 87 | $tmp = $node->getPhpValue(); |
| 86 | 88 | } |
| 87 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 89 | + if ($type === Y::REF_DEF) { |
|
| 90 | + self::$_root->addReference($identifier, $tmp); |
|
| 91 | + } |
|
| 88 | 92 | return self::$_root->getReference($identifier); |
| 89 | 93 | } |
| 90 | 94 | $typesActions = [Y::COMMENT => 'buildComment', |
@@ -169,10 +173,14 @@ discard block |
||
| 169 | 173 | } |
| 170 | 174 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 171 | 175 | foreach ($_root->value as $child) { |
| 172 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 176 | + if ($child->type & Y::DOC_START) { |
|
| 177 | + $totalDocStart++; |
|
| 178 | + } |
|
| 173 | 179 | //if 0 or 1 DOC_START = we are still in first document |
| 174 | 180 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 175 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 181 | + if (!isset($documents[$currentDoc])) { |
|
| 182 | + $documents[$currentDoc] = new NodeList(); |
|
| 183 | + } |
|
| 176 | 184 | $documents[$currentDoc]->push($child); |
| 177 | 185 | } |
| 178 | 186 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
@@ -242,9 +250,15 @@ discard block |
||
| 242 | 250 | $lines[] = $prefix.$child->value; |
| 243 | 251 | } |
| 244 | 252 | } |
| 245 | - if ($type & Y::RAW) return implode('', $lines); |
|
| 246 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
| 247 | - if ($type & Y::LITT_FOLDED) return implode(' ', $lines); |
|
| 253 | + if ($type & Y::RAW) { |
|
| 254 | + return implode('', $lines); |
|
| 255 | + } |
|
| 256 | + if ($type & Y::LITT) { |
|
| 257 | + return implode("\n", $lines); |
|
| 258 | + } |
|
| 259 | + if ($type & Y::LITT_FOLDED) { |
|
| 260 | + return implode(' ', $lines); |
|
| 261 | + } |
|
| 248 | 262 | return ''; |
| 249 | 263 | } |
| 250 | 264 | |
@@ -259,8 +273,9 @@ discard block |
||
| 259 | 273 | private function buildSetKey(Node $node, $parent):void |
| 260 | 274 | { |
| 261 | 275 | $key = json_encode(self::build($node->value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 262 | - if (empty($key)) |
|
| 263 | - throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 276 | + if (empty($key)) { |
|
| 277 | + throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 278 | + } |
|
| 264 | 279 | $parent->{$key} = null; |
| 265 | 280 | } |
| 266 | 281 | |
@@ -299,8 +314,11 @@ discard block |
||
| 299 | 314 | } |
| 300 | 315 | //TODO: have somewhere a list of common tags and their treatment |
| 301 | 316 | if (in_array($node->identifier, ['!binary', '!str'])) { |
| 302 | - if ($node->value->value instanceof NodeList) $node->value->value->type = Y::RAW; |
|
| 303 | - else $node->value->type = Y::RAW; |
|
| 317 | + if ($node->value->value instanceof NodeList) { |
|
| 318 | + $node->value->value->type = Y::RAW; |
|
| 319 | + } else { |
|
| 320 | + $node->value->type = Y::RAW; |
|
| 321 | + } |
|
| 304 | 322 | } |
| 305 | 323 | $val = is_null($node->value) ? null : self::build(/** @scrutinizer ignore-type */ $node->value, $node); |
| 306 | 324 | return new Tag($node->identifier, $val); |