@@ -1,11 +1,11 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Dallgoot\Yaml; |
3 | 3 | |
4 | -require_once __DIR__ . '/../../dependencies/autoload.php'; |
|
4 | +require_once __DIR__.'/../../dependencies/autoload.php'; |
|
5 | 5 | |
6 | 6 | use Dallgoot; |
7 | 7 | |
8 | -$fileName = __DIR__ . '/../dummy.yml'; |
|
8 | +$fileName = __DIR__.'/../dummy.yml'; |
|
9 | 9 | |
10 | 10 | $yamlObject = Yaml::parseFile($fileName, $options = null, $debug = null); |
11 | 11 |
@@ -177,7 +177,7 @@ |
||
177 | 177 | elseif (in_array($first, ['?', ':'])) NodeHandlers::onSetElement($nodeValue, $this); |
178 | 178 | elseif (in_array($first, ['!', '&', '*'])) NodeHandlers::onNodeAction($nodeValue, $this); |
179 | 179 | else { |
180 | - $characters = [ '#' => [Y::COMMENT, $nodeValue], |
|
180 | + $characters = ['#' => [Y::COMMENT, $nodeValue], |
|
181 | 181 | '%' => [Y::DIRECTIVE, $nodeValue], |
182 | 182 | '>' => [Y::LITT_FOLDED, null], |
183 | 183 | '|' => [Y::LITT, null] |
@@ -68,28 +68,28 @@ |
||
68 | 68 | public static function onCompact($value, Node $node) |
69 | 69 | { |
70 | 70 | $node->value = json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
71 | - if (json_last_error() === JSON_ERROR_NONE){ |
|
71 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
72 | 72 | $node->type = Y::JSON; |
73 | 73 | return; |
74 | 74 | } |
75 | 75 | $node->value = new NodeList(); |
76 | - if (preg_match(R::MAPPING, $value)){ |
|
76 | + if (preg_match(R::MAPPING, $value)) { |
|
77 | 77 | $node->type = Y::COMPACT_MAPPING; |
78 | 78 | $node->value->type = Y::COMPACT_MAPPING; |
79 | - preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1,-1)), $matches); |
|
79 | + preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1, -1)), $matches); |
|
80 | 80 | foreach ($matches['k'] as $index => $property) { |
81 | 81 | $n = new Node('', $node->line); |
82 | 82 | $n->type = Y::KEY; |
83 | - $n->identifier = trim($property, '"\' ');//TODO : maybe check for proper quoting first ? |
|
83 | + $n->identifier = trim($property, '"\' '); //TODO : maybe check for proper quoting first ? |
|
84 | 84 | $n->value = new Node(trim($matches['v'][$index]), $node->line); |
85 | 85 | $node->value->push($n); |
86 | 86 | } |
87 | 87 | return; |
88 | 88 | } |
89 | - if (preg_match(R::SEQUENCE, $value)){ |
|
89 | + if (preg_match(R::SEQUENCE, $value)) { |
|
90 | 90 | $node->type = Y::COMPACT_SEQUENCE; |
91 | 91 | $node->value->type = Y::COMPACT_SEQUENCE; |
92 | - preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1,-1)), $matches); |
|
92 | + preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1, -1)), $matches); |
|
93 | 93 | foreach ($matches['item'] as $key => $item) { |
94 | 94 | $i = new Node('', $node->line); |
95 | 95 | $i->type = Y::ITEM; |
@@ -53,8 +53,8 @@ |
||
53 | 53 | $value->forceType(); |
54 | 54 | // $parent = Builder::$_root; |
55 | 55 | switch ($this->tagName) { |
56 | - case '!set': $value->type = Y::SET;break; |
|
57 | - case '!omap': $value->type = Y::SEQUENCE;break; |
|
56 | + case '!set': $value->type = Y::SET; break; |
|
57 | + case '!omap': $value->type = Y::SEQUENCE; break; |
|
58 | 58 | // assumed to be !str,!binary |
59 | 59 | default: $parent = null; |
60 | 60 | $value->type = Y::RAW; |
@@ -98,27 +98,27 @@ discard block |
||
98 | 98 | * |
99 | 99 | * @return mixed The parent (object|array) or a string representing the NodeList. |
100 | 100 | */ |
101 | - public static function buildNodeList(NodeList $list, &$parent=null) |
|
101 | + public static function buildNodeList(NodeList $list, &$parent = null) |
|
102 | 102 | { |
103 | 103 | $list->forceType(); |
104 | - $action = function ($child, &$parent, &$out) { |
|
104 | + $action = function($child, &$parent, &$out) { |
|
105 | 105 | self::build($child, $out); |
106 | 106 | }; |
107 | 107 | $list->type = $list->type ?? Y::RAW; |
108 | 108 | if ($list->type & (Y::RAW|Y::LITTERALS)) { |
109 | - $tmp = self::buildLitteral($list, $list->type);//var_dump("ICI1",$list, $tmp); |
|
109 | + $tmp = self::buildLitteral($list, $list->type); //var_dump("ICI1",$list, $tmp); |
|
110 | 110 | return $tmp; |
111 | 111 | } elseif ($list->type & (Y::COMPACT_MAPPING|Y::MAPPING|Y::SET)) {//var_dump("ICI2"); |
112 | 112 | $out = $parent ?? new \StdClass; |
113 | 113 | } elseif ($list->type & (Y::COMPACT_SEQUENCE|Y::SEQUENCE)) {//var_dump("ICI3"); |
114 | 114 | $out = $parent ?? []; |
115 | 115 | } else { |
116 | - $out = null;//var_dump("ICI"); |
|
117 | - $action = function ($child, &$parent, &$out) { |
|
116 | + $out = null; //var_dump("ICI"); |
|
117 | + $action = function($child, &$parent, &$out) { |
|
118 | 118 | if ($child->type & (Y::SCALAR|Y::QUOTED|Y::DOC_START)) { |
119 | 119 | $out .= Node2PHP::get($child); |
120 | 120 | } else { |
121 | - self::build($child);//var_dump("HERE"); |
|
121 | + self::build($child); //var_dump("HERE"); |
|
122 | 122 | } |
123 | 123 | // var_dump("out:".$out); |
124 | 124 | }; |
@@ -152,15 +152,15 @@ discard block |
||
152 | 152 | ]; |
153 | 153 | if (isset($actions[$type])) { |
154 | 154 | return TB::{$actions[$type]}($node, $parent); |
155 | - } elseif ($type & Y::COMMENT) { |
|
155 | + } elseif ($type&Y::COMMENT) { |
|
156 | 156 | self::$_root->addComment($line, $value); |
157 | 157 | // } elseif ($type & Y::DOC_START) { |
158 | 158 | // if (!is_null($value)) { |
159 | 159 | // return self::build($value->value, $self::$_root); |
160 | 160 | // } |
161 | - } elseif ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
161 | + } elseif ($type&(Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
162 | 162 | return self::buildNodeList($value, $parent); |
163 | - } elseif ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
163 | + } elseif ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
164 | 164 | return TB::buildReference($node, $parent); |
165 | 165 | } elseif ($value instanceof Node) { |
166 | 166 | return self::buildNode($value, $parent); |
@@ -184,10 +184,10 @@ discard block |
||
184 | 184 | $result = ''; |
185 | 185 | $list->rewind(); |
186 | 186 | $refIndent = $list->count() > 0 ? $list->current()->indent : 0; |
187 | - if($list->count()) { |
|
187 | + if ($list->count()) { |
|
188 | 188 | //remove trailing blank |
189 | 189 | while ($list->top()->type & Y::BLANK) $list->pop(); |
190 | - $separator = [ Y::RAW => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][$type]; |
|
190 | + $separator = [Y::RAW => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][$type]; |
|
191 | 191 | foreach ($list as $child) { |
192 | 192 | if ($child->value instanceof NodeList) { |
193 | 193 | $result .= self::buildLitteral($child->value, $type).$separator; |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | $val = substr($node->raw, $refIndent); |
215 | 215 | } |
216 | 216 | } |
217 | - if ($type & Y::LITT_FOLDED && ($node->indent > $refIndent || ($node->type & Y::BLANK))) { |
|
217 | + if ($type&Y::LITT_FOLDED && ($node->indent > $refIndent || ($node->type & Y::BLANK))) { |
|
218 | 218 | if ($result[-1] === $separator) |
219 | 219 | $result[-1] = "\n"; |
220 | 220 | if ($result[-1] === "\n") |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | } else { |
53 | 53 | self::dump($dataType, 0); |
54 | 54 | } |
55 | - $out = implode("\n", iterator_to_array(self::$result));//var_dump(iterator_to_array(self::$result)); |
|
55 | + $out = implode("\n", iterator_to_array(self::$result)); //var_dump(iterator_to_array(self::$result)); |
|
56 | 56 | self::$result = null; |
57 | 57 | return $out; |
58 | 58 | } |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | foreach ($array as $key => $item) { |
120 | 120 | $lineStart = current($refKeys) === $key ? "- " : "- $key: "; |
121 | 121 | if (is_scalar($item)) { |
122 | - self::add($lineStart.self::dump($item,0), $indent); |
|
122 | + self::add($lineStart.self::dump($item, 0), $indent); |
|
123 | 123 | } else { |
124 | 124 | self::add(rtrim($lineStart), $indent); |
125 | 125 | self::dump($item, $indent + self::INDENT); |
@@ -149,14 +149,14 @@ discard block |
||
149 | 149 | //TODO: consider dumping datetime as date strings according to a format provided by user or default |
150 | 150 | if ($obj instanceof \DateTime) return $obj->format(self::DATE_FORMAT); |
151 | 151 | // if ($obj instanceof \SplString) {var_dump('splstrin',$obj);return '"'.$obj.'"';} |
152 | - $propList = get_object_vars($obj);//var_dump($propList); |
|
152 | + $propList = get_object_vars($obj); //var_dump($propList); |
|
153 | 153 | foreach ($propList as $property => $value) { |
154 | 154 | if (is_scalar($value) || $value instanceof Compact || $value instanceof \DateTime) { |
155 | - self::add("$property: ".self::dump($value, $indent), $indent);//var_dump('IS SCALAR', $value); |
|
155 | + self::add("$property: ".self::dump($value, $indent), $indent); //var_dump('IS SCALAR', $value); |
|
156 | 156 | } else { |
157 | 157 | self::add("$property:", $indent); |
158 | 158 | // self::add(self::dump($value, $indent + self::INDENT), $indent + self::INDENT);var_dump('NOT SCALAR'); |
159 | - self::dump($value, $indent + self::INDENT);//var_dump('NOT SCALAR'); |
|
159 | + self::dump($value, $indent + self::INDENT); //var_dump('NOT SCALAR'); |
|
160 | 160 | } |
161 | 161 | } |
162 | 162 | } |
@@ -166,8 +166,8 @@ discard block |
||
166 | 166 | $pairs = []; |
167 | 167 | if (is_array($subject) || $subject instanceof \ArrayIterator) { |
168 | 168 | $max = count($subject); |
169 | - $objectAsArray = is_array($subject) ? $subject : $subject->getArrayCopy();//var_dump(array_keys($objectAsArray), range(0, $max)); |
|
170 | - if(array_keys($objectAsArray) !== range(0, $max-1)) { |
|
169 | + $objectAsArray = is_array($subject) ? $subject : $subject->getArrayCopy(); //var_dump(array_keys($objectAsArray), range(0, $max)); |
|
170 | + if (array_keys($objectAsArray) !== range(0, $max - 1)) { |
|
171 | 171 | $pairs = $objectAsArray; |
172 | 172 | } else { |
173 | 173 | $valuesList = []; |
@@ -60,22 +60,22 @@ |
||
60 | 60 | { |
61 | 61 | if (is_null($this->type)) { |
62 | 62 | $childTypes = $this->getTypes(); |
63 | - if ($childTypes & (Y::KEY|Y::SET_KEY)) { |
|
64 | - if ($childTypes & Y::ITEM) { |
|
63 | + if ($childTypes&(Y::KEY|Y::SET_KEY)) { |
|
64 | + if ($childTypes&Y::ITEM) { |
|
65 | 65 | throw new \ParseError(self::class.": Error conflicting types found"); |
66 | 66 | } |
67 | 67 | $this->type = Y::MAPPING; |
68 | 68 | } else { |
69 | - if ($childTypes & Y::ITEM) { |
|
69 | + if ($childTypes&Y::ITEM) { |
|
70 | 70 | $this->type = Y::SEQUENCE; |
71 | - } elseif ($childTypes & Y::DOC_START && $this->count() === 1) {var_dump(__METHOD__.' theres a DOCSTART'); |
|
71 | + } elseif ($childTypes&Y::DOC_START && $this->count() === 1) {var_dump(__METHOD__.' theres a DOCSTART'); |
|
72 | 72 | if ($child->value instanceof Node) { |
73 | 73 | $this->type = $child->value->type; |
74 | 74 | } elseif ($child->value instanceof NodeList) { |
75 | 75 | $child->value->forceType(); |
76 | - $this->type = $child->value->type; |
|
76 | + $this->type = $child->value->type; |
|
77 | 77 | } |
78 | - } elseif (!($childTypes & Y::COMMENT)) { |
|
78 | + } elseif (!($childTypes&Y::COMMENT)) { |
|
79 | 79 | $this->type = Y::LITT_FOLDED; |
80 | 80 | } else { |
81 | 81 | $this->type = Y::SCALAR; |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
33 | 33 | * @return null |
34 | 34 | */ |
35 | - public static function buildKey(Node $node, &$parent=null) |
|
35 | + public static function buildKey(Node $node, &$parent = null) |
|
36 | 36 | { |
37 | 37 | extract((array) $node, EXTR_REFS); |
38 | 38 | if (is_null($identifier)) { |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | public static function buildSetKey(Node $node, &$parent = null) |
100 | 100 | { |
101 | 101 | $built = is_object($node->value) ? Builder::build($node->value) : null; |
102 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
102 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
103 | 103 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
104 | 104 | if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
105 | 105 | $parent->{trim($key, '\'" ')} = null; |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | { |
116 | 116 | $prop = array_keys(get_object_vars($parent)); |
117 | 117 | $key = end($prop); |
118 | - if ($node->value->type & (Y::ITEM|Y::KEY )) { |
|
118 | + if ($node->value->type & (Y::ITEM|Y::KEY)) { |
|
119 | 119 | $node->value = new NodeList($node->value); |
120 | 120 | } |
121 | 121 | $parent->{$key} = Builder::build(/** @scrutinizer ignore-type */ $node->value); |
@@ -6,14 +6,14 @@ |
||
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 | 15 | // $content = file_get_contents('./tests/cases/parsing/yaml_in_literal_folded.yml');//var_dump($content); |
16 | -$content = file_get_contents('./tests/cases/parsing/tags_in_mapping.yml');//var_dump($content); |
|
16 | +$content = file_get_contents('./tests/cases/parsing/tags_in_mapping.yml'); //var_dump($content); |
|
17 | 17 | $yaml = Y::parse($content, null, (int) $argv[1]); |
18 | 18 | // $yaml = Y::parseFile('./references/Example 2.27.yml', null, 1); |
19 | 19 | var_dump($yaml); |