@@ -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 | } |
@@ -129,8 +129,12 @@ |
||
| 129 | 129 | try { |
| 130 | 130 | foreach ($generator as $lineNb => $lineString) { |
| 131 | 131 | $node = NodeFactory::get($lineString, $lineNb); |
| 132 | - if ($this->_debug === 1) echo get_class($node)."\n"; |
|
| 133 | - if ($this->needsSpecialProcess($node, $previous)) continue; |
|
| 132 | + if ($this->_debug === 1) { |
|
| 133 | + echo get_class($node)."\n"; |
|
| 134 | + } |
|
| 135 | + if ($this->needsSpecialProcess($node, $previous)) { |
|
| 136 | + continue; |
|
| 137 | + } |
|
| 134 | 138 | $this->_attachBlankLines($previous); |
| 135 | 139 | switch ($node->indent <=> $previous->indent) { |
| 136 | 140 | case -1: $target = $previous->getTargetOnLessIndent($node); |
@@ -112,8 +112,12 @@ discard block |
||
| 112 | 112 | \.nan | \.NaN | \.NAN tag:yaml.org,2002:float (Not a number) |
| 113 | 113 | * tag:yaml.org,2002:str (Default) |
| 114 | 114 | */ |
| 115 | - if (Regex::isDate($v)) return self::$dateAsObject && !$onlyScalar ? date_create($v) : $v; |
|
| 116 | - if (Regex::isNumber($v)) return self::getNumber($v); |
|
| 115 | + if (Regex::isDate($v)) { |
|
| 116 | + return self::$dateAsObject && !$onlyScalar ? date_create($v) : $v; |
|
| 117 | + } |
|
| 118 | + if (Regex::isNumber($v)) { |
|
| 119 | + return self::getNumber($v); |
|
| 120 | + } |
|
| 117 | 121 | $types = ['yes' => true, |
| 118 | 122 | 'no' => false, |
| 119 | 123 | 'true' => true, |
@@ -136,8 +140,12 @@ discard block |
||
| 136 | 140 | */ |
| 137 | 141 | private static function getNumber(string $v) |
| 138 | 142 | { |
| 139 | - if ((bool) preg_match(Regex::OCTAL_NUM, $v)) return intval(base_convert($v, 8, 10)); |
|
| 140 | - if ((bool) preg_match(Regex::HEX_NUM, $v)) return intval(base_convert($v, 16, 10)); |
|
| 143 | + if ((bool) preg_match(Regex::OCTAL_NUM, $v)) { |
|
| 144 | + return intval(base_convert($v, 8, 10)); |
|
| 145 | + } |
|
| 146 | + if ((bool) preg_match(Regex::HEX_NUM, $v)) { |
|
| 147 | + return intval(base_convert($v, 16, 10)); |
|
| 148 | + } |
|
| 141 | 149 | return is_bool(strpos($v, '.')) || substr_count($v, '.') > 1 ? intval($v) : floatval($v); |
| 142 | 150 | } |
| 143 | 151 | |
@@ -21,4 +21,6 @@ |
||
| 21 | 21 | if ($nameResultPair[$testName] !== $text) { |
| 22 | 22 | var_dump('EXPECTED', $nameResultPair[$testName]); |
| 23 | 23 | var_dump('RECEIVED', $text); |
| 24 | -} else echo 'OK'; |
|
| 24 | +} else { |
|
| 25 | + echo 'OK'; |
|
| 26 | +} |
|
@@ -22,10 +22,13 @@ discard block |
||
| 22 | 22 | final public static function get($nodeString = null, $line = 0):NodeGeneric |
| 23 | 23 | { |
| 24 | 24 | $trimmed = ltrim($nodeString); |
| 25 | - if ($trimmed === '') return new Nodes\Blank($nodeString, $line); |
|
| 26 | - elseif (substr($trimmed, 0, 3) === '...') return new Nodes\DocEnd($nodeString, $line); |
|
| 27 | - elseif ((bool) preg_match(Regex::KEY, $trimmed, $matches)) return new Nodes\Key($nodeString, $line, $matches); |
|
| 28 | - else { |
|
| 25 | + if ($trimmed === '') { |
|
| 26 | + return new Nodes\Blank($nodeString, $line); |
|
| 27 | + } elseif (substr($trimmed, 0, 3) === '...') { |
|
| 28 | + return new Nodes\DocEnd($nodeString, $line); |
|
| 29 | + } elseif ((bool) preg_match(Regex::KEY, $trimmed, $matches)) { |
|
| 30 | + return new Nodes\Key($nodeString, $line, $matches); |
|
| 31 | + } else { |
|
| 29 | 32 | $first = $trimmed[0]; |
| 30 | 33 | $stringGroups = ["-" ,'>|' ,'"\'',"#%" ,"{[" ,":?" ,'*&!']; |
| 31 | 34 | $methodGroups = ['Hyphen','Literal','Quoted','Special','Compact','SetElement','NodeAction']; |
@@ -96,10 +99,13 @@ discard block |
||
| 96 | 99 | final private static function onCompact(string $first, string $nodeString, int $line):NodeGeneric |
| 97 | 100 | { |
| 98 | 101 | json_decode($nodeString, false, 512, self::JSON_OPTIONS); |
| 99 | - if (json_last_error() === \JSON_ERROR_NONE) return new Nodes\JSON($nodeString, $line); |
|
| 100 | - elseif ((bool) preg_match(Regex::MAPPING, trim($nodeString))) return new Nodes\CompactMapping($nodeString, $line); |
|
| 101 | - elseif ((bool) preg_match(Regex::SEQUENCE, trim($nodeString))) return new Nodes\CompactSequence($nodeString, $line); |
|
| 102 | - else { |
|
| 102 | + if (json_last_error() === \JSON_ERROR_NONE) { |
|
| 103 | + return new Nodes\JSON($nodeString, $line); |
|
| 104 | + } elseif ((bool) preg_match(Regex::MAPPING, trim($nodeString))) { |
|
| 105 | + return new Nodes\CompactMapping($nodeString, $line); |
|
| 106 | + } elseif ((bool) preg_match(Regex::SEQUENCE, trim($nodeString))) { |
|
| 107 | + return new Nodes\CompactSequence($nodeString, $line); |
|
| 108 | + } else { |
|
| 103 | 109 | return new Nodes\Partial($nodeString, $line); |
| 104 | 110 | } |
| 105 | 111 | } |
@@ -114,9 +120,11 @@ discard block |
||
| 114 | 120 | */ |
| 115 | 121 | final private static function onHyphen(string $first, string $nodeString, int $line):NodeGeneric |
| 116 | 122 | { |
| 117 | - if (substr($nodeString, 0, 3) === '---') return new Nodes\DocStart($nodeString, $line); |
|
| 118 | - elseif ((bool) preg_match(Regex::ITEM, ltrim($nodeString))) return new Nodes\Item($nodeString, $line); |
|
| 119 | - else { |
|
| 123 | + if (substr($nodeString, 0, 3) === '---') { |
|
| 124 | + return new Nodes\DocStart($nodeString, $line); |
|
| 125 | + } elseif ((bool) preg_match(Regex::ITEM, ltrim($nodeString))) { |
|
| 126 | + return new Nodes\Item($nodeString, $line); |
|
| 127 | + } else { |
|
| 120 | 128 | return new Nodes\Scalar($nodeString, $line); |
| 121 | 129 | } |
| 122 | 130 | } |
@@ -25,7 +25,9 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | public function __construct(int $options = null) |
| 27 | 27 | { |
| 28 | - if (is_int($options)) $this->options = $options; |
|
| 28 | + if (is_int($options)) { |
|
| 29 | + $this->options = $options; |
|
| 30 | + } |
|
| 29 | 31 | } |
| 30 | 32 | |
| 31 | 33 | |
@@ -45,8 +47,12 @@ discard block |
||
| 45 | 47 | |
| 46 | 48 | public function dumpScalar($dataType, $indent):string |
| 47 | 49 | { |
| 48 | - if ($dataType === \INF) return '.inf'; |
|
| 49 | - if ($dataType === -\INF) return '-.inf'; |
|
| 50 | + if ($dataType === \INF) { |
|
| 51 | + return '.inf'; |
|
| 52 | + } |
|
| 53 | + if ($dataType === -\INF) { |
|
| 54 | + return '-.inf'; |
|
| 55 | + } |
|
| 50 | 56 | $precision = "%.".$this->floatPrecision."F"; |
| 51 | 57 | switch (gettype($dataType)) { |
| 52 | 58 | case 'boolean': return $dataType ? 'true' : 'false'; |
@@ -73,11 +79,19 @@ discard block |
||
| 73 | 79 | } |
| 74 | 80 | } |
| 75 | 81 | } elseif (is_object($compound)) { |
| 76 | - if ($compound instanceof YamlObject) return $this->dumpYamlObject($compound); |
|
| 77 | - if ($compound instanceof Tagged) return $this->dumpTagged($compound, $indent); |
|
| 78 | - if ($compound instanceof Compact) return $this->dumpCompact($compound, $indent); |
|
| 82 | + if ($compound instanceof YamlObject) { |
|
| 83 | + return $this->dumpYamlObject($compound); |
|
| 84 | + } |
|
| 85 | + if ($compound instanceof Tagged) { |
|
| 86 | + return $this->dumpTagged($compound, $indent); |
|
| 87 | + } |
|
| 88 | + if ($compound instanceof Compact) { |
|
| 89 | + return $this->dumpCompact($compound, $indent); |
|
| 90 | + } |
|
| 79 | 91 | //TODO: consider dumping datetime as date strings according to a format provided by user |
| 80 | - if ($compound instanceof \DateTime) return $compound->format(self::DATE_FORMAT); |
|
| 92 | + if ($compound instanceof \DateTime) { |
|
| 93 | + return $compound->format(self::DATE_FORMAT); |
|
| 94 | + } |
|
| 81 | 95 | $iterator = new \ArrayIterator($compound); |
| 82 | 96 | } |
| 83 | 97 | return $this->iteratorToString($iterator, $mask, $indent); |
@@ -50,7 +50,9 @@ discard block |
||
| 50 | 50 | $tmp->rewind(); |
| 51 | 51 | $fqn = __NAMESPACE__."\\Nodes\\$nodeType"; |
| 52 | 52 | foreach ($tmp as $child) { |
| 53 | - if ($child instanceof $fqn) return true; |
|
| 53 | + if ($child instanceof $fqn) { |
|
| 54 | + return true; |
|
| 55 | + } |
|
| 54 | 56 | } |
| 55 | 57 | return false; |
| 56 | 58 | } |
@@ -64,7 +66,9 @@ discard block |
||
| 64 | 66 | && !($child instanceof Directive) |
| 65 | 67 | && !($child instanceof Blank) |
| 66 | 68 | && !($child instanceof Docstart |
| 67 | - && is_null($child->value)) ) return true; |
|
| 69 | + && is_null($child->value)) ) { |
|
| 70 | + return true; |
|
| 71 | + } |
|
| 68 | 72 | } |
| 69 | 73 | return false; |
| 70 | 74 | } |
@@ -72,9 +76,11 @@ discard block |
||
| 72 | 76 | public function push($node) |
| 73 | 77 | { |
| 74 | 78 | $type = null; |
| 75 | - if ($node instanceof Item ) $type = self::SEQUENCE; |
|
| 76 | - elseif ($node instanceof Key) $type = self::MAPPING; |
|
| 77 | - elseif ($node instanceof SetKey |
|
| 79 | + if ($node instanceof Item ) { |
|
| 80 | + $type = self::SEQUENCE; |
|
| 81 | + } elseif ($node instanceof Key) { |
|
| 82 | + $type = self::MAPPING; |
|
| 83 | + } elseif ($node instanceof SetKey |
|
| 78 | 84 | || $node instanceof SetValue) { |
| 79 | 85 | $type = self::SET; |
| 80 | 86 | } elseif ($node instanceof Scalar ){ |
@@ -44,7 +44,9 @@ |
||
| 44 | 44 | */ |
| 45 | 45 | public static function toString($dataType, int $options = null):string |
| 46 | 46 | { |
| 47 | - if (empty($dataType)) throw new \Exception(self::class.": No content to convert to Yaml"); |
|
| 47 | + if (empty($dataType)) { |
|
| 48 | + throw new \Exception(self::class.": No content to convert to Yaml"); |
|
| 49 | + } |
|
| 48 | 50 | self::$options = is_int($options) ? $options : self::OPTIONS; |
| 49 | 51 | $dumpHandler = new DumperHandlers($options); |
| 50 | 52 | if (is_scalar($dataType)) { |
@@ -34,7 +34,9 @@ |
||
| 34 | 34 | $built = is_null($this->value) ? null : $this->value->build(); |
| 35 | 35 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
| 36 | 36 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 37 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($this->value, true)); |
|
| 37 | + if (empty($key)) { |
|
| 38 | + throw new \Exception("Cant serialize complex key: ".var_export($this->value, true)); |
|
| 39 | + } |
|
| 38 | 40 | $parent->{trim($key, '\'" ')} = null; |
| 39 | 41 | return null; |
| 40 | 42 | } |