@@ -74,7 +74,7 @@ |
||
| 74 | 74 | switch (gettype($dataType)) { |
| 75 | 75 | case 'boolean': return $dataType ? 'true' : 'false'; |
| 76 | 76 | case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
| 77 | - return sprintf('%.2F', $dataType); |
|
| 77 | + return sprintf('%.2F', $dataType); |
|
| 78 | 78 | case 'double': if (is_nan((float) $dataType)) return '.nan'; |
| 79 | 79 | default: |
| 80 | 80 | return $dataType; |
@@ -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 { |
@@ -40,7 +40,9 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public static function toString($dataType, int $options = null):string |
| 42 | 42 | { |
| 43 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 43 | + if (is_null($dataType)) { |
|
| 44 | + throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 45 | + } |
|
| 44 | 46 | self::$options = is_int($options) ? $options : self::OPTIONS; |
| 45 | 47 | self::$result = new DLL; |
| 46 | 48 | if ($dataType instanceof YamlObject) { |
@@ -76,9 +78,13 @@ discard block |
||
| 76 | 78 | if (is_scalar($dataType)) { |
| 77 | 79 | switch (gettype($dataType)) { |
| 78 | 80 | case 'boolean': return $dataType ? 'true' : 'false'; |
| 79 | - case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 81 | + case 'float': if (is_infinite($dataType)) { |
|
| 82 | + return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 83 | + } |
|
| 80 | 84 | return sprintf('%.2F', $dataType); |
| 81 | - case 'double': if (is_nan((float) $dataType)) return '.nan'; |
|
| 85 | + case 'double': if (is_nan((float) $dataType)) { |
|
| 86 | + return '.nan'; |
|
| 87 | + } |
|
| 82 | 88 | default: |
| 83 | 89 | return $dataType; |
| 84 | 90 | } |
@@ -91,7 +97,9 @@ discard block |
||
| 91 | 97 | |
| 92 | 98 | private static function dumpYamlObject(YamlObject $dataType) |
| 93 | 99 | { |
| 94 | - if ($dataType->hasDocStart() && self::$result instanceof DLL) self::$result->push("---"); |
|
| 100 | + if ($dataType->hasDocStart() && self::$result instanceof DLL) { |
|
| 101 | + self::$result->push("---"); |
|
| 102 | + } |
|
| 95 | 103 | // self::dump($dataType, 0); |
| 96 | 104 | if (count($dataType) > 0) { |
| 97 | 105 | self::dumpSequence($dataType->getArrayCopy(), 0); |
@@ -142,9 +150,13 @@ discard block |
||
| 142 | 150 | self::add(self::dump($obj->value, $indent + self::INDENT), $indent + self::INDENT); |
| 143 | 151 | } |
| 144 | 152 | } |
| 145 | - if ($obj instanceof Compact) return self::dumpCompact($obj, $indent); |
|
| 153 | + if ($obj instanceof Compact) { |
|
| 154 | + return self::dumpCompact($obj, $indent); |
|
| 155 | + } |
|
| 146 | 156 | //TODO: consider dumping datetime as date strings according to a format provided by user or default |
| 147 | - if ($obj instanceof \DateTime) return $obj->format('Y-m-d'); |
|
| 157 | + if ($obj instanceof \DateTime) { |
|
| 158 | + return $obj->format('Y-m-d'); |
|
| 159 | + } |
|
| 148 | 160 | // if ($obj instanceof \SplString) {var_dump('splstrin',$obj);return '"'.$obj.'"';} |
| 149 | 161 | $propList = get_object_vars($obj); |
| 150 | 162 | foreach ($propList as $property => $value) { |
@@ -102,6 +102,9 @@ |
||
| 102 | 102 | //TODO: $references = $dataType->getAllReferences(); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | + /** |
|
| 106 | + * @param integer $indent |
|
| 107 | + */ |
|
| 105 | 108 | private static function add($value, $indent) |
| 106 | 109 | { |
| 107 | 110 | $newVal = str_repeat(" ", $indent).$value; |
@@ -52,8 +52,12 @@ |
||
| 52 | 52 | { |
| 53 | 53 | $prop = get_object_vars($this); |
| 54 | 54 | unset($prop["__yaml__object__api"]); |
| 55 | - if (count($prop) > 0) return $prop; |
|
| 56 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 55 | + if (count($prop) > 0) { |
|
| 56 | + return $prop; |
|
| 57 | + } |
|
| 58 | + if (count($this) > 0) { |
|
| 59 | + return iterator_to_array($this); |
|
| 60 | + } |
|
| 57 | 61 | return $this->__yaml__object__api->value ?? "Empty YamlObject"; |
| 58 | 62 | } |
| 59 | 63 | } |
@@ -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); |
@@ -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); |
@@ -162,11 +162,11 @@ |
||
| 162 | 162 | } |
| 163 | 163 | if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
| 164 | 164 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
| 165 | - Y::ITEM => 'buildItem', |
|
| 166 | - Y::KEY => 'buildKey', |
|
| 167 | - Y::SET_KEY => 'buildSetKey', |
|
| 168 | - Y::SET_VALUE => 'buildSetValue', |
|
| 169 | - Y::TAG => 'buildTag', |
|
| 165 | + Y::ITEM => 'buildItem', |
|
| 166 | + Y::KEY => 'buildKey', |
|
| 167 | + Y::SET_KEY => 'buildSetKey', |
|
| 168 | + Y::SET_VALUE => 'buildSetValue', |
|
| 169 | + Y::TAG => 'buildTag', |
|
| 170 | 170 | ]; |
| 171 | 171 | if (isset($typesActions[$type])) { |
| 172 | 172 | return self::{$typesActions[$type]}($node, $parent); |
@@ -238,7 +238,6 @@ |
||
| 238 | 238 | /** |
| 239 | 239 | * Builds a litteral (folded or not) or any NodeList that has YAML::RAW type (like a multiline value) |
| 240 | 240 | * |
| 241 | - * @param NodeList $children The children |
|
| 242 | 241 | * @param integer $type The type |
| 243 | 242 | * |
| 244 | 243 | * @return string The litteral. |
@@ -80,13 +80,13 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @return mixed The parent (object|array) or a string representing the NodeList. |
| 82 | 82 | */ |
| 83 | - private static function buildNodeList(NodeList $node, &$parent=null) |
|
| 83 | + private static function buildNodeList(NodeList $node, &$parent = null) |
|
| 84 | 84 | { |
| 85 | 85 | $node->forceType(); |
| 86 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
| 86 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
| 87 | 87 | return self::buildLitteral($node, (int) $node->type); |
| 88 | 88 | } |
| 89 | - $action = function ($child, &$parent, &$out) { |
|
| 89 | + $action = function($child, &$parent, &$out) { |
|
| 90 | 90 | self::build($child, $out); |
| 91 | 91 | }; |
| 92 | 92 | if ($node->type & (Y::COMPACT_MAPPING|Y::MAPPING|Y::SET)) { |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | $out = $parent ?? []; |
| 96 | 96 | } else { |
| 97 | 97 | $out = ''; |
| 98 | - $action = function ($child, &$parent, &$out) { |
|
| 98 | + $action = function($child, &$parent, &$out) { |
|
| 99 | 99 | if ($child->type & (Y::SCALAR|Y::QUOTED)) { |
| 100 | 100 | if ($parent) { |
| 101 | 101 | $parent->setText(self::build($child)); |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | private static function buildNode(Node $node, &$parent) |
| 126 | 126 | { |
| 127 | 127 | extract((array) $node, EXTR_REFS); |
| 128 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
| 128 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
| 129 | 129 | if (is_object($value)) { |
| 130 | 130 | $tmp = self::build($value, $parent) ?? $parent; |
| 131 | 131 | } else { |
@@ -134,10 +134,10 @@ discard block |
||
| 134 | 134 | if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
| 135 | 135 | return self::$_root->getReference($identifier); |
| 136 | 136 | } |
| 137 | - if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
| 137 | + if ($type&(Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
| 138 | 138 | return self::buildNodeList($node->value, $parent); |
| 139 | 139 | } |
| 140 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
| 140 | + if ($type&Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
| 141 | 141 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
| 142 | 142 | Y::ITEM => 'buildItem', |
| 143 | 143 | Y::KEY => 'buildKey', |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
| 161 | 161 | * @return null |
| 162 | 162 | */ |
| 163 | - private static function buildKey(Node $node, &$parent=null) |
|
| 163 | + private static function buildKey(Node $node, &$parent = null) |
|
| 164 | 164 | { |
| 165 | 165 | extract((array) $node, EXTR_REFS); |
| 166 | 166 | if (is_null($identifier)) { |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | } |
| 179 | 179 | if ($value instanceof NodeList) { |
| 180 | 180 | $childTypes = $value->getTypes(); |
| 181 | - if (is_null($value->type) && $childTypes & Y::SCALAR && !($childTypes & Y::COMMENT)) { |
|
| 181 | + if (is_null($value->type) && $childTypes&Y::SCALAR && !($childTypes&Y::COMMENT)) { |
|
| 182 | 182 | $result = self::buildLitteral($value, Y::LITT_FOLDED); |
| 183 | 183 | } else { |
| 184 | 184 | $result = self::buildNodeList($value); |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | $numKeys = array_filter(array_keys($ref), 'is_int'); |
| 216 | 216 | $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0; |
| 217 | 217 | if ($value instanceof Node) { |
| 218 | - if($value->type & Y::KEY) { |
|
| 218 | + if ($value->type & Y::KEY) { |
|
| 219 | 219 | self::buildKey($node->value, $parent); |
| 220 | 220 | return; |
| 221 | 221 | } elseif ($value->type & Y::ITEM) { |
@@ -250,14 +250,14 @@ discard block |
||
| 250 | 250 | } |
| 251 | 251 | $result = ''; |
| 252 | 252 | $separator = ''; |
| 253 | - if ($type & Y::LITT) $separator = "\n"; |
|
| 254 | - if ($type & Y::LITT_FOLDED) $separator = ' '; |
|
| 253 | + if ($type&Y::LITT) $separator = "\n"; |
|
| 254 | + if ($type&Y::LITT_FOLDED) $separator = ' '; |
|
| 255 | 255 | foreach ($list as $child) { |
| 256 | 256 | if ($child->value instanceof NodeList) { |
| 257 | 257 | $result .= self::buildLitteral($child->value, $type).$separator; |
| 258 | 258 | } else { |
| 259 | 259 | $val = $child->type & (Y::SCALAR|Y::BLANK) ? $child->value : substr($child->raw, $refIndent); |
| 260 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 260 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 261 | 261 | if ($result[-1] === $separator) |
| 262 | 262 | $result[-1] = "\n"; |
| 263 | 263 | if ($result[-1] === "\n") |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | private function buildSetKey(Node $node, &$parent) |
| 282 | 282 | { |
| 283 | 283 | $built = is_object($node->value) ? self::build($node->value) : null; |
| 284 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
| 284 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
| 285 | 285 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 286 | 286 | if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
| 287 | 287 | $parent->{trim($key, '\'" ')} = null; |
@@ -297,7 +297,7 @@ discard block |
||
| 297 | 297 | { |
| 298 | 298 | $prop = array_keys(get_object_vars($parent)); |
| 299 | 299 | $key = end($prop); |
| 300 | - if ($node->value->type & (Y::ITEM|Y::KEY )) { |
|
| 300 | + if ($node->value->type & (Y::ITEM|Y::KEY)) { |
|
| 301 | 301 | $nl = new NodeList; |
| 302 | 302 | $nl->push($node->value); |
| 303 | 303 | $node->value = $nl; |
@@ -40,10 +40,14 @@ discard block |
||
| 40 | 40 | } |
| 41 | 41 | $_root->value instanceof NodeList && $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 42 | 42 | foreach ($_root->value as $child) { |
| 43 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 43 | + if ($child->type & Y::DOC_START) { |
|
| 44 | + $totalDocStart++; |
|
| 45 | + } |
|
| 44 | 46 | //if 0 or 1 DOC_START = we are still in first document |
| 45 | 47 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 46 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 48 | + if (!isset($documents[$currentDoc])) { |
|
| 49 | + $documents[$currentDoc] = new NodeList(); |
|
| 50 | + } |
|
| 47 | 51 | $documents[$currentDoc]->push($child); |
| 48 | 52 | } |
| 49 | 53 | $content = []; |
@@ -68,7 +72,9 @@ discard block |
||
| 68 | 72 | */ |
| 69 | 73 | private static function build(object $node, &$parent = null) |
| 70 | 74 | { |
| 71 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
| 75 | + if ($node instanceof NodeList) { |
|
| 76 | + return self::buildNodeList($node, $parent); |
|
| 77 | + } |
|
| 72 | 78 | return self::buildNode($node, $parent); |
| 73 | 79 | } |
| 74 | 80 | |
@@ -131,13 +137,17 @@ discard block |
||
| 131 | 137 | } else { |
| 132 | 138 | $tmp = Node2PHP::get($node); |
| 133 | 139 | } |
| 134 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 140 | + if ($type === Y::REF_DEF) { |
|
| 141 | + self::$_root->addReference($identifier, $tmp); |
|
| 142 | + } |
|
| 135 | 143 | return self::$_root->getReference($identifier); |
| 136 | 144 | } |
| 137 | 145 | if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
| 138 | 146 | return self::buildNodeList($node->value, $parent); |
| 139 | 147 | } |
| 140 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
| 148 | + if ($type & Y::COMMENT) { |
|
| 149 | + self::$_root->addComment($node->line, $node->value); |
|
| 150 | + } |
|
| 141 | 151 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
| 142 | 152 | Y::ITEM => 'buildItem', |
| 143 | 153 | Y::KEY => 'buildKey', |
@@ -250,18 +260,24 @@ discard block |
||
| 250 | 260 | } |
| 251 | 261 | $result = ''; |
| 252 | 262 | $separator = ''; |
| 253 | - if ($type & Y::LITT) $separator = "\n"; |
|
| 254 | - if ($type & Y::LITT_FOLDED) $separator = ' '; |
|
| 263 | + if ($type & Y::LITT) { |
|
| 264 | + $separator = "\n"; |
|
| 265 | + } |
|
| 266 | + if ($type & Y::LITT_FOLDED) { |
|
| 267 | + $separator = ' '; |
|
| 268 | + } |
|
| 255 | 269 | foreach ($list as $child) { |
| 256 | 270 | if ($child->value instanceof NodeList) { |
| 257 | 271 | $result .= self::buildLitteral($child->value, $type).$separator; |
| 258 | 272 | } else { |
| 259 | 273 | $val = $child->type & (Y::SCALAR|Y::BLANK) ? $child->value : substr($child->raw, $refIndent); |
| 260 | 274 | if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
| 261 | - if ($result[-1] === $separator) |
|
| 262 | - $result[-1] = "\n"; |
|
| 263 | - if ($result[-1] === "\n") |
|
| 264 | - $result .= $val; |
|
| 275 | + if ($result[-1] === $separator) { |
|
| 276 | + $result[-1] = "\n"; |
|
| 277 | + } |
|
| 278 | + if ($result[-1] === "\n") { |
|
| 279 | + $result .= $val; |
|
| 280 | + } |
|
| 265 | 281 | continue; |
| 266 | 282 | } |
| 267 | 283 | $result .= $val.$separator; |
@@ -283,7 +299,9 @@ discard block |
||
| 283 | 299 | $built = is_object($node->value) ? self::build($node->value) : null; |
| 284 | 300 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
| 285 | 301 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 286 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 302 | + if (empty($key)) { |
|
| 303 | + throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 304 | + } |
|
| 287 | 305 | $parent->{trim($key, '\'" ')} = null; |
| 288 | 306 | } |
| 289 | 307 | |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * Create the Node object and parses $nodeString IF not null (else assume a root type Node) |
| 33 | 33 | * |
| 34 | 34 | * @param string|null $nodeString The node string |
| 35 | - * @param int|null $line The line |
|
| 35 | + * @param integer $line The line |
|
| 36 | 36 | */ |
| 37 | 37 | public function __construct($nodeString = null, $line = 0) |
| 38 | 38 | { |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | * Process when a "key: value" syntax is found in the parsed string |
| 222 | 222 | * Note : key is match 1, value is match 2 as per regex from R::KEY |
| 223 | 223 | * |
| 224 | - * @param array $matches The matches provided by 'preg_match' function in Node::parse |
|
| 224 | + * @param string[] $matches The matches provided by 'preg_match' function in Node::parse |
|
| 225 | 225 | */ |
| 226 | 226 | private function onKey(array $matches) |
| 227 | 227 | { |
@@ -181,9 +181,9 @@ |
||
| 181 | 181 | $this->value = $nodeValue; |
| 182 | 182 | return; |
| 183 | 183 | } elseif (in_array($first, ['{', '['])) { |
| 184 | - $this->onCompact(trim($nodeValue)); |
|
| 185 | - return; |
|
| 186 | - } elseif (in_array($first, ['!', '&', '*'])) { |
|
| 184 | + $this->onCompact(trim($nodeValue)); |
|
| 185 | + return; |
|
| 186 | + } elseif (in_array($first, ['!', '&', '*'])) { |
|
| 187 | 187 | $this->onNodeAction($nodeValue); |
| 188 | 188 | return; |
| 189 | 189 | } elseif(in_array($first, ['?', ':'])) {// Note : php don't like '?' as an array key -_-' |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | } elseif (in_array($first, ['!', '&', '*'])) { |
| 187 | 187 | $this->onNodeAction($nodeValue); |
| 188 | 188 | return; |
| 189 | - } elseif(in_array($first, ['?', ':'])) {// Note : php don't like '?' as an array key -_-' |
|
| 189 | + } elseif (in_array($first, ['?', ':'])) {// Note : php don't like '?' as an array key -_-' |
|
| 190 | 190 | $this->type = $first === '?' ? Y::SET_KEY : Y::SET_VALUE; |
| 191 | 191 | if (!empty(trim($v))) { |
| 192 | 192 | $this->value = new NodeList; |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | $this->onHyphen($nodeValue); |
| 198 | 198 | return; |
| 199 | 199 | } |
| 200 | - $characters = [ '#' => [Y::COMMENT, $v], |
|
| 200 | + $characters = ['#' => [Y::COMMENT, $v], |
|
| 201 | 201 | '%' => [Y::DIRECTIVE, $v], |
| 202 | 202 | '>' => [Y::LITT_FOLDED, null], |
| 203 | 203 | '|' => [Y::LITT, null] |
@@ -249,28 +249,28 @@ discard block |
||
| 249 | 249 | private function onCompact($value) |
| 250 | 250 | { |
| 251 | 251 | $this->value = json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 252 | - if (json_last_error() === JSON_ERROR_NONE){ |
|
| 252 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
| 253 | 253 | $this->type = Y::JSON; |
| 254 | 254 | return; |
| 255 | 255 | } |
| 256 | 256 | $this->value = new NodeList(); |
| 257 | - if (preg_match(R::MAPPING, $value)){ |
|
| 257 | + if (preg_match(R::MAPPING, $value)) { |
|
| 258 | 258 | $this->type = Y::COMPACT_MAPPING; |
| 259 | 259 | $this->value->type = Y::COMPACT_MAPPING; |
| 260 | - preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1,-1)), $matches); |
|
| 260 | + preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1, -1)), $matches); |
|
| 261 | 261 | foreach ($matches['k'] as $index => $property) { |
| 262 | 262 | $n = new Node('', $this->line); |
| 263 | 263 | $n->type = Y::KEY; |
| 264 | - $n->identifier = trim($property, '"\' ');//TODO : maybe check for proper quoting first ? |
|
| 264 | + $n->identifier = trim($property, '"\' '); //TODO : maybe check for proper quoting first ? |
|
| 265 | 265 | $n->value = new Node($matches['v'][$index], $this->line); |
| 266 | 266 | $this->value->push($n); |
| 267 | 267 | } |
| 268 | 268 | return; |
| 269 | 269 | } |
| 270 | - if (preg_match(R::SEQUENCE, $value)){ |
|
| 270 | + if (preg_match(R::SEQUENCE, $value)) { |
|
| 271 | 271 | $this->type = Y::COMPACT_SEQUENCE; |
| 272 | 272 | $this->value->type = Y::COMPACT_SEQUENCE; |
| 273 | - $count = preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1,-1)), $matches); |
|
| 273 | + $count = preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1, -1)), $matches); |
|
| 274 | 274 | foreach ($matches['item'] as $key => $item) { |
| 275 | 275 | $i = new Node('', $this->line); |
| 276 | 276 | $i->type = Y::ITEM; |
@@ -71,7 +71,9 @@ discard block |
||
| 71 | 71 | if ($this->type === Y::ROOT) { |
| 72 | 72 | return $this; |
| 73 | 73 | } |
| 74 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
| 74 | + if (!is_int($indent)) { |
|
| 75 | + return $this->parent ?? $this; |
|
| 76 | + } |
|
| 75 | 77 | $cursor = $this; |
| 76 | 78 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
| 77 | 79 | if ($cursor->indent === $indent && $cursor->type !== $type) { |
@@ -103,7 +105,7 @@ discard block |
||
| 103 | 105 | if (is_null($current)) { |
| 104 | 106 | $this->value = $child; |
| 105 | 107 | return; |
| 106 | - }elseif ($current instanceof Node) { |
|
| 108 | + } elseif ($current instanceof Node) { |
|
| 107 | 109 | $this->value = new NodeList(); |
| 108 | 110 | if ($current->type & Y::LITTERALS) { |
| 109 | 111 | $this->value->type = $current->type; |
@@ -113,9 +115,15 @@ discard block |
||
| 113 | 115 | } |
| 114 | 116 | //modify type according to child |
| 115 | 117 | if (is_null($this->value->type)) { |
| 116 | - if ($child->type & Y::SET_KEY) $this->value->type = Y::SET; |
|
| 117 | - if ($child->type & Y::KEY) $this->value->type = Y::MAPPING; |
|
| 118 | - if ($child->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
|
| 118 | + if ($child->type & Y::SET_KEY) { |
|
| 119 | + $this->value->type = Y::SET; |
|
| 120 | + } |
|
| 121 | + if ($child->type & Y::KEY) { |
|
| 122 | + $this->value->type = Y::MAPPING; |
|
| 123 | + } |
|
| 124 | + if ($child->type & Y::ITEM) { |
|
| 125 | + $this->value->type = Y::SEQUENCE; |
|
| 126 | + } |
|
| 119 | 127 | } |
| 120 | 128 | $this->value->push($child); |
| 121 | 129 | } |
@@ -37,6 +37,9 @@ discard block |
||
| 37 | 37 | private const EXCEPTION_READ_ERROR = self::class.": file '%s' failed to be loaded (permission denied ?)"; |
| 38 | 38 | private const EXCEPTION_LINE_SPLIT = self::class.": content is not a string(maybe a file error?)"; |
| 39 | 39 | |
| 40 | + /** |
|
| 41 | + * @param string $absolutePath |
|
| 42 | + */ |
|
| 40 | 43 | public function __construct($absolutePath = null, $options = null, $debug = 0) |
| 41 | 44 | { |
| 42 | 45 | $this->debug = is_int($debug) ? min($debug, 3) : 1; |
@@ -73,6 +76,9 @@ discard block |
||
| 73 | 76 | return $this; |
| 74 | 77 | } |
| 75 | 78 | |
| 79 | + /** |
|
| 80 | + * @param string $strContent |
|
| 81 | + */ |
|
| 76 | 82 | private function getSourceIterator($strContent = null) |
| 77 | 83 | { |
| 78 | 84 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
@@ -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(); |
@@ -77,7 +77,9 @@ discard block |
||
| 77 | 77 | { |
| 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 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 80 | + if (!is_array($source) || !count($source)) { |
|
| 81 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 82 | + } |
|
| 81 | 83 | return function () use($source) { |
| 82 | 84 | foreach ($source as $key => $value) { |
| 83 | 85 | yield ++$key => $value; |
@@ -105,7 +107,9 @@ discard block |
||
| 105 | 107 | $n = new Node($lineString, $lineNb); |
| 106 | 108 | $deepest = $previous->getDeepestNode(); |
| 107 | 109 | if ($n->type & (Y::LITTERALS|Y::BLANK|Y::COMMENT|Y::TAG) || $deepest->type & Y::PARTIAL) { |
| 108 | - if ($this->onSpecialType($n, $previous, $emptyLines, $lineString)) continue; |
|
| 110 | + if ($this->onSpecialType($n, $previous, $emptyLines, $lineString)) { |
|
| 111 | + continue; |
|
| 112 | + } |
|
| 109 | 113 | } |
| 110 | 114 | //Note: 6.6 comments: Note that outside scalar content, a line containing only white space characters is taken to be a comment line. |
| 111 | 115 | foreach ($emptyLines as $blankNode) { |
@@ -134,7 +138,9 @@ discard block |
||
| 134 | 138 | } |
| 135 | 139 | } |
| 136 | 140 | } |
| 137 | - if ($this->onContextType($n, $target, $lineString)) continue; |
|
| 141 | + if ($this->onContextType($n, $target, $lineString)) { |
|
| 142 | + continue; |
|
| 143 | + } |
|
| 138 | 144 | $target->add($n); |
| 139 | 145 | $previous = $n; |
| 140 | 146 | } |
@@ -171,8 +177,12 @@ discard block |
||
| 171 | 177 | return true; |
| 172 | 178 | } |
| 173 | 179 | if ($n->type & Y::BLANK) { |
| 174 | - if ($previous->type & Y::SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 175 | - if ($deepest->type & Y::LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
| 180 | + if ($previous->type & Y::SCALAR) { |
|
| 181 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 182 | + } |
|
| 183 | + if ($deepest->type & Y::LITTERALS) { |
|
| 184 | + $emptyLines[] = $n->setParent($deepest); |
|
| 185 | + } |
|
| 176 | 186 | return true; |
| 177 | 187 | } elseif ($n->type & Y::COMMENT |
| 178 | 188 | && !($previous->getParent()->value->type & Y::LITTERALS) |
@@ -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 | } |
@@ -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 | /** |
@@ -30,8 +30,12 @@ |
||
| 30 | 30 | public function jsonSerialize():array |
| 31 | 31 | { |
| 32 | 32 | $prop = get_object_vars($this); |
| 33 | - if (count($prop) > 0) return $prop; |
|
| 34 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 33 | + if (count($prop) > 0) { |
|
| 34 | + return $prop; |
|
| 35 | + } |
|
| 36 | + if (count($this) > 0) { |
|
| 37 | + return iterator_to_array($this); |
|
| 38 | + } |
|
| 35 | 39 | return $this;//new \Stdclass; |
| 36 | 40 | } |
| 37 | 41 | |
@@ -27,7 +27,7 @@ |
||
| 27 | 27 | if ($n->type & (Y::REF_CALL|Y::SCALAR)) return self::getScalar((string) $n->value); |
| 28 | 28 | if ($n->type & Y::JSON) return $n->value; |
| 29 | 29 | $expected = [Y::QUOTED => trim((string) $n->value, "\"'"), |
| 30 | - Y::RAW => strval((string) $n->value)]; |
|
| 30 | + Y::RAW => strval((string) $n->value)]; |
|
| 31 | 31 | return $expected[$n->type] ?? null; |
| 32 | 32 | } |
| 33 | 33 | |
@@ -23,9 +23,15 @@ 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((string) $n->value); |
|
| 28 | - if ($n->type & Y::JSON) return $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((string) $n->value); |
|
| 31 | + } |
|
| 32 | + if ($n->type & Y::JSON) { |
|
| 33 | + return $n->value; |
|
| 34 | + } |
|
| 29 | 35 | $expected = [Y::QUOTED => trim((string) $n->value, "\"'"), |
| 30 | 36 | Y::RAW => strval((string) $n->value)]; |
| 31 | 37 | return $expected[$n->type] ?? null; |
@@ -41,8 +47,12 @@ discard block |
||
| 41 | 47 | */ |
| 42 | 48 | private static function getScalar(string $v) |
| 43 | 49 | { |
| 44 | - if (R::isDate($v)) return self::$dateAsString ? $v : date_create($v); |
|
| 45 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 50 | + if (R::isDate($v)) { |
|
| 51 | + return self::$dateAsString ? $v : date_create($v); |
|
| 52 | + } |
|
| 53 | + if (R::isNumber($v)) { |
|
| 54 | + return self::getNumber($v); |
|
| 55 | + } |
|
| 46 | 56 | $types = ['yes' => true, |
| 47 | 57 | 'no' => false, |
| 48 | 58 | 'true' => true, |
@@ -64,8 +74,12 @@ discard block |
||
| 64 | 74 | */ |
| 65 | 75 | private static function getNumber(string $v) |
| 66 | 76 | { |
| 67 | - if (preg_match(R::OCTAL_NUM, $v)) return intval(base_convert($v, 8, 10)); |
|
| 68 | - if (preg_match(R::HEX_NUM, $v)) return intval(base_convert($v, 16, 10)); |
|
| 77 | + if (preg_match(R::OCTAL_NUM, $v)) { |
|
| 78 | + return intval(base_convert($v, 8, 10)); |
|
| 79 | + } |
|
| 80 | + if (preg_match(R::HEX_NUM, $v)) { |
|
| 81 | + return intval(base_convert($v, 16, 10)); |
|
| 82 | + } |
|
| 69 | 83 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
| 70 | 84 | } |
| 71 | 85 | } |
| 72 | 86 | \ No newline at end of file |