@@ -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, 3); |
18 | 18 | // $yaml = Y::parseFile('./references/Example 2.27.yml', null, 1); |
@@ -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 \ArrayIterator) { |
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 { |
@@ -17,10 +17,10 @@ discard block |
||
17 | 17 | //public |
18 | 18 | /* @var null|string */ |
19 | 19 | public static $error; |
20 | - public const IGNORE_DIRECTIVES = 1;//DONT include_directive |
|
21 | - public const IGNORE_COMMENTS = 2;//DONT include_comments |
|
22 | - public const NO_PARSING_EXCEPTIONS = 4;//DONT throw Exception on parsing errors |
|
23 | - public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
|
20 | + public const IGNORE_DIRECTIVES = 1; //DONT include_directive |
|
21 | + public const IGNORE_COMMENTS = 2; //DONT include_comments |
|
22 | + public const NO_PARSING_EXCEPTIONS = 4; //DONT throw Exception on parsing errors |
|
23 | + public const NO_OBJECT_FOR_DATE = 8; //DONT import date strings as dateTime Object |
|
24 | 24 | |
25 | 25 | //privates |
26 | 26 | /* @var null|false|array */ |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | /* @var null|string */ |
29 | 29 | private $filePath; |
30 | 30 | /* @var integer */ |
31 | - private $debug = 0;///TODO: determine levels |
|
31 | + private $debug = 0; ///TODO: determine levels |
|
32 | 32 | /* @var integer */ |
33 | 33 | private $options = 0; |
34 | 34 | //Exceptions messages |
@@ -78,7 +78,7 @@ discard block |
||
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 | 80 | if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
81 | - return function () use($source) { |
|
81 | + return function() use($source) { |
|
82 | 82 | foreach ($source as $key => $value) { |
83 | 83 | yield ++$key => $value; |
84 | 84 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | case 0: |
121 | 121 | if ($n->type & Y::KEY && $n->indent === 0) { |
122 | 122 | $target = $root; |
123 | - } elseif($n->type & Y::ITEM && $deepest->type & Y::KEY && is_null($deepest->value)) { |
|
123 | + } elseif ($n->type & Y::ITEM && $deepest->type & Y::KEY && is_null($deepest->value)) { |
|
124 | 124 | $target = $deepest; |
125 | 125 | } else { |
126 | 126 | $target = $previous->getParent(); |
@@ -45,16 +45,16 @@ |
||
45 | 45 | public function forceType() |
46 | 46 | { |
47 | 47 | if (is_null($this->type)) { |
48 | - $childTypes = $this->getTypes(); |
|
49 | - if ($childTypes & (Y::KEY|Y::SET_KEY)) { |
|
50 | - if ($childTypes & Y::ITEM) { |
|
48 | + $childTypes = $this->getTypes(); |
|
49 | + if ($childTypes&(Y::KEY|Y::SET_KEY)) { |
|
50 | + if ($childTypes&Y::ITEM) { |
|
51 | 51 | throw new \ParseError(self::class.": Error conflicting types found"); |
52 | 52 | } |
53 | 53 | $this->type = Y::MAPPING; |
54 | 54 | } else { |
55 | - if ($childTypes & Y::ITEM) { |
|
55 | + if ($childTypes&Y::ITEM) { |
|
56 | 56 | $this->type = Y::SEQUENCE; |
57 | - } elseif (!($childTypes & Y::COMMENT)) { |
|
57 | + } elseif (!($childTypes&Y::COMMENT)) { |
|
58 | 58 | $this->type = Y::LITT_FOLDED; |
59 | 59 | } |
60 | 60 | } |
@@ -43,9 +43,9 @@ |
||
43 | 43 | public function __call($funcName, $arguments) |
44 | 44 | { |
45 | 45 | $reflectAPI = new \ReflectionClass(get_class($this->__yaml__object__api)); |
46 | - $getName = function ($o) { return $o->name; }; |
|
46 | + $getName = function($o) { return $o->name; }; |
|
47 | 47 | $publicApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PUBLIC)); |
48 | - if (!in_array($funcName, $publicApi) ) { |
|
48 | + if (!in_array($funcName, $publicApi)) { |
|
49 | 49 | throw new \BadMethodCallException(sprintf(self::UNDEFINED_METHOD, $funcName, implode(",", $publicApi)), 1); |
50 | 50 | } |
51 | 51 | return call_user_func_array([$this->__yaml__object__api, $funcName], $arguments); |
@@ -32,7 +32,7 @@ |
||
32 | 32 | $prop = get_object_vars($this); |
33 | 33 | if (count($prop) > 0) return $prop; |
34 | 34 | if (count($this) > 0) return iterator_to_array($this); |
35 | - return $this;//new \Stdclass; |
|
35 | + return $this; //new \Stdclass; |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | /** |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | elseif (in_array($first, ['?', ':'])) $this->onSetElement($nodeValue); |
186 | 186 | elseif (in_array($first, ['!', '&', '*'])) $this->onNodeAction($nodeValue); |
187 | 187 | else { |
188 | - $characters = [ '#' => [Y::COMMENT, $v], |
|
188 | + $characters = ['#' => [Y::COMMENT, $v], |
|
189 | 189 | '%' => [Y::DIRECTIVE, $v], |
190 | 190 | '>' => [Y::LITT_FOLDED, null], |
191 | 191 | '|' => [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; |
@@ -75,13 +75,13 @@ discard block |
||
75 | 75 | * |
76 | 76 | * @return mixed The parent (object|array) or a string representing the NodeList. |
77 | 77 | */ |
78 | - private static function buildNodeList(NodeList $node, &$parent=null) |
|
78 | + private static function buildNodeList(NodeList $node, &$parent = null) |
|
79 | 79 | { |
80 | 80 | $node->forceType(); |
81 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
81 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
82 | 82 | return self::buildLitteral($node, (int) $node->type); |
83 | 83 | } |
84 | - $action = function ($child, &$parent, &$out) { |
|
84 | + $action = function($child, &$parent, &$out) { |
|
85 | 85 | self::build($child, $out); |
86 | 86 | }; |
87 | 87 | if ($node->type & (Y::COMPACT_MAPPING|Y::MAPPING|Y::SET)) { |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | $out = $parent ?? []; |
91 | 91 | } else { |
92 | 92 | $out = ''; |
93 | - $action = function ($child, &$parent, &$out) { |
|
93 | + $action = function($child, &$parent, &$out) { |
|
94 | 94 | if ($child->type & (Y::SCALAR|Y::QUOTED)) { |
95 | 95 | if ($parent) { |
96 | 96 | $parent->setText(self::build($child)); |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | private static function buildNode(Node $node, &$parent) |
121 | 121 | { |
122 | 122 | extract((array) $node, EXTR_REFS); |
123 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
123 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
124 | 124 | if (is_object($value)) { |
125 | 125 | $tmp = self::build($value, $parent) ?? $parent; |
126 | 126 | } else { |
@@ -129,10 +129,10 @@ discard block |
||
129 | 129 | if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
130 | 130 | return self::$_root->getReference($identifier); |
131 | 131 | } |
132 | - if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
132 | + if ($type&(Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
133 | 133 | return self::buildNodeList($node->value, $parent); |
134 | 134 | } |
135 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
135 | + if ($type&Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
136 | 136 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
137 | 137 | Y::ITEM => 'buildItem', |
138 | 138 | Y::KEY => 'buildKey', |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
156 | 156 | * @return null |
157 | 157 | */ |
158 | - private static function buildKey(Node $node, &$parent=null) |
|
158 | + private static function buildKey(Node $node, &$parent = null) |
|
159 | 159 | { |
160 | 160 | extract((array) $node, EXTR_REFS); |
161 | 161 | if (is_null($identifier)) { |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | $numKeys = array_filter(array_keys($ref), 'is_int'); |
203 | 203 | $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0; |
204 | 204 | if ($value instanceof Node) { |
205 | - if($value->type & Y::KEY) { |
|
205 | + if ($value->type & Y::KEY) { |
|
206 | 206 | self::buildKey($node->value, $parent); |
207 | 207 | return; |
208 | 208 | } elseif ($value->type & Y::ITEM) { |
@@ -231,13 +231,13 @@ discard block |
||
231 | 231 | //remove trailing blank |
232 | 232 | while ($list->top()->type & Y::BLANK) $list->pop(); |
233 | 233 | $result = ''; |
234 | - $separator = [ 0 => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][(int) $type]; |
|
234 | + $separator = [0 => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][(int) $type]; |
|
235 | 235 | foreach ($list as $child) { |
236 | 236 | if ($child->value instanceof NodeList) { |
237 | 237 | $result .= self::buildLitteral($child->value, $type).$separator; |
238 | 238 | } else { |
239 | 239 | $val = $child->type & (Y::SCALAR|Y::BLANK) ? $child->value : substr($child->raw, $refIndent); |
240 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
240 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
241 | 241 | if ($result[-1] === $separator) |
242 | 242 | $result[-1] = "\n"; |
243 | 243 | if ($result[-1] === "\n") |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | private function buildSetKey(Node $node, &$parent) |
262 | 262 | { |
263 | 263 | $built = is_object($node->value) ? self::build($node->value) : null; |
264 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
264 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
265 | 265 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
266 | 266 | if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
267 | 267 | $parent->{trim($key, '\'" ')} = null; |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | { |
278 | 278 | $prop = array_keys(get_object_vars($parent)); |
279 | 279 | $key = end($prop); |
280 | - if ($node->value->type & (Y::ITEM|Y::KEY )) { |
|
280 | + if ($node->value->type & (Y::ITEM|Y::KEY)) { |
|
281 | 281 | $node->value = new NodeList($node->value); |
282 | 282 | } |
283 | 283 | $parent->{$key} = self::build($node->value); |