@@ -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 | } |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | private static function buildNodeList(NodeList $node, &$parent) |
30 | 30 | { |
31 | 31 | $type = &$node->type; |
32 | - if ($type & (Y::RAW | Y::LITTERALS)) { |
|
32 | + if ($type&(Y::RAW|Y::LITTERALS)) { |
|
33 | 33 | return self::litteral($node, $type); |
34 | 34 | } |
35 | 35 | $p = $parent; |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | private static function buildNode(Node $node, &$parent) |
57 | 57 | { |
58 | 58 | extract((array) $node, EXTR_REFS); |
59 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
59 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
60 | 60 | if (is_object($value)) { |
61 | 61 | $tmp = self::build($value, $parent) ?? $parent; |
62 | 62 | } else { |
@@ -176,15 +176,15 @@ discard block |
||
176 | 176 | { |
177 | 177 | self::$_root = new YamlObject(); |
178 | 178 | $childTypes = $list->getTypes(); |
179 | - $isMapping = (bool) (Y::KEY | Y::MAPPING) & $childTypes; |
|
179 | + $isMapping = (bool) (Y::KEY|Y::MAPPING)&$childTypes; |
|
180 | 180 | $isSequence = (bool) Y::ITEM & $childTypes; |
181 | 181 | $isSet = (bool) Y::SET_VALUE & $childTypes; |
182 | 182 | if ($isMapping && $isSequence) { |
183 | 183 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
184 | 184 | } else { |
185 | 185 | switch (true) { |
186 | - case $isSequence: $list->type = Y::SEQUENCE;break; |
|
187 | - case $isSet: $list->type = Y::SET;break; |
|
186 | + case $isSequence: $list->type = Y::SEQUENCE; break; |
|
187 | + case $isSet: $list->type = Y::SET; break; |
|
188 | 188 | default: $list->type = Y::MAPPING; |
189 | 189 | } |
190 | 190 | } |
@@ -206,11 +206,11 @@ discard block |
||
206 | 206 | $children->rewind(); |
207 | 207 | $refIndent = $children->current()->indent; |
208 | 208 | $separator = $type === Y::RAW ? '' : "\n"; |
209 | - $action = function ($c) { return (string) $c->value; }; |
|
209 | + $action = function($c) { return (string) $c->value; }; |
|
210 | 210 | //TODO: use iterator_to_array for LITT & RAW |
211 | - if ($type & Y::LITT_FOLDED) { |
|
211 | + if ($type&Y::LITT_FOLDED) { |
|
212 | 212 | $separator = ' '; |
213 | - $action = function ($c) use ($refIndent) { |
|
213 | + $action = function($c) use ($refIndent) { |
|
214 | 214 | return $c->indent > $refIndent || ($c->type & Y::BLANK) ? "\n".$c->value : $c->value; |
215 | 215 | }; |
216 | 216 | } |
@@ -22,7 +22,9 @@ discard block |
||
22 | 22 | |
23 | 23 | private static function build(object $node, &$parent = null) |
24 | 24 | { |
25 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
25 | + if ($node instanceof NodeList) { |
|
26 | + return self::buildNodeList($node, $parent); |
|
27 | + } |
|
26 | 28 | return self::buildNode($node, $parent); |
27 | 29 | } |
28 | 30 | |
@@ -62,7 +64,9 @@ discard block |
||
62 | 64 | } else { |
63 | 65 | $tmp = $node->getPhpValue(); |
64 | 66 | } |
65 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
67 | + if ($type === Y::REF_DEF) { |
|
68 | + self::$_root->addReference($identifier, $tmp); |
|
69 | + } |
|
66 | 70 | return self::$_root->getReference($identifier); |
67 | 71 | } |
68 | 72 | switch ($type) { |
@@ -72,8 +76,9 @@ discard block |
||
72 | 76 | case Y::KEY: self::buildKey($node, $parent); return; |
73 | 77 | case Y::SET_KEY: |
74 | 78 | $key = json_encode(self::build($value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
75 | - if (empty($key)) |
|
76 | - throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
|
79 | + if (empty($key)) { |
|
80 | + throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
|
81 | + } |
|
77 | 82 | $parent->{$key} = null; |
78 | 83 | return; |
79 | 84 | case Y::SET_VALUE: |
@@ -92,8 +97,11 @@ discard block |
||
92 | 97 | $parent->addTag($identifier); return; |
93 | 98 | } else {//TODO: have somewhere a list of common tags and their treatment |
94 | 99 | if (in_array($identifier, ['!binary', '!str'])) { |
95 | - if ($value->value instanceof NodeList) $value->value->type = Y::RAW; |
|
96 | - else $value->type = Y::RAW; |
|
100 | + if ($value->value instanceof NodeList) { |
|
101 | + $value->value->type = Y::RAW; |
|
102 | + } else { |
|
103 | + $value->type = Y::RAW; |
|
104 | + } |
|
97 | 105 | } |
98 | 106 | $val = is_null($value) ? null : self::build(/** @scrutinizer ignore-type */ $value, $node); |
99 | 107 | return new Tag($identifier, $val); |
@@ -162,10 +170,14 @@ discard block |
||
162 | 170 | } |
163 | 171 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
164 | 172 | foreach ($_root->value as $child) { |
165 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
173 | + if ($child->type & Y::DOC_START) { |
|
174 | + $totalDocStart++; |
|
175 | + } |
|
166 | 176 | //if 0 or 1 DOC_START = we are still in first document |
167 | 177 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
168 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
178 | + if (!isset($documents[$currentDoc])) { |
|
179 | + $documents[$currentDoc] = new NodeList(); |
|
180 | + } |
|
169 | 181 | $documents[$currentDoc]->push($child); |
170 | 182 | } |
171 | 183 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
@@ -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; |
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; |
@@ -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) { |
@@ -263,8 +263,8 @@ discard block |
||
263 | 263 | { |
264 | 264 | $v = &$this->value; |
265 | 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); |
|
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); |
|
268 | 268 | switch ($this->type) { |
269 | 269 | case Y::JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
270 | 270 | case Y::QUOTED: return substr($v, 1, -1); |
@@ -320,7 +320,7 @@ discard block |
||
320 | 320 | { |
321 | 321 | $out = new Compact(); |
322 | 322 | if ($type === Y::COMPACT_SEQUENCE) { |
323 | - $f = function ($e) { return self::getScalar(trim($e));}; |
|
323 | + $f = function($e) { return self::getScalar(trim($e)); }; |
|
324 | 324 | //TODO : that's not robust enough, improve it |
325 | 325 | foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) { |
326 | 326 | $out[$key] = $value; |
@@ -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; |