@@ -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 | } |
@@ -32,7 +32,7 @@ |
||
| 32 | 32 | public function __call($funcName, $arguments) |
| 33 | 33 | { |
| 34 | 34 | $reflectAPI = new \ReflectionClass(get_class($this->__yaml__object__api)); |
| 35 | - $getName = function ($o) { return $o->name; }; |
|
| 35 | + $getName = function($o) { return $o->name; }; |
|
| 36 | 36 | $publicApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PUBLIC)); |
| 37 | 37 | $privateApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PRIVATE)); |
| 38 | 38 | if (!in_array($funcName, $publicApi) && !in_array($funcName, $privateApi)) { |
@@ -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 | } |
@@ -19,17 +19,17 @@ discard block |
||
| 19 | 19 | /* @var null|string */ |
| 20 | 20 | public $error; |
| 21 | 21 | |
| 22 | - public const EXCLUDE_DIRECTIVES = 1;//DONT include_directive |
|
| 23 | - public const IGNORE_COMMENTS = 2;//DONT include_comments |
|
| 24 | - public const NO_PARSING_EXCEPTIONS = 4;//THROW Exception on parsing Errors |
|
| 25 | - public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
|
| 22 | + public const EXCLUDE_DIRECTIVES = 1; //DONT include_directive |
|
| 23 | + public const IGNORE_COMMENTS = 2; //DONT include_comments |
|
| 24 | + public const NO_PARSING_EXCEPTIONS = 4; //THROW Exception on parsing Errors |
|
| 25 | + public const NO_OBJECT_FOR_DATE = 8; //DONT import date strings as dateTime Object |
|
| 26 | 26 | //privates |
| 27 | 27 | /* @var null|string */ |
| 28 | 28 | private $content; |
| 29 | 29 | /* @var null|string */ |
| 30 | 30 | private $filePath; |
| 31 | 31 | /* @var integer */ |
| 32 | - private $debug = 0;///TODO: determine levels |
|
| 32 | + private $debug = 0; ///TODO: determine levels |
|
| 33 | 33 | /* @var integer */ |
| 34 | 34 | private $options = 0; |
| 35 | 35 | //Exceptions messages |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | $previous = $root = new Node(); |
| 92 | 92 | $emptyLines = []; |
| 93 | 93 | try { |
| 94 | - $gen = function () use($source) { |
|
| 94 | + $gen = function() use($source) { |
|
| 95 | 95 | foreach ($source as $key => $value) { |
| 96 | 96 | yield ++$key => $value; |
| 97 | 97 | } |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | $target->add($n); |
| 118 | 118 | $previous = $n; |
| 119 | 119 | } |
| 120 | - if ($this->debug === 2) echo "\033[33mParsed Structure\033[0m\n",var_export($root, true); |
|
| 120 | + if ($this->debug === 2) echo "\033[33mParsed Structure\033[0m\n", var_export($root, true); |
|
| 121 | 121 | $out = Builder::buildContent($root, $this->debug); |
| 122 | 122 | return $out; |
| 123 | 123 | } catch (\Error|\Exception|\ParseError $e) { |
@@ -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; |
@@ -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; |
@@ -261,8 +261,8 @@ |
||
| 261 | 261 | if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
| 262 | 262 | if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
| 263 | 263 | $expected = [Y::JSON => json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
| 264 | - Y::QUOTED => substr($v, 1, -1), |
|
| 265 | - Y::RAW => strval($v)]; |
|
| 264 | + Y::QUOTED => substr($v, 1, -1), |
|
| 265 | + Y::RAW => strval($v)]; |
|
| 266 | 266 | if (isset($expected[$this->type])) { |
| 267 | 267 | return $expected[$this->type]; |
| 268 | 268 | } |
@@ -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 | |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | } |
| 95 | 95 | $this->value->push($child); |
| 96 | 96 | //modify type according to child |
| 97 | - if ($child->type & (Y::COMMENT | Y::KEY)) $this->value->type = Y::MAPPING; |
|
| 97 | + if ($child->type & (Y::COMMENT|Y::KEY)) $this->value->type = Y::MAPPING; |
|
| 98 | 98 | if ($child->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
| 99 | 99 | if ($this->type & Y::LITTERALS) $this->value->type = $this->type; |
| 100 | 100 | } |
@@ -158,8 +158,8 @@ discard block |
||
| 158 | 158 | if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
| 159 | 159 | if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
| 160 | 160 | // Note : php don't like '?' as an array key -_- |
| 161 | - if($first === '?') return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 162 | - $characters = [ '#' => [Y::COMMENT, ltrim($v)], |
|
| 161 | + if ($first === '?') return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 162 | + $characters = ['#' => [Y::COMMENT, ltrim($v)], |
|
| 163 | 163 | "-" => $this->onHyphen($nodeValue), |
| 164 | 164 | '%' => [Y::DIRECTIVE, ltrim($v)], |
| 165 | 165 | ':' => [Y::SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)], |
@@ -277,8 +277,8 @@ discard block |
||
| 277 | 277 | { |
| 278 | 278 | $v = &$this->value; |
| 279 | 279 | if (is_null($v)) return null; |
| 280 | - if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
|
| 281 | - if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 280 | + if ($this->type & (Y::REF_CALL|Y::SCALAR)) return self::getScalar($v); |
|
| 281 | + if ($this->type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 282 | 282 | $expected = [Y::JSON => json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
| 283 | 283 | Y::QUOTED => substr($v, 1, -1), |
| 284 | 284 | Y::RAW => strval($v)]; |
@@ -340,7 +340,7 @@ discard block |
||
| 340 | 340 | //TODO : this should handle references present inside the string |
| 341 | 341 | $out = new Compact(); |
| 342 | 342 | if ($type === Y::COMPACT_SEQUENCE) { |
| 343 | - $f = function ($e) { return self::getScalar(trim($e));}; |
|
| 343 | + $f = function($e) { return self::getScalar(trim($e)); }; |
|
| 344 | 344 | //TODO : that's not robust enough, improve it |
| 345 | 345 | foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) { |
| 346 | 346 | $out[$key] = $value; |
@@ -65,12 +65,12 @@ |
||
| 65 | 65 | return self::$_root->getReference($identifier); |
| 66 | 66 | } |
| 67 | 67 | $typesActions = [Y::COMMENT => 'buildComment', |
| 68 | - Y::DIRECTIVE => 'buildDirective', |
|
| 69 | - Y::ITEM => 'buildItem', |
|
| 70 | - Y::KEY => 'buildKey', |
|
| 71 | - Y::SET_KEY => 'buildSetKey', |
|
| 72 | - Y::SET_VALUE => 'buildSetValue', |
|
| 73 | - Y::TAG => 'buildTag', |
|
| 68 | + Y::DIRECTIVE => 'buildDirective', |
|
| 69 | + Y::ITEM => 'buildItem', |
|
| 70 | + Y::KEY => 'buildKey', |
|
| 71 | + Y::SET_KEY => 'buildSetKey', |
|
| 72 | + Y::SET_VALUE => 'buildSetValue', |
|
| 73 | + Y::TAG => 'buildTag', |
|
| 74 | 74 | ]; |
| 75 | 75 | if (isset($typesActions[$type])) { |
| 76 | 76 | return self::{$typesActions[$type]}($node, $parent); |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | private static function buildNodeList(NodeList $node, &$parent) |
| 29 | 29 | { |
| 30 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
| 30 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
| 31 | 31 | return self::buildLitteral($node, $node->type); |
| 32 | 32 | } |
| 33 | 33 | $p = $parent; |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | private static function buildNode(Node $node, &$parent) |
| 55 | 55 | { |
| 56 | 56 | extract((array) $node, EXTR_REFS); |
| 57 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
| 57 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
| 58 | 58 | if (is_object($value)) { |
| 59 | 59 | $tmp = self::build($value, $parent) ?? $parent; |
| 60 | 60 | } else { |
@@ -150,15 +150,15 @@ discard block |
||
| 150 | 150 | { |
| 151 | 151 | self::$_root = new YamlObject(); |
| 152 | 152 | $childTypes = $list->getTypes(); |
| 153 | - $isaMapping = (bool) (Y::KEY | Y::MAPPING) & $childTypes; |
|
| 153 | + $isaMapping = (bool) (Y::KEY|Y::MAPPING)&$childTypes; |
|
| 154 | 154 | $isaSequence = (bool) Y::ITEM & $childTypes; |
| 155 | 155 | $isaSet = (bool) Y::SET_VALUE & $childTypes; |
| 156 | 156 | if ($isaMapping && $isaSequence) { |
| 157 | 157 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
| 158 | 158 | } else { |
| 159 | 159 | switch (true) { |
| 160 | - case $isaSequence: $list->type = Y::SEQUENCE;break; |
|
| 161 | - case $isaSet: $list->type = Y::SET;break; |
|
| 160 | + case $isaSequence: $list->type = Y::SEQUENCE; break; |
|
| 161 | + case $isaSet: $list->type = Y::SET; break; |
|
| 162 | 162 | default: $list->type = Y::MAPPING; |
| 163 | 163 | } |
| 164 | 164 | } |
@@ -185,15 +185,15 @@ discard block |
||
| 185 | 185 | $lines[] = self::buildLitteral($child->value, $type); |
| 186 | 186 | } else { |
| 187 | 187 | $prefix = ''; |
| 188 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 188 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 189 | 189 | $prefix = "\n"; |
| 190 | 190 | } |
| 191 | 191 | $lines[] = $prefix.$child->value; |
| 192 | 192 | } |
| 193 | 193 | } |
| 194 | - if ($type & Y::RAW) return implode('', $lines); |
|
| 195 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
| 196 | - if ($type & Y::LITT_FOLDED) return implode(' ', $lines); |
|
| 194 | + if ($type&Y::RAW) return implode('', $lines); |
|
| 195 | + if ($type&Y::LITT) return implode("\n", $lines); |
|
| 196 | + if ($type&Y::LITT_FOLDED) return implode(' ', $lines); |
|
| 197 | 197 | return ''; |
| 198 | 198 | } |
| 199 | 199 | |
@@ -21,7 +21,9 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | private static function build(object $node, &$parent = null) |
| 23 | 23 | { |
| 24 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
| 24 | + if ($node instanceof NodeList) { |
|
| 25 | + return self::buildNodeList($node, $parent); |
|
| 26 | + } |
|
| 25 | 27 | return self::buildNode($node, $parent); |
| 26 | 28 | } |
| 27 | 29 | |
@@ -60,7 +62,9 @@ discard block |
||
| 60 | 62 | } else { |
| 61 | 63 | $tmp = $node->getPhpValue(); |
| 62 | 64 | } |
| 63 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 65 | + if ($type === Y::REF_DEF) { |
|
| 66 | + self::$_root->addReference($identifier, $tmp); |
|
| 67 | + } |
|
| 64 | 68 | return self::$_root->getReference($identifier); |
| 65 | 69 | } |
| 66 | 70 | $typesActions = [Y::COMMENT => 'buildComment', |
@@ -136,10 +140,14 @@ discard block |
||
| 136 | 140 | } |
| 137 | 141 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 138 | 142 | foreach ($_root->value as $child) { |
| 139 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 143 | + if ($child->type & Y::DOC_START) { |
|
| 144 | + $totalDocStart++; |
|
| 145 | + } |
|
| 140 | 146 | //if 0 or 1 DOC_START = we are still in first document |
| 141 | 147 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 142 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 148 | + if (!isset($documents[$currentDoc])) { |
|
| 149 | + $documents[$currentDoc] = new NodeList(); |
|
| 150 | + } |
|
| 143 | 151 | $documents[$currentDoc]->push($child); |
| 144 | 152 | } |
| 145 | 153 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
@@ -191,17 +199,24 @@ discard block |
||
| 191 | 199 | $lines[] = $prefix.$child->value; |
| 192 | 200 | } |
| 193 | 201 | } |
| 194 | - if ($type & Y::RAW) return implode('', $lines); |
|
| 195 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
| 196 | - if ($type & Y::LITT_FOLDED) return implode(' ', $lines); |
|
| 202 | + if ($type & Y::RAW) { |
|
| 203 | + return implode('', $lines); |
|
| 204 | + } |
|
| 205 | + if ($type & Y::LITT) { |
|
| 206 | + return implode("\n", $lines); |
|
| 207 | + } |
|
| 208 | + if ($type & Y::LITT_FOLDED) { |
|
| 209 | + return implode(' ', $lines); |
|
| 210 | + } |
|
| 197 | 211 | return ''; |
| 198 | 212 | } |
| 199 | 213 | |
| 200 | 214 | private function buildSetKey(Node $node, $parent):void |
| 201 | 215 | { |
| 202 | 216 | $key = json_encode(self::build($node->value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 203 | - if (empty($key)) |
|
| 204 | - throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 217 | + if (empty($key)) { |
|
| 218 | + throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 219 | + } |
|
| 205 | 220 | $parent->{$key} = null; |
| 206 | 221 | } |
| 207 | 222 | |
@@ -226,8 +241,11 @@ discard block |
||
| 226 | 241 | } |
| 227 | 242 | //TODO: have somewhere a list of common tags and their treatment |
| 228 | 243 | if (in_array($node->identifier, ['!binary', '!str'])) { |
| 229 | - if ($node->value->value instanceof NodeList) $node->value->value->type = Y::RAW; |
|
| 230 | - else $node->value->type = Y::RAW; |
|
| 244 | + if ($node->value->value instanceof NodeList) { |
|
| 245 | + $node->value->value->type = Y::RAW; |
|
| 246 | + } else { |
|
| 247 | + $node->value->type = Y::RAW; |
|
| 248 | + } |
|
| 231 | 249 | } |
| 232 | 250 | $val = is_null($node->value) ? null : self::build(/** @scrutinizer ignore-type */ $node->value, $node); |
| 233 | 251 | return new Tag($node->identifier, $val); |