@@ -31,7 +31,9 @@ |
||
| 31 | 31 | $built = is_null($this->value) ? null : $this->value->build(); |
| 32 | 32 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
| 33 | 33 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 34 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($this->value, true)); |
|
| 34 | + if (empty($key)) { |
|
| 35 | + throw new \Exception("Cant serialize complex key: ".var_export($this->value, true)); |
|
| 36 | + } |
|
| 35 | 37 | $parent->{trim($key, '\'" ')} = null; |
| 36 | 38 | } |
| 37 | 39 | |
@@ -74,8 +74,12 @@ |
||
| 74 | 74 | { |
| 75 | 75 | $prop = get_object_vars($this); |
| 76 | 76 | unset($prop["__yaml__object__api"]); |
| 77 | - if (count($prop) > 0) return $prop; |
|
| 78 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 77 | + if (count($prop) > 0) { |
|
| 78 | + return $prop; |
|
| 79 | + } |
|
| 80 | + if (count($this) > 0) { |
|
| 81 | + return iterator_to_array($this); |
|
| 82 | + } |
|
| 79 | 83 | return $this->__yaml__object__api->value ?? "_Empty YamlObject_"; |
| 80 | 84 | } |
| 81 | 85 | } |
@@ -16,10 +16,13 @@ discard block |
||
| 16 | 16 | final public static function get($nodeString = null, $line = 0):Node |
| 17 | 17 | { |
| 18 | 18 | $trimmed = ltrim($nodeString); |
| 19 | - if ($trimmed === '') return new NodeBlank($nodeString, $line); |
|
| 20 | - elseif (substr($trimmed, 0, 3) === '...') return new NodeDocEnd($nodeString, $line); |
|
| 21 | - elseif (preg_match(Regex::KEY, $trimmed, $matches)) return new NodeKey($nodeString, $line, $matches); |
|
| 22 | - else { |
|
| 19 | + if ($trimmed === '') { |
|
| 20 | + return new NodeBlank($nodeString, $line); |
|
| 21 | + } elseif (substr($trimmed, 0, 3) === '...') { |
|
| 22 | + return new NodeDocEnd($nodeString, $line); |
|
| 23 | + } elseif (preg_match(Regex::KEY, $trimmed, $matches)) { |
|
| 24 | + return new NodeKey($nodeString, $line, $matches); |
|
| 25 | + } else { |
|
| 23 | 26 | $first = $trimmed[0]; |
| 24 | 27 | $stringGroups = ["-" ,'>|' ,'"\'',"#%" ,"{[" ,":?" ,'*&!']; |
| 25 | 28 | $methodGroups = ['Hyphen','Literal','Quoted','Special','Compact','SetElement','NodeAction']; |
@@ -90,10 +93,13 @@ discard block |
||
| 90 | 93 | final private static function onCompact(string $first, string $nodeString, int $line):Node |
| 91 | 94 | { |
| 92 | 95 | json_decode($nodeString, false, 512, self::JSON_OPTIONS); |
| 93 | - if (json_last_error() === \JSON_ERROR_NONE) return new NodeJSON($nodeString, $line); |
|
| 94 | - elseif (preg_match(Regex::MAPPING, trim($nodeString))) return new NodeCompactMapping($nodeString, $line); |
|
| 95 | - elseif (preg_match(Regex::SEQUENCE, trim($nodeString))) return new NodeCompactSequence($nodeString, $line); |
|
| 96 | - else { |
|
| 96 | + if (json_last_error() === \JSON_ERROR_NONE) { |
|
| 97 | + return new NodeJSON($nodeString, $line); |
|
| 98 | + } elseif (preg_match(Regex::MAPPING, trim($nodeString))) { |
|
| 99 | + return new NodeCompactMapping($nodeString, $line); |
|
| 100 | + } elseif (preg_match(Regex::SEQUENCE, trim($nodeString))) { |
|
| 101 | + return new NodeCompactSequence($nodeString, $line); |
|
| 102 | + } else { |
|
| 97 | 103 | return new NodePartial($nodeString, $line); |
| 98 | 104 | } |
| 99 | 105 | } |
@@ -108,9 +114,11 @@ discard block |
||
| 108 | 114 | */ |
| 109 | 115 | final private static function onHyphen(string $first, string $nodeString, int $line):Node |
| 110 | 116 | { |
| 111 | - if (substr($nodeString, 0, 3) === '---') return new NodeDocStart($nodeString, $line); |
|
| 112 | - elseif (preg_match(Regex::ITEM, ltrim($nodeString))) return new NodeItem($nodeString, $line); |
|
| 113 | - else { |
|
| 117 | + if (substr($nodeString, 0, 3) === '---') { |
|
| 118 | + return new NodeDocStart($nodeString, $line); |
|
| 119 | + } elseif (preg_match(Regex::ITEM, ltrim($nodeString))) { |
|
| 120 | + return new NodeItem($nodeString, $line); |
|
| 121 | + } else { |
|
| 114 | 122 | return new NodeScalar($nodeString, $line); |
| 115 | 123 | } |
| 116 | 124 | } |
@@ -41,7 +41,9 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | public static function toString($dataType, int $options = null):string |
| 43 | 43 | { |
| 44 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml"); |
|
| 44 | + if (is_null($dataType)) { |
|
| 45 | + throw new \Exception(self::class.": No content to convert to Yaml"); |
|
| 46 | + } |
|
| 45 | 47 | self::$options = is_int($options) ? $options : self::OPTIONS; |
| 46 | 48 | self::$result = new DLL; |
| 47 | 49 | if ($dataType instanceof YamlObject) { |
@@ -83,8 +85,12 @@ discard block |
||
| 83 | 85 | private static function dump($dataType, int $indent) |
| 84 | 86 | { |
| 85 | 87 | if (is_scalar($dataType)) { |
| 86 | - if ($dataType === \INF) return '.inf'; |
|
| 87 | - if ($dataType === -\INF) return '-.inf'; |
|
| 88 | + if ($dataType === \INF) { |
|
| 89 | + return '.inf'; |
|
| 90 | + } |
|
| 91 | + if ($dataType === -\INF) { |
|
| 92 | + return '-.inf'; |
|
| 93 | + } |
|
| 88 | 94 | switch (gettype($dataType)) { |
| 89 | 95 | case 'boolean': return $dataType ? 'true' : 'false'; |
| 90 | 96 | case 'float': //fall through |
@@ -106,7 +112,9 @@ discard block |
||
| 106 | 112 | */ |
| 107 | 113 | private static function dumpYamlObject(YamlObject $obj) |
| 108 | 114 | { |
| 109 | - if ($obj->hasDocStart() && self::$result instanceof DLL) self::$result->push("---"); |
|
| 115 | + if ($obj->hasDocStart() && self::$result instanceof DLL) { |
|
| 116 | + self::$result->push("---"); |
|
| 117 | + } |
|
| 110 | 118 | // self::dump($obj, 0); |
| 111 | 119 | if (count($obj) > 0) { |
| 112 | 120 | self::dumpArray($obj->getArrayCopy(), 0); |
@@ -181,9 +189,13 @@ discard block |
||
| 181 | 189 | self::add(self::dump($obj->value, $indent + self::INDENT), $indent + self::INDENT); |
| 182 | 190 | } |
| 183 | 191 | } |
| 184 | - if ($obj instanceof Compact) return self::dumpCompact($obj, $indent); |
|
| 192 | + if ($obj instanceof Compact) { |
|
| 193 | + return self::dumpCompact($obj, $indent); |
|
| 194 | + } |
|
| 185 | 195 | //TODO: consider dumping datetime as date strings according to a format provided by user or default |
| 186 | - if ($obj instanceof \DateTime) return $obj->format(self::DATE_FORMAT); |
|
| 196 | + if ($obj instanceof \DateTime) { |
|
| 197 | + return $obj->format(self::DATE_FORMAT); |
|
| 198 | + } |
|
| 187 | 199 | $propList = get_object_vars($obj); |
| 188 | 200 | foreach ($propList as $property => $value) { |
| 189 | 201 | if (is_scalar($value) || $value instanceof Compact || $value instanceof \DateTime) { |
@@ -210,13 +210,23 @@ |
||
| 210 | 210 | { |
| 211 | 211 | $props = []; |
| 212 | 212 | $props['line->indent'] = "$this->line -> $this->indent"; |
| 213 | - if ($this->identifier) $props['identifier'] = "($this->identifier)"; |
|
| 214 | - if ($this->anchor) $props['anchor'] = "($this->anchor)"; |
|
| 215 | - if ($this->tag) $props['tag'] = "($this->tag)"; |
|
| 216 | - if ($this->value) $props['value'] = $this->value; |
|
| 213 | + if ($this->identifier) { |
|
| 214 | + $props['identifier'] = "($this->identifier)"; |
|
| 215 | + } |
|
| 216 | + if ($this->anchor) { |
|
| 217 | + $props['anchor'] = "($this->anchor)"; |
|
| 218 | + } |
|
| 219 | + if ($this->tag) { |
|
| 220 | + $props['tag'] = "($this->tag)"; |
|
| 221 | + } |
|
| 222 | + if ($this->value) { |
|
| 223 | + $props['value'] = $this->value; |
|
| 224 | + } |
|
| 217 | 225 | // $props['value'] = $this->value; |
| 218 | 226 | $props['raw'] = $this->raw; |
| 219 | - if (!$this->_parent) $props['parent'] = 'NO PARENT!!!'; |
|
| 227 | + if (!$this->_parent) { |
|
| 228 | + $props['parent'] = 'NO PARENT!!!'; |
|
| 229 | + } |
|
| 220 | 230 | return $props; |
| 221 | 231 | } |
| 222 | 232 | } |
@@ -93,7 +93,9 @@ discard block |
||
| 93 | 93 | { |
| 94 | 94 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
| 95 | 95 | //TODO : be more permissive on $strContent values |
| 96 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 96 | + if (!is_array($source) || !count($source)) { |
|
| 97 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 98 | + } |
|
| 97 | 99 | foreach ($source as $key => $value) { |
| 98 | 100 | yield ++$key => $value; |
| 99 | 101 | } |
@@ -116,8 +118,12 @@ discard block |
||
| 116 | 118 | try { |
| 117 | 119 | foreach ($generator as $lineNb => $lineString) { |
| 118 | 120 | $node = NodeFactory::get($lineString, $lineNb); |
| 119 | - if ($this->_debug === 1) echo get_class($node)."\n"; |
|
| 120 | - if ($this->needsSpecialProcess($node, $previous)) continue; |
|
| 121 | + if ($this->_debug === 1) { |
|
| 122 | + echo get_class($node)."\n"; |
|
| 123 | + } |
|
| 124 | + if ($this->needsSpecialProcess($node, $previous)) { |
|
| 125 | + continue; |
|
| 126 | + } |
|
| 121 | 127 | $this->attachBlankLines($previous); |
| 122 | 128 | switch ($node->indent <=> $previous->indent) { |
| 123 | 129 | case -1: $target = $previous->getTargetOnLessIndent($node); |
@@ -25,7 +25,9 @@ |
||
| 25 | 25 | |
| 26 | 26 | public function add(Node $child):Node |
| 27 | 27 | { |
| 28 | - if (is_null($this->value)) $this->value = new NodeList(); |
|
| 28 | + if (is_null($this->value)) { |
|
| 29 | + $this->value = new NodeList(); |
|
| 30 | + } |
|
| 29 | 31 | $candidate = $child; |
| 30 | 32 | if (!Yaml::isOneOf($child, ['NodeScalar', 'NodeBlank', 'NodeComment', 'NodeQuoted'])) { |
| 31 | 33 | $candidate = new NodeScalar((string) $child->raw, $child->line); |
@@ -107,7 +107,9 @@ |
||
| 107 | 107 | { |
| 108 | 108 | foreach ($comparison as $className) { |
| 109 | 109 | $fqn = __NAMESPACE__."\\Yaml\\$className"; |
| 110 | - if ($subject instanceof $fqn) return true; |
|
| 110 | + if ($subject instanceof $fqn) { |
|
| 111 | + return true; |
|
| 112 | + } |
|
| 111 | 113 | } |
| 112 | 114 | return false; |
| 113 | 115 | } |
@@ -37,7 +37,9 @@ discard block |
||
| 37 | 37 | $tmp->rewind(); |
| 38 | 38 | $fqn = __NAMESPACE__."\\$nodeType"; |
| 39 | 39 | foreach ($tmp as $child) { |
| 40 | - if ($child instanceof $fqn) return true; |
|
| 40 | + if ($child instanceof $fqn) { |
|
| 41 | + return true; |
|
| 42 | + } |
|
| 41 | 43 | } |
| 42 | 44 | return false; |
| 43 | 45 | } |
@@ -51,7 +53,9 @@ discard block |
||
| 51 | 53 | && !($child instanceof NodeDirective) |
| 52 | 54 | && !($child instanceof NodeBlank) |
| 53 | 55 | && !($child instanceof NodeDocstart |
| 54 | - && is_null($child->value)) ) return true; |
|
| 56 | + && is_null($child->value)) ) { |
|
| 57 | + return true; |
|
| 58 | + } |
|
| 55 | 59 | } |
| 56 | 60 | return false; |
| 57 | 61 | } |
@@ -59,9 +63,11 @@ discard block |
||
| 59 | 63 | public function push($node) |
| 60 | 64 | { |
| 61 | 65 | $type = null; |
| 62 | - if ($node instanceof NodeItem ) $type = self::SEQUENCE; |
|
| 63 | - elseif ($node instanceof NodeKey) $type = self::MAPPING; |
|
| 64 | - elseif ($node instanceof NodeSetKey |
|
| 66 | + if ($node instanceof NodeItem ) { |
|
| 67 | + $type = self::SEQUENCE; |
|
| 68 | + } elseif ($node instanceof NodeKey) { |
|
| 69 | + $type = self::MAPPING; |
|
| 70 | + } elseif ($node instanceof NodeSetKey |
|
| 65 | 71 | || $node instanceof NodeSetValue) { |
| 66 | 72 | $type = self::SET; |
| 67 | 73 | } elseif ($node instanceof NodeScalar ){ |