@@ -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) |
@@ -24,7 +24,9 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | public function __construct(int $options = null) |
| 26 | 26 | { |
| 27 | - if (is_int($options)) self::$options = $options; |
|
| 27 | + if (is_int($options)) { |
|
| 28 | + self::$options = $options; |
|
| 29 | + } |
|
| 28 | 30 | } |
| 29 | 31 | |
| 30 | 32 | /** |
@@ -39,7 +41,9 @@ discard block |
||
| 39 | 41 | */ |
| 40 | 42 | public static function toString($dataType, int $options = null):string |
| 41 | 43 | { |
| 42 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 44 | + if (is_null($dataType)) { |
|
| 45 | + throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 46 | + } |
|
| 43 | 47 | self::$options = is_int($options) ? $options : self::OPTIONS; |
| 44 | 48 | self::$result = new DLL; |
| 45 | 49 | if ($dataType instanceof YamlObject) { |
@@ -73,9 +77,13 @@ discard block |
||
| 73 | 77 | if (is_scalar($dataType)) { |
| 74 | 78 | switch (gettype($dataType)) { |
| 75 | 79 | case 'boolean': return $dataType ? 'true' : 'false'; |
| 76 | - case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 80 | + case 'float': if (is_infinite($dataType)) { |
|
| 81 | + return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 82 | + } |
|
| 77 | 83 | return sprintf('%.2F', $dataType); |
| 78 | - case 'double': if (is_nan((float) $dataType)) return '.nan'; |
|
| 84 | + case 'double': if (is_nan((float) $dataType)) { |
|
| 85 | + return '.nan'; |
|
| 86 | + } |
|
| 79 | 87 | default: |
| 80 | 88 | return $dataType; |
| 81 | 89 | } |
@@ -139,9 +147,13 @@ discard block |
||
| 139 | 147 | self::add(self::dump($obj->value, $indent + self::INDENT), $indent + self::INDENT); |
| 140 | 148 | } |
| 141 | 149 | } |
| 142 | - if ($obj instanceof Compact) return self::dumpCompact($obj, $indent); |
|
| 150 | + if ($obj instanceof Compact) { |
|
| 151 | + return self::dumpCompact($obj, $indent); |
|
| 152 | + } |
|
| 143 | 153 | //TODO: consider dumping datetime as date strings according to a format provided by user or default |
| 144 | - if ($obj instanceof \DateTime) return $obj->format('Y-m-d'); |
|
| 154 | + if ($obj instanceof \DateTime) { |
|
| 155 | + return $obj->format('Y-m-d'); |
|
| 156 | + } |
|
| 145 | 157 | $propList = get_object_vars($obj); |
| 146 | 158 | foreach ($propList as $property => $value) { |
| 147 | 159 | if (is_scalar($value)) { |
@@ -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); |
@@ -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 |
@@ -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 | |