@@ -77,7 +77,9 @@ discard block |
||
77 | 77 | { |
78 | 78 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
79 | 79 | //TODO : be more permissive on $strContent values |
80 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
80 | + if (!is_array($source) || !count($source)) { |
|
81 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
82 | + } |
|
81 | 83 | return function () use($source) { |
82 | 84 | foreach ($source as $key => $value) { |
83 | 85 | yield ++$key => $value; |
@@ -105,7 +107,9 @@ discard block |
||
105 | 107 | try { |
106 | 108 | foreach ($sourceIterator() as $lineNb => $lineString) { |
107 | 109 | $n = new Node($lineString, $lineNb); |
108 | - if ($this->onSpecialType($n, $previous, $emptyLines, $lineString)) continue; |
|
110 | + if ($this->onSpecialType($n, $previous, $emptyLines, $lineString)) { |
|
111 | + continue; |
|
112 | + } |
|
109 | 113 | $this->attachBlankLines($emptyLines, $previous); |
110 | 114 | $emptyLines = []; |
111 | 115 | $target = $previous; |
@@ -117,7 +121,9 @@ discard block |
||
117 | 121 | default: |
118 | 122 | $this->onMoreIndent($previous, $target); |
119 | 123 | } |
120 | - if ($this->onContextType($n, $target, $lineString)) continue; |
|
124 | + if ($this->onContextType($n, $target, $lineString)) { |
|
125 | + continue; |
|
126 | + } |
|
121 | 127 | $target->add($n); |
122 | 128 | $previous = $n; |
123 | 129 | } |
@@ -185,8 +191,12 @@ discard block |
||
185 | 191 | return true; |
186 | 192 | } elseif ($n->type & Y::BLANK) { |
187 | 193 | // $this->onSpecialBlank($emptyLines, $n, $previous, $deepest); |
188 | - if ($previous->type & Y::SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
189 | - if ($deepest->type & Y::LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
194 | + if ($previous->type & Y::SCALAR) { |
|
195 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
196 | + } |
|
197 | + if ($deepest->type & Y::LITTERALS) { |
|
198 | + $emptyLines[] = $n->setParent($deepest); |
|
199 | + } |
|
190 | 200 | return true; |
191 | 201 | } elseif ($n->type & Y::COMMENT |
192 | 202 | && !($previous->getParent()->value->type & Y::LITTERALS) |
@@ -68,8 +68,12 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public function getParent(int $indent = null, $type = 0):Node |
70 | 70 | { |
71 | - if ($this->type === Y::ROOT) return $this; |
|
72 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
71 | + if ($this->type === Y::ROOT) { |
|
72 | + return $this; |
|
73 | + } |
|
74 | + if (!is_int($indent)) { |
|
75 | + return $this->parent ?? $this; |
|
76 | + } |
|
73 | 77 | $cursor = $this; |
74 | 78 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
75 | 79 | if ($cursor->indent === $indent && $cursor->type !== $type) { |
@@ -171,12 +175,17 @@ discard block |
||
171 | 175 | { |
172 | 176 | $v = ltrim(substr($nodeValue, 1)); |
173 | 177 | $first = $nodeValue[0]; |
174 | - if ($first === "-") NodeHandlers::onHyphen($nodeValue, $this); |
|
175 | - elseif (in_array($first, ['"', "'"])) NodeHandlers::onQuoted($nodeValue, $this); |
|
176 | - elseif (in_array($first, ['{', '['])) NodeHandlers::onCompact($nodeValue, $this); |
|
177 | - elseif (in_array($first, ['?', ':'])) NodeHandlers::onSetElement($nodeValue, $this); |
|
178 | - elseif (in_array($first, ['!', '&', '*'])) NodeHandlers::onNodeAction($nodeValue, $this); |
|
179 | - else { |
|
178 | + if ($first === "-") { |
|
179 | + NodeHandlers::onHyphen($nodeValue, $this); |
|
180 | + } elseif (in_array($first, ['"', "'"])) { |
|
181 | + NodeHandlers::onQuoted($nodeValue, $this); |
|
182 | + } elseif (in_array($first, ['{', '['])) { |
|
183 | + NodeHandlers::onCompact($nodeValue, $this); |
|
184 | + } elseif (in_array($first, ['?', ':'])) { |
|
185 | + NodeHandlers::onSetElement($nodeValue, $this); |
|
186 | + } elseif (in_array($first, ['!', '&', '*'])) { |
|
187 | + NodeHandlers::onNodeAction($nodeValue, $this); |
|
188 | + } else { |
|
180 | 189 | $characters = [ '#' => [Y::COMMENT, $nodeValue], |
181 | 190 | '%' => [Y::DIRECTIVE, $nodeValue], |
182 | 191 | '>' => [Y::LITT_FOLDED, null], |
@@ -86,7 +86,9 @@ discard block |
||
86 | 86 | */ |
87 | 87 | public static function build(object $node, &$parent = null) |
88 | 88 | { |
89 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
89 | + if ($node instanceof NodeList) { |
|
90 | + return self::buildNodeList($node, $parent); |
|
91 | + } |
|
90 | 92 | return self::buildNode($node, $parent); |
91 | 93 | } |
92 | 94 | |
@@ -186,7 +188,9 @@ discard block |
||
186 | 188 | $refIndent = $list->count() > 0 ? $list->current()->indent : 0; |
187 | 189 | if($list->count()) { |
188 | 190 | //remove trailing blank |
189 | - while ($list->top()->type & Y::BLANK) $list->pop(); |
|
191 | + while ($list->top()->type & Y::BLANK) { |
|
192 | + $list->pop(); |
|
193 | + } |
|
190 | 194 | $separator = [ Y::RAW => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][$type]; |
191 | 195 | foreach ($list as $child) { |
192 | 196 | if ($child->value instanceof NodeList) { |
@@ -215,10 +219,12 @@ discard block |
||
215 | 219 | } |
216 | 220 | } |
217 | 221 | if ($type & Y::LITT_FOLDED && ($node->indent > $refIndent || ($node->type & Y::BLANK))) { |
218 | - if ($result[-1] === $separator) |
|
219 | - $result[-1] = "\n"; |
|
220 | - if ($result[-1] === "\n") |
|
221 | - $result .= $val; |
|
222 | + if ($result[-1] === $separator) { |
|
223 | + $result[-1] = "\n"; |
|
224 | + } |
|
225 | + if ($result[-1] === "\n") { |
|
226 | + $result .= $val; |
|
227 | + } |
|
222 | 228 | return; |
223 | 229 | } |
224 | 230 | $result .= $val.$separator; |
@@ -42,7 +42,9 @@ discard block |
||
42 | 42 | */ |
43 | 43 | public static function toString($dataType, int $options = null):string |
44 | 44 | { |
45 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
45 | + if (is_null($dataType)) { |
|
46 | + throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
47 | + } |
|
46 | 48 | self::$options = is_int($options) ? $options : self::OPTIONS; |
47 | 49 | self::$result = new DLL; |
48 | 50 | if ($dataType instanceof YamlObject) { |
@@ -76,8 +78,12 @@ discard block |
||
76 | 78 | private static function dump($dataType, int $indent) |
77 | 79 | { |
78 | 80 | if (is_scalar($dataType)) { |
79 | - if ($dataType === INF) return '.inf'; |
|
80 | - if ($dataType === -INF) return '-.inf'; |
|
81 | + if ($dataType === INF) { |
|
82 | + return '.inf'; |
|
83 | + } |
|
84 | + if ($dataType === -INF) { |
|
85 | + return '-.inf'; |
|
86 | + } |
|
81 | 87 | switch (gettype($dataType)) { |
82 | 88 | case 'boolean': return $dataType ? 'true' : 'false'; |
83 | 89 | case 'float': //fall through |
@@ -94,7 +100,9 @@ discard block |
||
94 | 100 | |
95 | 101 | private static function dumpYamlObject(YamlObject $obj) |
96 | 102 | { |
97 | - if ($obj->hasDocStart() && self::$result instanceof DLL) self::$result->push("---"); |
|
103 | + if ($obj->hasDocStart() && self::$result instanceof DLL) { |
|
104 | + self::$result->push("---"); |
|
105 | + } |
|
98 | 106 | // self::dump($obj, 0); |
99 | 107 | if (count($obj) > 0) { |
100 | 108 | self::dumpArray($obj->getArrayCopy(), 0); |
@@ -145,9 +153,13 @@ discard block |
||
145 | 153 | self::add(self::dump($obj->value, $indent + self::INDENT), $indent + self::INDENT); |
146 | 154 | } |
147 | 155 | } |
148 | - if ($obj instanceof Compact) return self::dumpCompact($obj, $indent); |
|
156 | + if ($obj instanceof Compact) { |
|
157 | + return self::dumpCompact($obj, $indent); |
|
158 | + } |
|
149 | 159 | //TODO: consider dumping datetime as date strings according to a format provided by user or default |
150 | - if ($obj instanceof \DateTime) return $obj->format(self::DATE_FORMAT); |
|
160 | + if ($obj instanceof \DateTime) { |
|
161 | + return $obj->format(self::DATE_FORMAT); |
|
162 | + } |
|
151 | 163 | // if ($obj instanceof \SplString) {var_dump('splstrin',$obj);return '"'.$obj.'"';} |
152 | 164 | $propList = get_object_vars($obj);//var_dump($propList); |
153 | 165 | foreach ($propList as $property => $value) { |
@@ -23,9 +23,15 @@ 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((string) $n->value); |
|
28 | - if ($n->type & Y::JSON) return $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((string) $n->value); |
|
31 | + } |
|
32 | + if ($n->type & Y::JSON) { |
|
33 | + return $n->value; |
|
34 | + } |
|
29 | 35 | $expected = [Y::QUOTED => trim((string) $n->value, "\"'"), |
30 | 36 | Y::RAW => strval((string) $n->value)]; |
31 | 37 | return $expected[$n->type] ?? null; |
@@ -41,8 +47,12 @@ discard block |
||
41 | 47 | */ |
42 | 48 | public static function getScalar(string $v) |
43 | 49 | { |
44 | - if (R::isDate($v)) return self::$dateAsString ? $v : date_create($v); |
|
45 | - if (R::isNumber($v)) return self::getNumber($v); |
|
50 | + if (R::isDate($v)) { |
|
51 | + return self::$dateAsString ? $v : date_create($v); |
|
52 | + } |
|
53 | + if (R::isNumber($v)) { |
|
54 | + return self::getNumber($v); |
|
55 | + } |
|
46 | 56 | $types = ['yes' => true, |
47 | 57 | 'no' => false, |
48 | 58 | 'true' => true, |
@@ -64,8 +74,12 @@ discard block |
||
64 | 74 | */ |
65 | 75 | private static function getNumber(string $v) |
66 | 76 | { |
67 | - if (preg_match(R::OCTAL_NUM, $v)) return intval(base_convert($v, 8, 10)); |
|
68 | - if (preg_match(R::HEX_NUM, $v)) return intval(base_convert($v, 16, 10)); |
|
77 | + if (preg_match(R::OCTAL_NUM, $v)) { |
|
78 | + return intval(base_convert($v, 8, 10)); |
|
79 | + } |
|
80 | + if (preg_match(R::HEX_NUM, $v)) { |
|
81 | + return intval(base_convert($v, 16, 10)); |
|
82 | + } |
|
69 | 83 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
70 | 84 | } |
71 | 85 | } |
72 | 86 | \ No newline at end of file |
@@ -17,7 +17,9 @@ discard block |
||
17 | 17 | public static function buildReference($node, $parent) |
18 | 18 | { |
19 | 19 | $tmp = is_null($node->value) ? null : Builder::build($node->value, $parent); |
20 | - if ($node->type === Y::REF_DEF) Builder::$_root->addReference($node->identifier, $tmp); |
|
20 | + if ($node->type === Y::REF_DEF) { |
|
21 | + Builder::$_root->addReference($node->identifier, $tmp); |
|
22 | + } |
|
21 | 23 | return Builder::$_root->getReference($node->identifier); |
22 | 24 | } |
23 | 25 | |
@@ -104,7 +106,9 @@ discard block |
||
104 | 106 | $built = is_object($node->value) ? Builder::build($node->value) : null; |
105 | 107 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
106 | 108 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
107 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
109 | + if (empty($key)) { |
|
110 | + throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
111 | + } |
|
108 | 112 | $parent->{trim($key, '\'" ')} = null; |
109 | 113 | } |
110 | 114 |
@@ -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 | /** |
@@ -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 | } |