@@ -88,7 +88,9 @@ discard block |
||
| 88 | 88 | { |
| 89 | 89 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
| 90 | 90 | //TODO : be more permissive on $strContent values |
| 91 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 91 | + if (!is_array($source) || !count($source)) { |
|
| 92 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 93 | + } |
|
| 92 | 94 | $previous = $root = new Node(); |
| 93 | 95 | $emptyLines = []; |
| 94 | 96 | try { //var_dump($source); |
@@ -101,7 +103,9 @@ discard block |
||
| 101 | 103 | $n = new Node($lineString, $lineNb); |
| 102 | 104 | $deepest = $previous->getDeepestNode(); |
| 103 | 105 | if ($n->type & (Y::LITTERALS|Y::BLANK|Y::COMMENT|Y::TAG) || $deepest->type & Y::PARTIAL) { |
| 104 | - if ($this->onSpecialType($n, $previous, $emptyLines, $lineString)) continue; |
|
| 106 | + if ($this->onSpecialType($n, $previous, $emptyLines, $lineString)) { |
|
| 107 | + continue; |
|
| 108 | + } |
|
| 105 | 109 | } |
| 106 | 110 | //Note: 6.6 comments: Note that outside scalar content, a line containing only white space characters is taken to be a comment line. |
| 107 | 111 | foreach ($emptyLines as $blankNode) { |
@@ -135,7 +139,9 @@ discard block |
||
| 135 | 139 | } |
| 136 | 140 | } |
| 137 | 141 | } |
| 138 | - if ($this->onContextType($n, $target, $lineString)) continue; |
|
| 142 | + if ($this->onContextType($n, $target, $lineString)) { |
|
| 143 | + continue; |
|
| 144 | + } |
|
| 139 | 145 | $target->add($n); |
| 140 | 146 | $previous = $n; |
| 141 | 147 | } |
@@ -172,8 +178,12 @@ discard block |
||
| 172 | 178 | return true; |
| 173 | 179 | } |
| 174 | 180 | if ($n->type & Y::BLANK) { |
| 175 | - if ($previous->type & Y::SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 176 | - if ($deepest->type & Y::LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
| 181 | + if ($previous->type & Y::SCALAR) { |
|
| 182 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 183 | + } |
|
| 184 | + if ($deepest->type & Y::LITTERALS) { |
|
| 185 | + $emptyLines[] = $n->setParent($deepest); |
|
| 186 | + } |
|
| 177 | 187 | return true; |
| 178 | 188 | } |
| 179 | 189 | //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 | if ($cursor->indent === $indent && $cursor->type !== $type) { |
@@ -110,13 +112,21 @@ discard block |
||
| 110 | 112 | } |
| 111 | 113 | //modify type according to child |
| 112 | 114 | if (is_null($this->value->type)) { |
| 113 | - if ($child->type & Y::SET_KEY) $this->value->type = Y::SET; |
|
| 114 | - if ($child->type & Y::KEY) $this->value->type = Y::MAPPING; |
|
| 115 | - if ($child->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
|
| 115 | + if ($child->type & Y::SET_KEY) { |
|
| 116 | + $this->value->type = Y::SET; |
|
| 117 | + } |
|
| 118 | + if ($child->type & Y::KEY) { |
|
| 119 | + $this->value->type = Y::MAPPING; |
|
| 120 | + } |
|
| 121 | + if ($child->type & Y::ITEM) { |
|
| 122 | + $this->value->type = Y::SEQUENCE; |
|
| 123 | + } |
|
| 116 | 124 | } |
| 117 | 125 | $this->value->push($child); |
| 118 | 126 | |
| 119 | - if ($this->type & Y::LITTERALS) $this->value->type = $this->type; |
|
| 127 | + if ($this->type & Y::LITTERALS) { |
|
| 128 | + $this->value->type = $this->type; |
|
| 129 | + } |
|
| 120 | 130 | } |
| 121 | 131 | |
| 122 | 132 | /** |
@@ -44,10 +44,14 @@ discard block |
||
| 44 | 44 | } |
| 45 | 45 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 46 | 46 | foreach ($_root->value as $child) { |
| 47 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 47 | + if ($child->type & Y::DOC_START) { |
|
| 48 | + $totalDocStart++; |
|
| 49 | + } |
|
| 48 | 50 | //if 0 or 1 DOC_START = we are still in first document |
| 49 | 51 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 50 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 52 | + if (!isset($documents[$currentDoc])) { |
|
| 53 | + $documents[$currentDoc] = new NodeList(); |
|
| 54 | + } |
|
| 51 | 55 | $documents[$currentDoc]->push($child); |
| 52 | 56 | } |
| 53 | 57 | // $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
@@ -117,7 +121,9 @@ discard block |
||
| 117 | 121 | */ |
| 118 | 122 | private static function build(object $node, &$parent = null) |
| 119 | 123 | { |
| 120 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
| 124 | + if ($node instanceof NodeList) { |
|
| 125 | + return self::buildNodeList($node, $parent); |
|
| 126 | + } |
|
| 121 | 127 | return self::buildNode($node, $parent); |
| 122 | 128 | } |
| 123 | 129 | |
@@ -206,13 +212,17 @@ discard block |
||
| 206 | 212 | } else { |
| 207 | 213 | $tmp = Node2PHP::get($node); |
| 208 | 214 | } |
| 209 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 215 | + if ($type === Y::REF_DEF) { |
|
| 216 | + self::$_root->addReference($identifier, $tmp); |
|
| 217 | + } |
|
| 210 | 218 | return self::$_root->getReference($identifier); |
| 211 | 219 | } |
| 212 | 220 | if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
| 213 | 221 | return self::buildNodeList($node->value, $parent); |
| 214 | 222 | } |
| 215 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
| 223 | + if ($type & Y::COMMENT) { |
|
| 224 | + self::$_root->addComment($node->line, $node->value); |
|
| 225 | + } |
|
| 216 | 226 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
| 217 | 227 | Y::ITEM => 'buildItem', |
| 218 | 228 | Y::KEY => 'buildKey', |
@@ -351,9 +361,15 @@ discard block |
||
| 351 | 361 | $lines[] = $prefix.$val; |
| 352 | 362 | } |
| 353 | 363 | } |
| 354 | - if ($type & Y::RAW) return implode('', $lines); |
|
| 355 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
| 356 | - if ($type & Y::LITT_FOLDED) return preg_replace(['/ +(\n)/','/(\n+) +/'], "$1", implode(' ', $lines)); |
|
| 364 | + if ($type & Y::RAW) { |
|
| 365 | + return implode('', $lines); |
|
| 366 | + } |
|
| 367 | + if ($type & Y::LITT) { |
|
| 368 | + return implode("\n", $lines); |
|
| 369 | + } |
|
| 370 | + if ($type & Y::LITT_FOLDED) { |
|
| 371 | + return preg_replace(['/ +(\n)/','/(\n+) +/'], "$1", implode(' ', $lines)); |
|
| 372 | + } |
|
| 357 | 373 | return ''; |
| 358 | 374 | } |
| 359 | 375 | |
@@ -40,7 +40,9 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public static function toString($dataType, int $options = null):string |
| 42 | 42 | { |
| 43 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 43 | + if (is_null($dataType)) { |
|
| 44 | + throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 45 | + } |
|
| 44 | 46 | self::$options = is_int($options) ? $options : self::OPTIONS; |
| 45 | 47 | self::$result = new DLL; |
| 46 | 48 | if ($dataType instanceof YamlObject) { |
@@ -76,9 +78,13 @@ discard block |
||
| 76 | 78 | if (is_scalar($dataType)) { |
| 77 | 79 | switch (gettype($dataType)) { |
| 78 | 80 | case 'boolean': return $dataType ? 'true' : 'false'; |
| 79 | - case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 81 | + case 'float': if (is_infinite($dataType)) { |
|
| 82 | + return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 83 | + } |
|
| 80 | 84 | return sprintf('%.2F', $dataType); |
| 81 | - case 'double': if (is_nan((float) $dataType)) return '.nan'; |
|
| 85 | + case 'double': if (is_nan((float) $dataType)) { |
|
| 86 | + return '.nan'; |
|
| 87 | + } |
|
| 82 | 88 | default: |
| 83 | 89 | return $dataType; |
| 84 | 90 | } |
@@ -91,7 +97,9 @@ discard block |
||
| 91 | 97 | |
| 92 | 98 | private static function dumpYamlObject(YamlObject $dataType) |
| 93 | 99 | { |
| 94 | - if ($dataType->hasDocStart()) self::$result->push("---"); |
|
| 100 | + if ($dataType->hasDocStart()) { |
|
| 101 | + self::$result->push("---"); |
|
| 102 | + } |
|
| 95 | 103 | // self::dump($dataType, 0); |
| 96 | 104 | if (count($dataType) > 0) { |
| 97 | 105 | self::dumpSequence($dataType->getArrayCopy(), 0); |
@@ -142,9 +150,13 @@ discard block |
||
| 142 | 150 | self::add(self::dump($obj->value, $indent + self::INDENT), $indent + self::INDENT); |
| 143 | 151 | } |
| 144 | 152 | } |
| 145 | - if ($obj instanceof Compact) return self::dumpCompact($obj, $indent); |
|
| 153 | + if ($obj instanceof Compact) { |
|
| 154 | + return self::dumpCompact($obj, $indent); |
|
| 155 | + } |
|
| 146 | 156 | //TODO: consider dumping datetime as date strings according to a format provided by user or default |
| 147 | - if ($obj instanceof \DateTime) return $obj->format('Y-m-d'); |
|
| 157 | + if ($obj instanceof \DateTime) { |
|
| 158 | + return $obj->format('Y-m-d'); |
|
| 159 | + } |
|
| 148 | 160 | // if ($obj instanceof \SplString) {var_dump('splstrin',$obj);return '"'.$obj.'"';} |
| 149 | 161 | $propList = get_object_vars($obj); |
| 150 | 162 | foreach ($propList as $property => $value) { |
@@ -23,8 +23,12 @@ 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($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($n->value); |
|
| 31 | + } |
|
| 28 | 32 | // if ($n->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) |
| 29 | 33 | // return self::getCompact(substr($n->value, 1, -1), $n->type); |
| 30 | 34 | if ($n->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE| Y::JSON)) { |
@@ -46,8 +50,12 @@ discard block |
||
| 46 | 50 | */ |
| 47 | 51 | private static function getScalar(string $v) |
| 48 | 52 | { |
| 49 | - if (R::isDate($v)) return date_create($v); |
|
| 50 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 53 | + if (R::isDate($v)) { |
|
| 54 | + return date_create($v); |
|
| 55 | + } |
|
| 56 | + if (R::isNumber($v)) { |
|
| 57 | + return self::getNumber($v); |
|
| 58 | + } |
|
| 51 | 59 | $types = ['yes' => true, |
| 52 | 60 | 'no' => false, |
| 53 | 61 | 'true' => true, |
@@ -69,8 +77,12 @@ discard block |
||
| 69 | 77 | */ |
| 70 | 78 | private static function getNumber(string $v) |
| 71 | 79 | { |
| 72 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
| 73 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
| 80 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
| 81 | + return intval(base_convert($v, 8, 10)); |
|
| 82 | + } |
|
| 83 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
| 84 | + return intval(base_convert($v, 16, 10)); |
|
| 85 | + } |
|
| 74 | 86 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
| 75 | 87 | } |
| 76 | 88 | |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | { |
| 17 | 17 | if ($candidate){ |
| 18 | 18 | parent::__construct($candidate, 1); //1 = Array indices can be accessed as properties in read/write. |
| 19 | - }else{ |
|
| 19 | + } else{ |
|
| 20 | 20 | parent::__construct([], 1); //1 = Array indices can be accessed as properties in read/write. |
| 21 | 21 | } |
| 22 | 22 | } |
@@ -29,8 +29,12 @@ discard block |
||
| 29 | 29 | public function jsonSerialize():array |
| 30 | 30 | { |
| 31 | 31 | $prop = get_object_vars($this); |
| 32 | - if (count($prop) > 0) return $prop; |
|
| 33 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 32 | + if (count($prop) > 0) { |
|
| 33 | + return $prop; |
|
| 34 | + } |
|
| 35 | + if (count($this) > 0) { |
|
| 36 | + return iterator_to_array($this); |
|
| 37 | + } |
|
| 34 | 38 | } |
| 35 | 39 | |
| 36 | 40 | /** |
@@ -52,8 +52,12 @@ |
||
| 52 | 52 | { |
| 53 | 53 | $prop = get_object_vars($this); |
| 54 | 54 | unset($prop["__yaml__object__api"]); |
| 55 | - if (count($prop) > 0) return $prop; |
|
| 56 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 55 | + if (count($prop) > 0) { |
|
| 56 | + return $prop; |
|
| 57 | + } |
|
| 58 | + if (count($this) > 0) { |
|
| 59 | + return iterator_to_array($this); |
|
| 60 | + } |
|
| 57 | 61 | return $this->__yaml__object__api->value ?? "Empty YamlObject"; |
| 58 | 62 | } |
| 59 | 63 | } |