@@ -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) { |
@@ -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 | /** |
@@ -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); |
@@ -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 | } |
@@ -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 |
@@ -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/multidoc_sequence.yml');//var_dump($content); |
|
| 15 | +$content = file_get_contents('./tests/cases/parsing/multidoc_sequence.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); |
@@ -17,7 +17,9 @@ discard block |
||
| 17 | 17 | public static function buildReference($node, $parent) |
| 18 | 18 | { |
| 19 | 19 | $tmp = is_null($node->value) ? null : Builder::build($node->value, $parent); |
| 20 | - if ($node->type === Y::REF_DEF) Builder::$_root->addReference($node->identifier, $tmp); |
|
| 20 | + if ($node->type === Y::REF_DEF) { |
|
| 21 | + Builder::$_root->addReference($node->identifier, $tmp); |
|
| 22 | + } |
|
| 21 | 23 | return Builder::$_root->getReference($node->identifier); |
| 22 | 24 | } |
| 23 | 25 | |
@@ -104,7 +106,9 @@ discard block |
||
| 104 | 106 | $built = is_object($node->value) ? Builder::build($node->value) : null; |
| 105 | 107 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
| 106 | 108 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 107 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 109 | + if (empty($key)) { |
|
| 110 | + throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
| 111 | + } |
|
| 108 | 112 | $parent->{trim($key, '\'" ')} = null; |
| 109 | 113 | } |
| 110 | 114 | |
@@ -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)) { |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | public function buildSetKey(Node $node, &$parent) |
| 99 | 99 | { |
| 100 | 100 | $built = is_object($node->value) ? Builder::build($node->value) : null; |
| 101 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
| 101 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
| 102 | 102 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 103 | 103 | if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
| 104 | 104 | $parent->{trim($key, '\'" ')} = null; |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | { |
| 115 | 115 | $prop = array_keys(get_object_vars($parent)); |
| 116 | 116 | $key = end($prop); |
| 117 | - if ($node->value->type & (Y::ITEM|Y::KEY )) { |
|
| 117 | + if ($node->value->type & (Y::ITEM|Y::KEY)) { |
|
| 118 | 118 | $node->value = new NodeList($node->value); |
| 119 | 119 | } |
| 120 | 120 | $parent->{$key} = Builder::build(/** @scrutinizer ignore-type */ $node->value); |
@@ -174,7 +174,7 @@ |
||
| 174 | 174 | elseif (in_array($first, ['?', ':'])) NodeHandlers::onSetElement($nodeValue, $this); |
| 175 | 175 | elseif (in_array($first, ['!', '&', '*'])) NodeHandlers::onNodeAction($nodeValue, $this); |
| 176 | 176 | else { |
| 177 | - $characters = [ '#' => [Y::COMMENT, $v], |
|
| 177 | + $characters = ['#' => [Y::COMMENT, $v], |
|
| 178 | 178 | '%' => [Y::DIRECTIVE, $v], |
| 179 | 179 | '>' => [Y::LITT_FOLDED, null], |
| 180 | 180 | '|' => [Y::LITT, null] |
@@ -68,8 +68,12 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function getParent(int $indent = null, $type = 0):Node |
| 70 | 70 | { |
| 71 | - if ($this->type === Y::ROOT) return $this; |
|
| 72 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
| 71 | + if ($this->type === Y::ROOT) { |
|
| 72 | + return $this; |
|
| 73 | + } |
|
| 74 | + if (!is_int($indent)) { |
|
| 75 | + return $this->parent ?? $this; |
|
| 76 | + } |
|
| 73 | 77 | $cursor = $this; |
| 74 | 78 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
| 75 | 79 | if ($cursor->indent === $indent && $cursor->type !== $type) { |
@@ -100,7 +104,7 @@ discard block |
||
| 100 | 104 | if (is_null($this->value)) { |
| 101 | 105 | $this->value = $child; |
| 102 | 106 | return; |
| 103 | - }elseif ($this->value instanceof Node) { |
|
| 107 | + } elseif ($this->value instanceof Node) { |
|
| 104 | 108 | if ($this->value->type & Y::LITTERALS) { |
| 105 | 109 | $type = $this->value->type; |
| 106 | 110 | $this->value = new NodeList(); |
@@ -168,12 +172,17 @@ discard block |
||
| 168 | 172 | { |
| 169 | 173 | $v = ltrim(substr($nodeValue, 1)); |
| 170 | 174 | $first = $nodeValue[0]; |
| 171 | - if ($first === "-") NodeHandlers::onHyphen($nodeValue, $this); |
|
| 172 | - elseif (in_array($first, ['"', "'"])) NodeHandlers::onQuoted($nodeValue, $this); |
|
| 173 | - elseif (in_array($first, ['{', '['])) NodeHandlers::onCompact($nodeValue, $this); |
|
| 174 | - elseif (in_array($first, ['?', ':'])) NodeHandlers::onSetElement($nodeValue, $this); |
|
| 175 | - elseif (in_array($first, ['!', '&', '*'])) NodeHandlers::onNodeAction($nodeValue, $this); |
|
| 176 | - else { |
|
| 175 | + if ($first === "-") { |
|
| 176 | + NodeHandlers::onHyphen($nodeValue, $this); |
|
| 177 | + } elseif (in_array($first, ['"', "'"])) { |
|
| 178 | + NodeHandlers::onQuoted($nodeValue, $this); |
|
| 179 | + } elseif (in_array($first, ['{', '['])) { |
|
| 180 | + NodeHandlers::onCompact($nodeValue, $this); |
|
| 181 | + } elseif (in_array($first, ['?', ':'])) { |
|
| 182 | + NodeHandlers::onSetElement($nodeValue, $this); |
|
| 183 | + } elseif (in_array($first, ['!', '&', '*'])) { |
|
| 184 | + NodeHandlers::onNodeAction($nodeValue, $this); |
|
| 185 | + } else { |
|
| 177 | 186 | $characters = [ '#' => [Y::COMMENT, $v], |
| 178 | 187 | '%' => [Y::DIRECTIVE, $v], |
| 179 | 188 | '>' => [Y::LITT_FOLDED, null], |
@@ -66,28 +66,28 @@ |
||
| 66 | 66 | public static function onCompact($value, Node $node) |
| 67 | 67 | { |
| 68 | 68 | $node->value = json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 69 | - if (json_last_error() === JSON_ERROR_NONE){ |
|
| 69 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
| 70 | 70 | $node->type = Y::JSON; |
| 71 | 71 | return; |
| 72 | 72 | } |
| 73 | 73 | $node->value = new NodeList(); |
| 74 | - if (preg_match(R::MAPPING, $value)){ |
|
| 74 | + if (preg_match(R::MAPPING, $value)) { |
|
| 75 | 75 | $node->type = Y::COMPACT_MAPPING; |
| 76 | 76 | $node->value->type = Y::COMPACT_MAPPING; |
| 77 | - preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1,-1)), $matches); |
|
| 77 | + preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1, -1)), $matches); |
|
| 78 | 78 | foreach ($matches['k'] as $index => $property) { |
| 79 | 79 | $n = new Node('', $node->line); |
| 80 | 80 | $n->type = Y::KEY; |
| 81 | - $n->identifier = trim($property, '"\' ');//TODO : maybe check for proper quoting first ? |
|
| 81 | + $n->identifier = trim($property, '"\' '); //TODO : maybe check for proper quoting first ? |
|
| 82 | 82 | $n->value = new Node($matches['v'][$index], $node->line); |
| 83 | 83 | $node->value->push($n); |
| 84 | 84 | } |
| 85 | 85 | return; |
| 86 | 86 | } |
| 87 | - if (preg_match(R::SEQUENCE, $value)){ |
|
| 87 | + if (preg_match(R::SEQUENCE, $value)) { |
|
| 88 | 88 | $node->type = Y::COMPACT_SEQUENCE; |
| 89 | 89 | $node->value->type = Y::COMPACT_SEQUENCE; |
| 90 | - preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1,-1)), $matches); |
|
| 90 | + preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1, -1)), $matches); |
|
| 91 | 91 | foreach ($matches['item'] as $key => $item) { |
| 92 | 92 | $i = new Node('', $node->line); |
| 93 | 93 | $i->type = Y::ITEM; |