@@ -6,13 +6,13 @@ |
||
| 6 | 6 | /** |
| 7 | 7 | * Display some use cases for Yaml library |
| 8 | 8 | */ |
| 9 | -const JSON_OPTIONS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_LINE_TERMINATORS | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_PARTIAL_OUTPUT_ON_ERROR; |
|
| 9 | +const JSON_OPTIONS = JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_LINE_TERMINATORS|JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION|JSON_PARTIAL_OUTPUT_ON_ERROR; |
|
| 10 | 10 | |
| 11 | 11 | /* USE CASE 1 |
| 12 | 12 | * load and parse if file exists |
| 13 | 13 | */ |
| 14 | 14 | ini_set("auto_detect_line_endings", 1); |
| 15 | -$content = file_get_contents('./tests/cases/parsing/multiline_quoted_with_blank.yml');//var_dump($content); |
|
| 15 | +$content = file_get_contents('./tests/cases/parsing/multiline_quoted_with_blank.yml'); //var_dump($content); |
|
| 16 | 16 | // $content = file_get_contents('./tests/cases/examples/Example_2_17.yml');//var_dump($content); |
| 17 | 17 | $yaml = Y::parse($content, null, 2); |
| 18 | 18 | // $yaml = Y::parseFile('./references/Example 2.27.yml', null, 1); |
@@ -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(); |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | $previous->getParent(0)->add($n); |
| 184 | 184 | return true; |
| 185 | 185 | } |
| 186 | - if($n->type & Y::TAG && is_null($n->value) && $previous->type & (Y::ROOT|Y::DOC_START|Y::DOC_END)) { |
|
| 186 | + if ($n->type & Y::TAG && is_null($n->value) && $previous->type & (Y::ROOT|Y::DOC_START|Y::DOC_END)) { |
|
| 187 | 187 | $n->value = ''; |
| 188 | 188 | } |
| 189 | 189 | return false; |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | if ((($deepest->type & (Y::LITTERALS|Y::REF_DEF|Y::SET_VALUE)) && |
| 204 | 204 | is_null($deepest->value)) //&& |
| 205 | 205 | /*!($previous->type & Y::ROOT)*/ || |
| 206 | - ($deepest->type & Y::TAG && is_null($deepest->value)) ) { |
|
| 206 | + ($deepest->type & Y::TAG && is_null($deepest->value))) { |
|
| 207 | 207 | // var_dump(Y::getName($previous->type)); |
| 208 | 208 | $previous = $deepest; |
| 209 | 209 | } |
@@ -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 | - $count = preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1,-1)), $matches); |
|
| 265 | + $count = 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 | } |
@@ -129,32 +129,32 @@ discard block |
||
| 129 | 129 | * |
| 130 | 130 | * @return mixed The parent (object|array) or a string representing the NodeList. |
| 131 | 131 | */ |
| 132 | - private static function buildNodeList(NodeList $node, &$parent=null) |
|
| 132 | + private static function buildNodeList(NodeList $node, &$parent = null) |
|
| 133 | 133 | { |
| 134 | 134 | if (is_null($node->type)) { |
| 135 | - $childTypes = $node->getTypes(); |
|
| 136 | - if ($childTypes & (Y::KEY|Y::SET_KEY)) { |
|
| 137 | - if ($childTypes & Y::ITEM) { |
|
| 135 | + $childTypes = $node->getTypes(); |
|
| 136 | + if ($childTypes&(Y::KEY|Y::SET_KEY)) { |
|
| 137 | + if ($childTypes&Y::ITEM) { |
|
| 138 | 138 | // TODO: replace the document index in HERE ----------v |
| 139 | 139 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, 0)); |
| 140 | 140 | } else { |
| 141 | 141 | $node->type = Y::MAPPING; |
| 142 | 142 | } |
| 143 | 143 | } else { |
| 144 | - if ($childTypes & Y::ITEM) { |
|
| 144 | + if ($childTypes&Y::ITEM) { |
|
| 145 | 145 | $node->type = Y::SEQUENCE; |
| 146 | - } elseif (!($childTypes & Y::COMMENT)) |
|
| 146 | + } elseif (!($childTypes&Y::COMMENT)) |
|
| 147 | 147 | { |
| 148 | 148 | $node->type = Y::LITT_FOLDED; |
| 149 | 149 | } |
| 150 | 150 | } |
| 151 | 151 | } |
| 152 | 152 | // var_dump('nodetype:'.Y::getName($node->type) ); |
| 153 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
| 153 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
| 154 | 154 | return self::buildLitteral($node, $node->type); |
| 155 | 155 | } |
| 156 | 156 | if ($node->type & (Y::COMPACT_MAPPING|Y::MAPPING|Y::SET)) { |
| 157 | - $out = $parent ?? new \StdClass;//var_dump("PAS LA"); |
|
| 157 | + $out = $parent ?? new \StdClass; //var_dump("PAS LA"); |
|
| 158 | 158 | foreach ($node as $key => $child) { |
| 159 | 159 | if ($child->type & (Y::KEY)) { |
| 160 | 160 | self::buildKey($child, $out); |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | } |
| 164 | 164 | } |
| 165 | 165 | } elseif ($node->type & (Y::COMPACT_SEQUENCE|Y::SEQUENCE)) { |
| 166 | - $out = $parent ?? [];//var_dump("HERE"); |
|
| 166 | + $out = $parent ?? []; //var_dump("HERE"); |
|
| 167 | 167 | foreach ($node as $key => $child) { |
| 168 | 168 | if ($child->type & Y::ITEM) { |
| 169 | 169 | self::buildItem($child, $out); |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | } |
| 173 | 173 | } |
| 174 | 174 | } else { |
| 175 | - $tmpString = null;//var_dump("PAS ICI"); |
|
| 175 | + $tmpString = null; //var_dump("PAS ICI"); |
|
| 176 | 176 | foreach ($node as $key => $child) { |
| 177 | 177 | if ($child->type & (Y::SCALAR|Y::QUOTED)) { |
| 178 | 178 | if ($parent) { |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | private static function buildNode(Node $node, &$parent) |
| 201 | 201 | { |
| 202 | 202 | extract((array) $node, EXTR_REFS); |
| 203 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
| 203 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
| 204 | 204 | if (is_object($value)) { |
| 205 | 205 | $tmp = self::build($value, $parent) ?? $parent; |
| 206 | 206 | } else { |
@@ -209,10 +209,10 @@ discard block |
||
| 209 | 209 | if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
| 210 | 210 | return self::$_root->getReference($identifier); |
| 211 | 211 | } |
| 212 | - if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
| 212 | + if ($type&(Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
| 213 | 213 | return self::buildNodeList($node->value, $parent); |
| 214 | 214 | } |
| 215 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
| 215 | + if ($type&Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
| 216 | 216 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
| 217 | 217 | Y::ITEM => 'buildItem', |
| 218 | 218 | Y::KEY => 'buildKey', |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
| 236 | 236 | * @return null |
| 237 | 237 | */ |
| 238 | - private static function buildKey(Node $node, &$parent=null) |
|
| 238 | + private static function buildKey(Node $node, &$parent = null) |
|
| 239 | 239 | { |
| 240 | 240 | extract((array) $node, EXTR_REFS); |
| 241 | 241 | if (is_null($identifier)) { |
@@ -253,7 +253,7 @@ discard block |
||
| 253 | 253 | } |
| 254 | 254 | if ($value instanceof NodeList) { |
| 255 | 255 | $childTypes = $value->getTypes(); |
| 256 | - if (is_null($value->type) && $childTypes & Y::SCALAR && !($childTypes & Y::COMMENT)) { |
|
| 256 | + if (is_null($value->type) && $childTypes&Y::SCALAR && !($childTypes&Y::COMMENT)) { |
|
| 257 | 257 | $result = self::buildLitteral($value, Y::LITT_FOLDED); |
| 258 | 258 | } else { |
| 259 | 259 | $result = self::buildNodeList($value); |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | */ |
| 283 | 283 | private static function buildItem(Node $node, &$parent) |
| 284 | 284 | { |
| 285 | - extract((array) $node, EXTR_REFS);//var_dump(__METHOD__); |
|
| 285 | + extract((array) $node, EXTR_REFS); //var_dump(__METHOD__); |
|
| 286 | 286 | if (!is_array($parent) && !($parent instanceof \ArrayIterator)) { |
| 287 | 287 | throw new \Exception("parent must be an Iterable not ".(is_object($parent) ? get_class($parent) : gettype($parent)), 1); |
| 288 | 288 | } |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | $numKeys = array_filter(array_keys($ref), 'is_int'); |
| 291 | 291 | $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0; |
| 292 | 292 | if ($value instanceof Node) { |
| 293 | - if($value->type & Y::KEY) { |
|
| 293 | + if ($value->type & Y::KEY) { |
|
| 294 | 294 | self::buildKey($node->value, $parent); |
| 295 | 295 | return; |
| 296 | 296 | } else if ($value->type & Y::ITEM) { |
@@ -334,13 +334,13 @@ discard block |
||
| 334 | 334 | $lines[] = self::buildLitteral($child->value, $type); |
| 335 | 335 | } else { |
| 336 | 336 | $prefix = ''; |
| 337 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 337 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 338 | 338 | $prefix = "\n"; |
| 339 | 339 | } |
| 340 | 340 | if (!($child->type & (Y::SCALAR|Y::BLANK))) { |
| 341 | 341 | switch ($child->type) { |
| 342 | - case Y::ITEM: $child->value = '- '.$child->value;break; |
|
| 343 | - case Y::COMMENT: $child->value = '# '.$child->value;break; |
|
| 342 | + case Y::ITEM: $child->value = '- '.$child->value; break; |
|
| 343 | + case Y::COMMENT: $child->value = '# '.$child->value; break; |
|
| 344 | 344 | default: //die(__METHOD__.Y::getName($child->type)); |
| 345 | 345 | } |
| 346 | 346 | } |
@@ -351,9 +351,9 @@ discard block |
||
| 351 | 351 | $lines[] = $prefix.$val; |
| 352 | 352 | } |
| 353 | 353 | } |
| 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)); |
|
| 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)); |
|
| 357 | 357 | return ''; |
| 358 | 358 | } |
| 359 | 359 | |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | private function buildSetKey(Node $node, &$parent) |
| 369 | 369 | { |
| 370 | 370 | $built = self::build($node->value); |
| 371 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
| 371 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
| 372 | 372 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 373 | 373 | // if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
| 374 | 374 | $parent->{trim($key, '\'" ')} = null; |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | foreach ($array as $key => $item) { |
| 117 | 117 | $lineStart = current($refKeys) === $key ? "- " : "- $key: "; |
| 118 | 118 | if (is_scalar($item)) { |
| 119 | - self::add($lineStart.self::dump($item,0), $indent); |
|
| 119 | + self::add($lineStart.self::dump($item, 0), $indent); |
|
| 120 | 120 | } else { |
| 121 | 121 | self::add($lineStart, $indent); |
| 122 | 122 | self::dump($item, $indent + self::INDENT); |
@@ -163,10 +163,10 @@ discard block |
||
| 163 | 163 | if (is_array($subject) || $subject instanceof \Countable) { |
| 164 | 164 | $max = count($subject); |
| 165 | 165 | $objectAsArray = is_array($subject) ? $subject : $subject->getArrayCopy(); |
| 166 | - if(array_keys($objectAsArray) !== range(0, $max)) { |
|
| 166 | + if (array_keys($objectAsArray) !== range(0, $max)) { |
|
| 167 | 167 | $pairs = $objectAsArray; |
| 168 | 168 | } else { |
| 169 | - $valuesList = array_map([self, 'dump'], $objectAsArray, array_fill( 0 , $max , $indent )); |
|
| 169 | + $valuesList = array_map([self, 'dump'], $objectAsArray, array_fill(0, $max, $indent)); |
|
| 170 | 170 | return '['.implode(', ', $valuesList).']'; |
| 171 | 171 | } |
| 172 | 172 | } else { |
@@ -24,10 +24,10 @@ discard block |
||
| 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::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) |
| 29 | 29 | // return self::getCompact(substr($n->value, 1, -1), $n->type); |
| 30 | - if ($n->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE| Y::JSON)) { |
|
| 30 | + if ($n->type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE|Y::JSON)) { |
|
| 31 | 31 | return $n->value; |
| 32 | 32 | } |
| 33 | 33 | $expected = [//Y::JSON => $n->value, //json_decode($n->value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | private function getArray($sequenceString) |
| 101 | 101 | { |
| 102 | 102 | $out = []; |
| 103 | - $f = function ($e) { return self::getScalar(trim($e));}; |
|
| 103 | + $f = function($e) { return self::getScalar(trim($e)); }; |
|
| 104 | 104 | foreach (array_map($f, explode(",", $sequenceString)) as $key => $value) { |
| 105 | 105 | $out[$key] = $value; |
| 106 | 106 | } |
@@ -14,9 +14,9 @@ |
||
| 14 | 14 | |
| 15 | 15 | public function __construct($candidate) |
| 16 | 16 | { |
| 17 | - if ($candidate){ |
|
| 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 | } |