@@ -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; |
@@ -150,8 +152,12 @@ discard block |
||
| 150 | 152 | $type = R::isProperlyQuoted($nodeValue) ? Y::QUOTED : Y::PARTIAL; |
| 151 | 153 | return [$type, $nodeValue]; |
| 152 | 154 | } |
| 153 | - if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
|
| 154 | - if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
|
| 155 | + if (in_array($first, ['{', '['])) { |
|
| 156 | + return $this->onObject($nodeValue); |
|
| 157 | + } |
|
| 158 | + if (in_array($first, ['!', '&', '*'])) { |
|
| 159 | + return $this->onNodeAction($nodeValue); |
|
| 160 | + } |
|
| 155 | 161 | switch ($first) { |
| 156 | 162 | case '#': return [Y::COMMENT, ltrim($v)]; |
| 157 | 163 | case "-": return $this->onHyphen($nodeValue); |
@@ -203,9 +209,15 @@ discard block |
||
| 203 | 209 | private function onObject($value):array |
| 204 | 210 | { |
| 205 | 211 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 206 | - if (json_last_error() === JSON_ERROR_NONE) return [Y::JSON, $value]; |
|
| 207 | - if (preg_match(R::MAPPING, $value)) return [Y::COMPACT_MAPPING, $value]; |
|
| 208 | - if (preg_match(R::SEQUENCE, $value)) return [Y::COMPACT_SEQUENCE, $value]; |
|
| 212 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
| 213 | + return [Y::JSON, $value]; |
|
| 214 | + } |
|
| 215 | + if (preg_match(R::MAPPING, $value)) { |
|
| 216 | + return [Y::COMPACT_MAPPING, $value]; |
|
| 217 | + } |
|
| 218 | + if (preg_match(R::SEQUENCE, $value)) { |
|
| 219 | + return [Y::COMPACT_SEQUENCE, $value]; |
|
| 220 | + } |
|
| 209 | 221 | return [Y::PARTIAL, $value]; |
| 210 | 222 | } |
| 211 | 223 | |
@@ -220,7 +232,9 @@ discard block |
||
| 220 | 232 | { |
| 221 | 233 | if (substr($nodeValue, 0, 3) === '---') { |
| 222 | 234 | $rest = trim(substr($nodeValue, 3)); |
| 223 | - if (empty($rest)) return [Y::DOC_START, null]; |
|
| 235 | + if (empty($rest)) { |
|
| 236 | + return [Y::DOC_START, null]; |
|
| 237 | + } |
|
| 224 | 238 | $n = new Node($rest, $this->line); |
| 225 | 239 | $n->indent = $this->indent + 4; |
| 226 | 240 | return [Y::DOC_START, $n->setParent($this)]; |
@@ -262,9 +276,15 @@ discard block |
||
| 262 | 276 | public function getPhpValue() |
| 263 | 277 | { |
| 264 | 278 | $v = &$this->value; |
| 265 | - if (is_null($v)) return null; |
|
| 266 | - if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
|
| 267 | - if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 279 | + if (is_null($v)) { |
|
| 280 | + return null; |
|
| 281 | + } |
|
| 282 | + if ($this->type & (Y::REF_CALL | Y::SCALAR)) { |
|
| 283 | + return self::getScalar($v); |
|
| 284 | + } |
|
| 285 | + if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) { |
|
| 286 | + return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 287 | + } |
|
| 268 | 288 | switch ($this->type) { |
| 269 | 289 | case Y::JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
| 270 | 290 | case Y::QUOTED: return substr($v, 1, -1); |
@@ -293,9 +313,15 @@ discard block |
||
| 293 | 313 | '-.inf' => -INF, |
| 294 | 314 | '.nan' => NAN |
| 295 | 315 | ]; |
| 296 | - if (isset($types[strtolower($v)])) return $types[strtolower($v)]; |
|
| 297 | - if (R::isDate($v)) return date_create($v); |
|
| 298 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 316 | + if (isset($types[strtolower($v)])) { |
|
| 317 | + return $types[strtolower($v)]; |
|
| 318 | + } |
|
| 319 | + if (R::isDate($v)) { |
|
| 320 | + return date_create($v); |
|
| 321 | + } |
|
| 322 | + if (R::isNumber($v)) { |
|
| 323 | + return self::getNumber($v); |
|
| 324 | + } |
|
| 299 | 325 | return strval($v); |
| 300 | 326 | } |
| 301 | 327 | |
@@ -308,8 +334,12 @@ discard block |
||
| 308 | 334 | */ |
| 309 | 335 | private static function getNumber(string $v) |
| 310 | 336 | { |
| 311 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
| 312 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
| 337 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
| 338 | + return intval(base_convert($v, 8, 10)); |
|
| 339 | + } |
|
| 340 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
| 341 | + return intval(base_convert($v, 16, 10)); |
|
| 342 | + } |
|
| 313 | 343 | // TODO: remove these if not needed |
| 314 | 344 | // if preg_match("/^([\d.]+e[-+]\d{1,2})$/", $v)://fall through |
| 315 | 345 | // if preg_match("/^([-+]?(?:\d+|\d*.\d+))$/", $v): |
@@ -37,7 +37,7 @@ |
||
| 37 | 37 | $matchSpaced = preg_match($spaced, $v); |
| 38 | 38 | $matchIso = preg_match($iso8601, $v); |
| 39 | 39 | if (is_bool($matchDate) || is_bool($matchCanonical) || is_bool($matchSpaced) || is_bool($matchIso)) { |
| 40 | - throw new \Exception("Regex date error"); |
|
| 40 | + throw new \Exception("Regex date error"); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | return $matchDate || $matchCanonical || $matchSpaced || $matchIso; |