@@ -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 | /** |
@@ -19,17 +19,17 @@ discard block |
||
19 | 19 | /* @var null|string */ |
20 | 20 | public static $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 |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | $previous = $root = new Node(); |
93 | 93 | $emptyLines = []; |
94 | 94 | try { //var_dump($source); |
95 | - $gen = function () use($source) { |
|
95 | + $gen = function() use($source) { |
|
96 | 96 | foreach ($source as $key => $value) { |
97 | 97 | yield ++$key => $value; |
98 | 98 | } |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | case 0: |
120 | 120 | if ($n->type & Y::KEY && $n->indent === 0) { |
121 | 121 | $target = $root; |
122 | - } elseif($n->type & Y::ITEM && $deepest->type & Y::KEY && is_null($deepest->value)) { |
|
122 | + } elseif ($n->type & Y::ITEM && $deepest->type & Y::KEY && is_null($deepest->value)) { |
|
123 | 123 | $target = $deepest; |
124 | 124 | } else { |
125 | 125 | $target = $previous->getParent(); |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | return; |
190 | 190 | } |
191 | 191 | // Note : php don't like '?' as an array key -_-' |
192 | - if(in_array($first, ['?', ':'])) { |
|
192 | + if (in_array($first, ['?', ':'])) { |
|
193 | 193 | $this->type = $first === '?' ? Y::SET_KEY : Y::SET_VALUE; |
194 | 194 | if (!empty(trim($v))) { |
195 | 195 | $this->value = new NodeList; |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | $this->onHyphen($nodeValue); |
202 | 202 | return; |
203 | 203 | } |
204 | - $characters = [ '#' => [Y::COMMENT, ltrim($v)], |
|
204 | + $characters = ['#' => [Y::COMMENT, ltrim($v)], |
|
205 | 205 | '%' => [Y::DIRECTIVE, ltrim($v)], |
206 | 206 | '>' => [Y::LITT_FOLDED, null], |
207 | 207 | '|' => [Y::LITT, null] |
@@ -254,28 +254,28 @@ discard block |
||
254 | 254 | private function onCompact($value) |
255 | 255 | { |
256 | 256 | $this->value = json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
257 | - if (json_last_error() === JSON_ERROR_NONE){ |
|
257 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
258 | 258 | $this->type = Y::JSON; |
259 | 259 | return; |
260 | 260 | } |
261 | 261 | $this->value = new NodeList(); |
262 | - if (preg_match(R::MAPPING, $value)){ |
|
262 | + if (preg_match(R::MAPPING, $value)) { |
|
263 | 263 | $this->type = Y::COMPACT_MAPPING; |
264 | 264 | $this->value->type = Y::COMPACT_MAPPING; |
265 | - preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1,-1)), $matches); |
|
265 | + preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1, -1)), $matches); |
|
266 | 266 | foreach ($matches['k'] as $index => $property) { |
267 | 267 | $n = new Node('', $this->line); |
268 | 268 | $n->type = Y::KEY; |
269 | - $n->identifier = trim($property, '"\' ');//TODO : maybe check for proper quoting first ? |
|
269 | + $n->identifier = trim($property, '"\' '); //TODO : maybe check for proper quoting first ? |
|
270 | 270 | $n->value = new Node($matches['v'][$index], $this->line); |
271 | 271 | $this->value->push($n); |
272 | 272 | } |
273 | 273 | return; |
274 | 274 | } |
275 | - if (preg_match(R::SEQUENCE, $value)){ |
|
275 | + if (preg_match(R::SEQUENCE, $value)) { |
|
276 | 276 | $this->type = Y::COMPACT_SEQUENCE; |
277 | 277 | $this->value->type = Y::COMPACT_SEQUENCE; |
278 | - $count = preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1,-1)), $matches); |
|
278 | + $count = preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1, -1)), $matches); |
|
279 | 279 | foreach ($matches['item'] as $key => $item) { |
280 | 280 | $i = new Node('', $this->line); |
281 | 281 | $i->type = Y::ITEM; |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | $q->push($_root->value); |
39 | 39 | // return self::buildNodeList($q, new YamlObject); |
40 | 40 | self::$_root = new YamlObject; |
41 | - $tmp = self::buildNodeList($q, self::$_root); |
|
41 | + $tmp = self::buildNodeList($q, self::$_root); |
|
42 | 42 | // var_dump('alone', $tmp); |
43 | 43 | return $tmp; |
44 | 44 | } |
@@ -87,31 +87,31 @@ discard block |
||
87 | 87 | * |
88 | 88 | * @return mixed The parent (object|array) or a string representing the NodeList. |
89 | 89 | */ |
90 | - private static function buildNodeList(NodeList $node, &$parent=null) |
|
90 | + private static function buildNodeList(NodeList $node, &$parent = null) |
|
91 | 91 | { |
92 | 92 | if (is_null($node->type)) { |
93 | - $childTypes = $node->getTypes(); |
|
94 | - if ($childTypes & (Y::KEY|Y::SET_KEY)) { |
|
95 | - if ($childTypes & Y::ITEM) { |
|
93 | + $childTypes = $node->getTypes(); |
|
94 | + if ($childTypes&(Y::KEY|Y::SET_KEY)) { |
|
95 | + if ($childTypes&Y::ITEM) { |
|
96 | 96 | // TODO: replace the document index in HERE ----------v |
97 | 97 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, 0)); |
98 | 98 | } else { |
99 | 99 | $node->type = Y::MAPPING; |
100 | 100 | } |
101 | 101 | } else { |
102 | - if ($childTypes & Y::ITEM) { |
|
102 | + if ($childTypes&Y::ITEM) { |
|
103 | 103 | $node->type = Y::SEQUENCE; |
104 | - } elseif (!($childTypes & Y::COMMENT)) { |
|
104 | + } elseif (!($childTypes&Y::COMMENT)) { |
|
105 | 105 | $node->type = Y::LITT_FOLDED; |
106 | 106 | } |
107 | 107 | } |
108 | 108 | } |
109 | 109 | // var_dump('nodetype:'.Y::getName($node->type) ); |
110 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
110 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
111 | 111 | return self::buildLitteral($node, $node->type); |
112 | 112 | } |
113 | 113 | if ($node->type & (Y::COMPACT_MAPPING|Y::MAPPING|Y::SET)) { |
114 | - $out = $parent ?? new \StdClass;//var_dump("PAS LA"); |
|
114 | + $out = $parent ?? new \StdClass; //var_dump("PAS LA"); |
|
115 | 115 | foreach ($node as $key => $child) { |
116 | 116 | if ($child->type & (Y::KEY)) { |
117 | 117 | self::buildKey($child, $out); |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | $out = new Compact($out); |
124 | 124 | } |
125 | 125 | } elseif ($node->type & (Y::COMPACT_SEQUENCE|Y::SEQUENCE)) { |
126 | - $out = $parent ?? [];//var_dump("HERE"); |
|
126 | + $out = $parent ?? []; //var_dump("HERE"); |
|
127 | 127 | foreach ($node as $key => $child) { |
128 | 128 | if ($child->type & Y::ITEM) { |
129 | 129 | self::buildItem($child, $out); |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | $out = new Compact($out); |
136 | 136 | } |
137 | 137 | } else { |
138 | - $tmpString = null;//var_dump("PAS ICI"); |
|
138 | + $tmpString = null; //var_dump("PAS ICI"); |
|
139 | 139 | foreach ($node as $key => $child) { |
140 | 140 | if ($child->type & (Y::SCALAR|Y::QUOTED)) { |
141 | 141 | if ($parent) { |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | private static function buildNode(Node $node, &$parent) |
164 | 164 | { |
165 | 165 | extract((array) $node, EXTR_REFS); |
166 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
166 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
167 | 167 | if (is_object($value)) { |
168 | 168 | $tmp = self::build($value, $parent) ?? $parent; |
169 | 169 | } else { |
@@ -172,10 +172,10 @@ discard block |
||
172 | 172 | if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
173 | 173 | return self::$_root->getReference($identifier); |
174 | 174 | } |
175 | - if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
175 | + if ($type&(Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
176 | 176 | return self::buildNodeList($node->value, $parent); |
177 | 177 | } |
178 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
178 | + if ($type&Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
179 | 179 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
180 | 180 | Y::ITEM => 'buildItem', |
181 | 181 | Y::KEY => 'buildKey', |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
199 | 199 | * @return null |
200 | 200 | */ |
201 | - private static function buildKey(Node $node, &$parent=null) |
|
201 | + private static function buildKey(Node $node, &$parent = null) |
|
202 | 202 | { |
203 | 203 | extract((array) $node, EXTR_REFS); |
204 | 204 | if (is_null($identifier)) { |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | } |
217 | 217 | if ($value instanceof NodeList) { |
218 | 218 | $childTypes = $value->getTypes(); |
219 | - if (is_null($value->type) && $childTypes & Y::SCALAR && !($childTypes & Y::COMMENT)) { |
|
219 | + if (is_null($value->type) && $childTypes&Y::SCALAR && !($childTypes&Y::COMMENT)) { |
|
220 | 220 | $result = self::buildLitteral($value, Y::LITT_FOLDED); |
221 | 221 | } else { |
222 | 222 | $result = self::buildNodeList($value); |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | */ |
246 | 246 | private static function buildItem(Node $node, &$parent) |
247 | 247 | { |
248 | - extract((array) $node, EXTR_REFS);//var_dump(__METHOD__); |
|
248 | + extract((array) $node, EXTR_REFS); //var_dump(__METHOD__); |
|
249 | 249 | if (!is_array($parent) && !($parent instanceof \ArrayIterator)) { |
250 | 250 | throw new \Exception("parent must be an Iterable not ".(is_object($parent) ? get_class($parent) : gettype($parent)), 1); |
251 | 251 | } |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | $numKeys = array_filter(array_keys($ref), 'is_int'); |
254 | 254 | $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0; |
255 | 255 | if ($value instanceof Node) { |
256 | - if($value->type & Y::KEY) { |
|
256 | + if ($value->type & Y::KEY) { |
|
257 | 257 | self::buildKey($node->value, $parent); |
258 | 258 | return; |
259 | 259 | } elseif ($value->type & Y::ITEM) { |
@@ -297,13 +297,13 @@ discard block |
||
297 | 297 | $lines[] = self::buildLitteral($child->value, $type); |
298 | 298 | } else { |
299 | 299 | $prefix = ''; |
300 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
300 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
301 | 301 | $prefix = "\n"; |
302 | 302 | } |
303 | 303 | if (!($child->type & (Y::SCALAR|Y::BLANK))) { |
304 | 304 | switch ($child->type) { |
305 | - case Y::ITEM: $child->value = '- '.$child->value;break; |
|
306 | - case Y::COMMENT: $child->value = '# '.$child->value;break; |
|
305 | + case Y::ITEM: $child->value = '- '.$child->value; break; |
|
306 | + case Y::COMMENT: $child->value = '# '.$child->value; break; |
|
307 | 307 | default: //die(__METHOD__.Y::getName($child->type)); |
308 | 308 | } |
309 | 309 | } |
@@ -314,9 +314,9 @@ discard block |
||
314 | 314 | $lines[] = $prefix.$val; |
315 | 315 | } |
316 | 316 | } |
317 | - if ($type & Y::RAW) return implode('', $lines); |
|
318 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
319 | - if ($type & Y::LITT_FOLDED) return preg_replace(['/ +(\n)/','/(\n+) +/'], "$1", implode(' ', $lines)); |
|
317 | + if ($type&Y::RAW) return implode('', $lines); |
|
318 | + if ($type&Y::LITT) return implode("\n", $lines); |
|
319 | + if ($type&Y::LITT_FOLDED) return preg_replace(['/ +(\n)/', '/(\n+) +/'], "$1", implode(' ', $lines)); |
|
320 | 320 | return ''; |
321 | 321 | } |
322 | 322 | |
@@ -331,7 +331,7 @@ discard block |
||
331 | 331 | private function buildSetKey(Node $node, &$parent) |
332 | 332 | { |
333 | 333 | $built = is_object($node->value) ? self::build($node->value) : null; |
334 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
334 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
335 | 335 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
336 | 336 | // if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
337 | 337 | $parent->{trim($key, '\'" ')} = null; |
@@ -29,7 +29,7 @@ |
||
29 | 29 | return $n->value; |
30 | 30 | } |
31 | 31 | $expected = [Y::QUOTED => trim($n->value, "\"'"), |
32 | - Y::RAW => strval($n->value)]; |
|
32 | + Y::RAW => strval($n->value)]; |
|
33 | 33 | return $expected[$n->type] ?? null; |
34 | 34 | } |
35 | 35 |
@@ -24,7 +24,7 @@ |
||
24 | 24 | public static function get(Node $n) |
25 | 25 | { |
26 | 26 | if (is_null($n->value)) return null; |
27 | - if ($n->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($n->value); |
|
27 | + if ($n->type & (Y::REF_CALL|Y::SCALAR)) return self::getScalar($n->value); |
|
28 | 28 | if ($n->type & Y::JSON) { |
29 | 29 | return $n->value; |
30 | 30 | } |
@@ -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::JSON) { |
29 | 33 | return $n->value; |
30 | 34 | } |
@@ -43,8 +47,12 @@ discard block |
||
43 | 47 | */ |
44 | 48 | private static function getScalar(string $v) |
45 | 49 | { |
46 | - if (R::isDate($v)) return date_create($v); |
|
47 | - if (R::isNumber($v)) return self::getNumber($v); |
|
50 | + if (R::isDate($v)) { |
|
51 | + return date_create($v); |
|
52 | + } |
|
53 | + if (R::isNumber($v)) { |
|
54 | + return self::getNumber($v); |
|
55 | + } |
|
48 | 56 | $types = ['yes' => true, |
49 | 57 | 'no' => false, |
50 | 58 | 'true' => true, |
@@ -66,8 +74,12 @@ discard block |
||
66 | 74 | */ |
67 | 75 | private static function getNumber(string $v) |
68 | 76 | { |
69 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
70 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
77 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
78 | + return intval(base_convert($v, 8, 10)); |
|
79 | + } |
|
80 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
81 | + return intval(base_convert($v, 16, 10)); |
|
82 | + } |
|
71 | 83 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
72 | 84 | } |
73 | 85 | } |
74 | 86 | \ No newline at end of file |