@@ -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 | } |
@@ -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 | |
@@ -83,11 +85,15 @@ discard block |
||
| 83 | 85 | } else { |
| 84 | 86 | $tmp = Node2PHP::get($node); |
| 85 | 87 | } |
| 86 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 88 | + if ($type === Y::REF_DEF) { |
|
| 89 | + self::$_root->addReference($identifier, $tmp); |
|
| 90 | + } |
|
| 87 | 91 | return self::$_root->getReference($identifier); |
| 88 | 92 | } |
| 89 | 93 | |
| 90 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
| 94 | + if ($type & Y::COMMENT) { |
|
| 95 | + self::$_root->addComment($node->line, $node->value); |
|
| 96 | + } |
|
| 91 | 97 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
| 92 | 98 | Y::ITEM => 'buildItem', |
| 93 | 99 | Y::KEY => 'buildKey', |
@@ -179,10 +185,14 @@ discard block |
||
| 179 | 185 | } |
| 180 | 186 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 181 | 187 | foreach ($_root->value as $child) { |
| 182 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 188 | + if ($child->type & Y::DOC_START) { |
|
| 189 | + $totalDocStart++; |
|
| 190 | + } |
|
| 183 | 191 | //if 0 or 1 DOC_START = we are still in first document |
| 184 | 192 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 185 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 193 | + if (!isset($documents[$currentDoc])) { |
|
| 194 | + $documents[$currentDoc] = new NodeList(); |
|
| 195 | + } |
|
| 186 | 196 | $documents[$currentDoc]->push($child); |
| 187 | 197 | } |
| 188 | 198 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
@@ -280,9 +290,15 @@ discard block |
||
| 280 | 290 | $lines[] = $prefix.$child->value; |
| 281 | 291 | } |
| 282 | 292 | } |
| 283 | - if ($type & Y::RAW) return implode('', $lines); |
|
| 284 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
| 285 | - if ($type & Y::LITT_FOLDED) return preg_replace(['/ +(\n)/','/(\n+) +/'], "$1", implode(' ', $lines)); |
|
| 293 | + if ($type & Y::RAW) { |
|
| 294 | + return implode('', $lines); |
|
| 295 | + } |
|
| 296 | + if ($type & Y::LITT) { |
|
| 297 | + return implode("\n", $lines); |
|
| 298 | + } |
|
| 299 | + if ($type & Y::LITT_FOLDED) { |
|
| 300 | + return preg_replace(['/ +(\n)/','/(\n+) +/'], "$1", implode(' ', $lines)); |
|
| 301 | + } |
|
| 286 | 302 | return ''; |
| 287 | 303 | } |
| 288 | 304 | |
@@ -25,8 +25,12 @@ |
||
| 25 | 25 | public function jsonSerialize():array |
| 26 | 26 | { |
| 27 | 27 | $prop = get_object_vars($this); |
| 28 | - if (count($prop) > 0) return $prop; |
|
| 29 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 28 | + if (count($prop) > 0) { |
|
| 29 | + return $prop; |
|
| 30 | + } |
|
| 31 | + if (count($this) > 0) { |
|
| 32 | + return iterator_to_array($this); |
|
| 33 | + } |
|
| 30 | 34 | } |
| 31 | 35 | |
| 32 | 36 | /** |
@@ -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) || $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 |
@@ -39,7 +39,9 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | public static function toString($dataType, int $options = null):string |
| 41 | 41 | { |
| 42 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 42 | + if (is_null($dataType)) { |
|
| 43 | + throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 44 | + } |
|
| 43 | 45 | self::$options = is_int($options) ? $options : self::OPTIONS; |
| 44 | 46 | self::$result = new DLL; |
| 45 | 47 | if ($dataType instanceof YamlObject) { |
@@ -73,9 +75,13 @@ discard block |
||
| 73 | 75 | if (is_scalar($dataType)) { |
| 74 | 76 | switch (gettype($dataType)) { |
| 75 | 77 | case 'boolean': return $dataType ? 'true' : 'false'; |
| 76 | - case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 78 | + case 'float': if (is_infinite($dataType)) { |
|
| 79 | + return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 80 | + } |
|
| 77 | 81 | return sprintf('%.2F', $dataType); |
| 78 | - case 'double': if (is_nan((float) $dataType)) return '.nan'; |
|
| 82 | + case 'double': if (is_nan((float) $dataType)) { |
|
| 83 | + return '.nan'; |
|
| 84 | + } |
|
| 79 | 85 | default: |
| 80 | 86 | return $dataType; |
| 81 | 87 | } |
@@ -139,9 +145,13 @@ discard block |
||
| 139 | 145 | self::add(self::dump($obj->value, $indent + self::INDENT), $indent + self::INDENT); |
| 140 | 146 | } |
| 141 | 147 | } |
| 142 | - if ($obj instanceof Compact) return self::dumpCompact($obj, $indent); |
|
| 148 | + if ($obj instanceof Compact) { |
|
| 149 | + return self::dumpCompact($obj, $indent); |
|
| 150 | + } |
|
| 143 | 151 | //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'); |
|
| 152 | + if ($obj instanceof \DateTime) { |
|
| 153 | + return $obj->format('Y-m-d'); |
|
| 154 | + } |
|
| 145 | 155 | $propList = get_object_vars($obj); |
| 146 | 156 | foreach ($propList as $property => $value) { |
| 147 | 157 | if (is_scalar($value)) { |
@@ -67,7 +67,9 @@ discard block |
||
| 67 | 67 | if ($this->type === Y::ROOT) { |
| 68 | 68 | return $this; |
| 69 | 69 | } |
| 70 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
| 70 | + if (!is_int($indent)) { |
|
| 71 | + return $this->parent ?? $this; |
|
| 72 | + } |
|
| 71 | 73 | $cursor = $this; |
| 72 | 74 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
| 73 | 75 | if ($cursor->indent === $indent && $cursor->type !== $type) { |
@@ -106,13 +108,21 @@ discard block |
||
| 106 | 108 | $this->value->push($current); |
| 107 | 109 | } |
| 108 | 110 | //modify type according to child |
| 109 | - if ($current->type & Y::SET_KEY) $this->value->type = Y::SET; |
|
| 110 | - if ($current->type & Y::KEY) $this->value->type = Y::MAPPING; |
|
| 111 | - if ($current->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
|
| 111 | + if ($current->type & Y::SET_KEY) { |
|
| 112 | + $this->value->type = Y::SET; |
|
| 113 | + } |
|
| 114 | + if ($current->type & Y::KEY) { |
|
| 115 | + $this->value->type = Y::MAPPING; |
|
| 116 | + } |
|
| 117 | + if ($current->type & Y::ITEM) { |
|
| 118 | + $this->value->type = Y::SEQUENCE; |
|
| 119 | + } |
|
| 112 | 120 | } |
| 113 | 121 | $this->value->push($child); |
| 114 | 122 | |
| 115 | - if ($this->type & Y::LITTERALS) $this->value->type = $this->type; |
|
| 123 | + if ($this->type & Y::LITTERALS) { |
|
| 124 | + $this->value->type = $this->type; |
|
| 125 | + } |
|
| 116 | 126 | } |
| 117 | 127 | } |
| 118 | 128 | |
@@ -21,10 +21,15 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public static function get(Node $n) |
| 23 | 23 | { |
| 24 | - if (is_null($n->value)) return null; |
|
| 25 | - if ($n->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($n->value); |
|
| 26 | - if ($n->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) |
|
| 27 | - return self::getCompact(substr($n->value, 1, -1), $n->type); |
|
| 24 | + if (is_null($n->value)) { |
|
| 25 | + return null; |
|
| 26 | + } |
|
| 27 | + if ($n->type & (Y::REF_CALL | Y::SCALAR)) { |
|
| 28 | + return self::getScalar($n->value); |
|
| 29 | + } |
|
| 30 | + if ($n->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) { |
|
| 31 | + return self::getCompact(substr($n->value, 1, -1), $n->type); |
|
| 32 | + } |
|
| 28 | 33 | $expected = [Y::JSON => json_decode($n->value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
| 29 | 34 | Y::QUOTED => trim($n->value, "\"'"), |
| 30 | 35 | Y::RAW => strval($n->value)]; |
@@ -40,8 +45,12 @@ discard block |
||
| 40 | 45 | */ |
| 41 | 46 | private static function getScalar(string $v) |
| 42 | 47 | { |
| 43 | - if (R::isDate($v)) return date_create($v); |
|
| 44 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 48 | + if (R::isDate($v)) { |
|
| 49 | + return date_create($v); |
|
| 50 | + } |
|
| 51 | + if (R::isNumber($v)) { |
|
| 52 | + return self::getNumber($v); |
|
| 53 | + } |
|
| 45 | 54 | $types = ['yes' => true, |
| 46 | 55 | 'no' => false, |
| 47 | 56 | 'true' => true, |
@@ -63,8 +72,12 @@ discard block |
||
| 63 | 72 | */ |
| 64 | 73 | private static function getNumber(string $v) |
| 65 | 74 | { |
| 66 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
| 67 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
| 75 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
| 76 | + return intval(base_convert($v, 8, 10)); |
|
| 77 | + } |
|
| 78 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
| 79 | + return intval(base_convert($v, 16, 10)); |
|
| 80 | + } |
|
| 68 | 81 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
| 69 | 82 | } |
| 70 | 83 | |