@@ -4,13 +4,13 @@ |
||
| 4 | 4 | use Dallgoot\Yaml\API as API; |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | - * @method void addReference(string $name, $value) |
|
| 8 | - * @method mixed getReference(string $name) |
|
| 9 | - * @method array getAllReferences() |
|
| 10 | - * @method void addComment($index, $value) |
|
| 11 | - * @method string|array getComment($lineNumber) |
|
| 12 | - * @method void setText(string $value) |
|
| 13 | - * @method void addTag(string $value) |
|
| 7 | + * @method void addReference(string $name, $value) |
|
| 8 | + * @method mixed getReference(string $name) |
|
| 9 | + * @method array getAllReferences() |
|
| 10 | + * @method void addComment($index, $value) |
|
| 11 | + * @method string|array getComment($lineNumber) |
|
| 12 | + * @method void setText(string $value) |
|
| 13 | + * @method void addTag(string $value) |
|
| 14 | 14 | */ |
| 15 | 15 | class YamlObject extends \ArrayIterator implements \JsonSerializable |
| 16 | 16 | { |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | public function __construct() |
| 22 | 22 | { |
| 23 | - parent::__construct([], 1);//1 = Array indices can be accessed as properties in read/write. |
|
| 23 | + parent::__construct([], 1); //1 = Array indices can be accessed as properties in read/write. |
|
| 24 | 24 | $this->__yaml__object__api = new API(); |
| 25 | 25 | } |
| 26 | 26 | |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | public function __call($funcName, $arguments) |
| 29 | 29 | { |
| 30 | 30 | $reflectAPI = new \ReflectionClass(get_class($this->__yaml__object__api)); |
| 31 | - $getName = function ($o) { return $o->name; }; |
|
| 31 | + $getName = function($o) { return $o->name; }; |
|
| 32 | 32 | $publicApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PUBLIC)); |
| 33 | 33 | $privateApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PRIVATE)); |
| 34 | 34 | if (!in_array($funcName, $publicApi) && !in_array($funcName, $privateApi)) { |
@@ -80,8 +80,8 @@ discard block |
||
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | - * CAUTION : the types assumed here are NOT FINAL : they CAN be adjusted according to parent |
|
| 84 | - */ |
|
| 83 | + * CAUTION : the types assumed here are NOT FINAL : they CAN be adjusted according to parent |
|
| 84 | + */ |
|
| 85 | 85 | public function parse(String $nodeString):Node |
| 86 | 86 | { |
| 87 | 87 | $nodeValue = preg_replace("/^\t+/m", " ", $nodeString);//permissive to tabs but replacement |
@@ -214,13 +214,13 @@ discard block |
||
| 214 | 214 | private static function getScalar($v) |
| 215 | 215 | { |
| 216 | 216 | $types = ['yes' => true, |
| 217 | - 'no' => false, |
|
| 218 | - 'true' => true, |
|
| 219 | - 'false' => false, |
|
| 220 | - 'null' => null, |
|
| 221 | - '.inf' => INF, |
|
| 222 | - '-.inf' => -INF, |
|
| 223 | - '.nan' => NAN |
|
| 217 | + 'no' => false, |
|
| 218 | + 'true' => true, |
|
| 219 | + 'false' => false, |
|
| 220 | + 'null' => null, |
|
| 221 | + '.inf' => INF, |
|
| 222 | + '-.inf' => -INF, |
|
| 223 | + '.nan' => NAN |
|
| 224 | 224 | ]; |
| 225 | 225 | if (in_array(strtolower($v), array_keys($types))) return $types[strtolower($v)]; |
| 226 | 226 | if (R::isDate($v)) return date_create($v); |
@@ -63,9 +63,9 @@ discard block |
||
| 63 | 63 | //modify type according to child |
| 64 | 64 | if ($this->value instanceof DLL && !property_exists($this->value, "type")) { |
| 65 | 65 | switch ($child->type) { |
| 66 | - case T::KEY: $this->value->type = T::MAPPING;break; |
|
| 67 | - case T::ITEM: $this->value->type = T::SEQUENCE;break; |
|
| 68 | - case T::SCALAR: $this->value->type = $this->type;break; |
|
| 66 | + case T::KEY: $this->value->type = T::MAPPING; break; |
|
| 67 | + case T::ITEM: $this->value->type = T::SEQUENCE; break; |
|
| 68 | + case T::SCALAR: $this->value->type = $this->type; break; |
|
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | 71 | } |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | */ |
| 85 | 85 | public function parse(String $nodeString):Node |
| 86 | 86 | { |
| 87 | - $nodeValue = preg_replace("/^\t+/m", " ", $nodeString);//permissive to tabs but replacement |
|
| 87 | + $nodeValue = preg_replace("/^\t+/m", " ", $nodeString); //permissive to tabs but replacement |
|
| 88 | 88 | $this->indent = strspn($nodeValue, ' '); |
| 89 | 89 | $nodeValue = ltrim($nodeValue); |
| 90 | 90 | if ($nodeValue === '') { |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | case '#': return [T::COMMENT, ltrim($v)]; |
| 121 | 121 | case "-": return $this->onHyphen($nodeValue); |
| 122 | 122 | case '%': return [T::DIRECTIVE, ltrim($v)]; |
| 123 | - case '?': return [T::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 123 | + case '?': return [T::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 124 | 124 | case ':': return [T::SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
| 125 | 125 | case '>': return [T::LITTERAL_FOLDED, null]; |
| 126 | 126 | case '|': return [T::LITTERAL, null]; |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | if (!is_bool($hasComment)) { |
| 141 | 141 | $tmpNode = new Node(trim(substr($keyValue, 0, $hasComment)), $this->line); |
| 142 | 142 | if ($tmpNode->type !== T::PARTIAL) { |
| 143 | - $comment = new Node(trim(substr($keyValue, $hasComment+1)), $this->line); |
|
| 143 | + $comment = new Node(trim(substr($keyValue, $hasComment + 1)), $this->line); |
|
| 144 | 144 | //TODO: modify "identifier" to specify if fullline comment or not |
| 145 | 145 | $this->add($comment); |
| 146 | 146 | $n = $tmpNode; |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | $type = ['!' => T::TAG, '&' => T::REF_DEF, '*' => T::REF_CALL][$nodeValue[0]]; |
| 187 | 187 | $pos = strpos($v, ' '); |
| 188 | 188 | $this->identifier = is_bool($pos) ? $v : strstr($v, ' ', true); |
| 189 | - $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos+1)), $this->line))->setParent($this); |
|
| 189 | + $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos + 1)), $this->line))->setParent($this); |
|
| 190 | 190 | return [$type, $n]; |
| 191 | 191 | } |
| 192 | 192 | |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | case T::MAPPING_SHORT: return self::getShortMapping(substr($v, 1, -1)); |
| 204 | 204 | //TODO : that's not robust enough, improve it |
| 205 | 205 | case T::SEQUENCE_SHORT: |
| 206 | - $f = function ($e) { return self::getScalar(trim($e));}; |
|
| 206 | + $f = function($e) { return self::getScalar(trim($e)); }; |
|
| 207 | 207 | return array_map($f, explode(",", substr($v, 1, -1))); |
| 208 | 208 | default: |
| 209 | 209 | trigger_error("Error can not get PHP type for ".T::getName($this->type), E_USER_WARNING); |
@@ -38,7 +38,9 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | public function getParent($indent = null):Node |
| 40 | 40 | { |
| 41 | - if (is_null($indent)) return $this->parent ?? $this; |
|
| 41 | + if (is_null($indent)) { |
|
| 42 | + return $this->parent ?? $this; |
|
| 43 | + } |
|
| 42 | 44 | $cursor = $this; |
| 43 | 45 | while ($cursor->indent >= $indent) { |
| 44 | 46 | $cursor = $cursor->parent; |
@@ -50,7 +52,9 @@ discard block |
||
| 50 | 52 | { |
| 51 | 53 | $child->setParent($this); |
| 52 | 54 | $current = $this->value; |
| 53 | - if (in_array($this->type, T::$LITTERALS)) $child->type = T::SCALAR; |
|
| 55 | + if (in_array($this->type, T::$LITTERALS)) { |
|
| 56 | + $child->type = T::SCALAR; |
|
| 57 | + } |
|
| 54 | 58 | if (is_null($current)) { |
| 55 | 59 | $this->value = $child; |
| 56 | 60 | return; |
@@ -114,8 +118,12 @@ discard block |
||
| 114 | 118 | $type = R::isProperlyQuoted($nodeValue) ? T::QUOTED : T::PARTIAL; |
| 115 | 119 | return [$type, $nodeValue]; |
| 116 | 120 | } |
| 117 | - if (in_array($nodeValue[0], ['{', '['])) return $this->onObject($nodeValue); |
|
| 118 | - if (in_array($nodeValue[0], ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
|
| 121 | + if (in_array($nodeValue[0], ['{', '['])) { |
|
| 122 | + return $this->onObject($nodeValue); |
|
| 123 | + } |
|
| 124 | + if (in_array($nodeValue[0], ['!', '&', '*'])) { |
|
| 125 | + return $this->onNodeAction($nodeValue); |
|
| 126 | + } |
|
| 119 | 127 | switch ($nodeValue[0]) { |
| 120 | 128 | case '#': return [T::COMMENT, ltrim($v)]; |
| 121 | 129 | case "-": return $this->onHyphen($nodeValue); |
@@ -154,9 +162,15 @@ discard block |
||
| 154 | 162 | private function onObject($value):array |
| 155 | 163 | { |
| 156 | 164 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 157 | - if (json_last_error() === JSON_ERROR_NONE) return [T::JSON, $value]; |
|
| 158 | - if (preg_match(R::MAPPING, $value)) return [T::MAPPING_SHORT, $value]; |
|
| 159 | - if (preg_match(R::SEQUENCE, $value)) return [T::SEQUENCE_SHORT, $value]; |
|
| 165 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
| 166 | + return [T::JSON, $value]; |
|
| 167 | + } |
|
| 168 | + if (preg_match(R::MAPPING, $value)) { |
|
| 169 | + return [T::MAPPING_SHORT, $value]; |
|
| 170 | + } |
|
| 171 | + if (preg_match(R::SEQUENCE, $value)) { |
|
| 172 | + return [T::SEQUENCE_SHORT, $value]; |
|
| 173 | + } |
|
| 160 | 174 | return [T::PARTIAL, $value]; |
| 161 | 175 | } |
| 162 | 176 | |
@@ -164,7 +178,9 @@ discard block |
||
| 164 | 178 | { |
| 165 | 179 | if (substr($nodeValue, 0, 3) === '---') { |
| 166 | 180 | $rest = trim(substr($nodeValue, 3)); |
| 167 | - if (empty($rest)) return [T::DOC_START, null]; |
|
| 181 | + if (empty($rest)) { |
|
| 182 | + return [T::DOC_START, null]; |
|
| 183 | + } |
|
| 168 | 184 | $n = new Node($rest, $this->line); |
| 169 | 185 | $n->indent = $this->indent + 4; |
| 170 | 186 | return [T::DOC_START, $n->setParent($this)]; |
@@ -193,7 +209,9 @@ discard block |
||
| 193 | 209 | public function getPhpValue() |
| 194 | 210 | { |
| 195 | 211 | $v = $this->value; |
| 196 | - if (is_null($v)) return null; |
|
| 212 | + if (is_null($v)) { |
|
| 213 | + return null; |
|
| 214 | + } |
|
| 197 | 215 | switch ($this->type) { |
| 198 | 216 | case T::JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
| 199 | 217 | case T::QUOTED: return substr($v, 1, -1); |
@@ -222,16 +240,26 @@ discard block |
||
| 222 | 240 | '-.inf' => -INF, |
| 223 | 241 | '.nan' => NAN |
| 224 | 242 | ]; |
| 225 | - if (in_array(strtolower($v), array_keys($types))) return $types[strtolower($v)]; |
|
| 226 | - if (R::isDate($v)) return date_create($v); |
|
| 227 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 243 | + if (in_array(strtolower($v), array_keys($types))) { |
|
| 244 | + return $types[strtolower($v)]; |
|
| 245 | + } |
|
| 246 | + if (R::isDate($v)) { |
|
| 247 | + return date_create($v); |
|
| 248 | + } |
|
| 249 | + if (R::isNumber($v)) { |
|
| 250 | + return self::getNumber($v); |
|
| 251 | + } |
|
| 228 | 252 | return strval($v); |
| 229 | 253 | } |
| 230 | 254 | |
| 231 | 255 | private static function getNumber($v) |
| 232 | 256 | { |
| 233 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
| 234 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
| 257 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
| 258 | + return intval(base_convert($v, 8, 10)); |
|
| 259 | + } |
|
| 260 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
| 261 | + return intval(base_convert($v, 16, 10)); |
|
| 262 | + } |
|
| 235 | 263 | // if preg_match("/^([\d.]+e[-+]\d{1,2})$/", $v)://fall through |
| 236 | 264 | // if preg_match("/^([-+]?(?:\d+|\d*.\d+))$/", $v): |
| 237 | 265 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
@@ -254,7 +282,9 @@ discard block |
||
| 254 | 282 | 'indent'=> $this->indent, |
| 255 | 283 | 'type' => T::getName($this->type), |
| 256 | 284 | 'value' => $this->value]; |
| 257 | - if (!is_null($this->identifier)) $out['type'] .= "($this->identifier)"; |
|
| 285 | + if (!is_null($this->identifier)) { |
|
| 286 | + $out['type'] .= "($this->identifier)"; |
|
| 287 | + } |
|
| 258 | 288 | return $out; |
| 259 | 289 | } |
| 260 | 290 | } |
@@ -9,14 +9,14 @@ discard block |
||
| 9 | 9 | { |
| 10 | 10 | public $errors = []; |
| 11 | 11 | //options |
| 12 | - public const EXCLUDE_DIRECTIVES = 0001;//DONT include_directive |
|
| 13 | - public const IGNORE_COMMENTS = 0010;//DONT include_comments |
|
| 14 | - public const EXCEPTIONS_PARSING = 0100;//THROW Exception on parsing Errors |
|
| 15 | - public const NO_OBJECT_FOR_DATE = 1000;//DONT import date strings as dateTime Object |
|
| 12 | + public const EXCLUDE_DIRECTIVES = 0001; //DONT include_directive |
|
| 13 | + public const IGNORE_COMMENTS = 0010; //DONT include_comments |
|
| 14 | + public const EXCEPTIONS_PARSING = 0100; //THROW Exception on parsing Errors |
|
| 15 | + public const NO_OBJECT_FOR_DATE = 1000; //DONT import date strings as dateTime Object |
|
| 16 | 16 | // |
| 17 | 17 | private $content; |
| 18 | 18 | private $filePath; |
| 19 | - private $debug = 0;//TODO: determine levels |
|
| 19 | + private $debug = 0; //TODO: determine levels |
|
| 20 | 20 | private $options = 0; |
| 21 | 21 | //Exceptions |
| 22 | 22 | const INVALID_VALUE = self::class.": at line %d"; |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | $specialTypes = [T::LITTERAL, T::LITTERAL_FOLDED, T::EMPTY]; |
| 75 | 75 | try { |
| 76 | 76 | foreach ($source as $lineNb => $lineString) { |
| 77 | - $n = new Node($lineString, $lineNb + 1);//TODO: useful???-> $this->debug && var_dump($n); |
|
| 77 | + $n = new Node($lineString, $lineNb + 1); //TODO: useful???-> $this->debug && var_dump($n); |
|
| 78 | 78 | $parent = $previous; |
| 79 | 79 | $deepest = $previous->getDeepestNode(); |
| 80 | 80 | if ($deepest->type === T::PARTIAL) { |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | switch ($deepest->type) { |
| 147 | 147 | case T::LITTERAL: |
| 148 | 148 | case T::LITTERAL_FOLDED: |
| 149 | - $n->value = trim($lineString);//fall through |
|
| 149 | + $n->value = trim($lineString); //fall through |
|
| 150 | 150 | case T::REF_DEF://fall through |
| 151 | 151 | case T::SET_VALUE://fall through |
| 152 | 152 | case T::TAG: |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | case T::EMPTY: |
| 156 | 156 | case T::SCALAR: |
| 157 | 157 | if ($n->type === T::SCALAR && |
| 158 | - !in_array($deepest->getParent()->type, T::$LITTERALS) ) { |
|
| 158 | + !in_array($deepest->getParent()->type, T::$LITTERALS)) { |
|
| 159 | 159 | $deepest->type = T::SCALAR; |
| 160 | 160 | $deepest->value .= "\n".$n->value; |
| 161 | 161 | return true; |
@@ -66,9 +66,13 @@ discard block |
||
| 66 | 66 | public function parse($strContent = null) |
| 67 | 67 | { |
| 68 | 68 | $source = $this->content; |
| 69 | - if (is_null($source)) $source = preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
|
| 69 | + if (is_null($source)) { |
|
| 70 | + $source = preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
|
| 71 | + } |
|
| 70 | 72 | //TODO : be more permissive on $strContent values |
| 71 | - if (!is_array($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 73 | + if (!is_array($source)) { |
|
| 74 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 75 | + } |
|
| 72 | 76 | $previous = $root = new Node(); |
| 73 | 77 | $emptyLines = []; |
| 74 | 78 | $specialTypes = [T::LITTERAL, T::LITTERAL_FOLDED, T::EMPTY]; |
@@ -85,7 +89,9 @@ discard block |
||
| 85 | 89 | $deepest->parse($deepest->value.' '.ltrim($lineString)); |
| 86 | 90 | } else { |
| 87 | 91 | if (in_array($n->type, $specialTypes)) { |
| 88 | - if ($this->onSpecialType($n, $parent, $previous, $emptyLines)) continue; |
|
| 92 | + if ($this->onSpecialType($n, $parent, $previous, $emptyLines)) { |
|
| 93 | + continue; |
|
| 94 | + } |
|
| 89 | 95 | } |
| 90 | 96 | foreach ($emptyLines as $key => $node) { |
| 91 | 97 | $node->getParent()->add($node); |
@@ -96,7 +102,9 @@ discard block |
||
| 96 | 102 | } elseif ($n->indent === $previous->indent) { |
| 97 | 103 | $parent = $previous->getParent(); |
| 98 | 104 | } elseif ($n->indent > $previous->indent) { |
| 99 | - if ($this->onDeepestType($n, $parent, $previous, $lineString)) continue; |
|
| 105 | + if ($this->onDeepestType($n, $parent, $previous, $lineString)) { |
|
| 106 | + continue; |
|
| 107 | + } |
|
| 100 | 108 | } |
| 101 | 109 | $parent->add($n); |
| 102 | 110 | $previous = $n; |
@@ -125,8 +133,12 @@ discard block |
||
| 125 | 133 | $deepest = $previous->getDeepestNode(); |
| 126 | 134 | switch ($n->type) { |
| 127 | 135 | case T::EMPTY: |
| 128 | - if ($previous->type === T::SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 129 | - if (in_array($deepest->type, T::$LITTERALS)) $emptyLines[] = $n->setParent($deepest); |
|
| 136 | + if ($previous->type === T::SCALAR) { |
|
| 137 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 138 | + } |
|
| 139 | + if (in_array($deepest->type, T::$LITTERALS)) { |
|
| 140 | + $emptyLines[] = $n->setParent($deepest); |
|
| 141 | + } |
|
| 130 | 142 | return true; |
| 131 | 143 | case T::LITTERAL://fall through |
| 132 | 144 | case T::LITTERAL_FOLDED://var_dump($deepest);exit(); |
@@ -32,8 +32,8 @@ discard block |
||
| 32 | 32 | $p = $parent; |
| 33 | 33 | switch ($type) { |
| 34 | 34 | case T::MAPPING: //fall through |
| 35 | - case T::SET: $p = new \StdClass;break; |
|
| 36 | - case T::SEQUENCE: $p = [];break; |
|
| 35 | + case T::SET: $p = new \StdClass; break; |
|
| 36 | + case T::SEQUENCE: $p = []; break; |
|
| 37 | 37 | // case T::KEY: $p = $parent;break; |
| 38 | 38 | } |
| 39 | 39 | $out = null; |
@@ -54,10 +54,10 @@ discard block |
||
| 54 | 54 | { |
| 55 | 55 | list($line, $type, $value, $identifier) = [$node->line, $node->type, $node->value, $node->identifier]; |
| 56 | 56 | switch ($type) { |
| 57 | - case T::COMMENT: self::$root->addComment($line, $value);return; |
|
| 58 | - case T::DIRECTIVE: return;//TODO |
|
| 59 | - case T::ITEM: self::buildItem($value, $parent);return; |
|
| 60 | - case T::KEY: self::buildKey($node, $parent);return; |
|
| 57 | + case T::COMMENT: self::$root->addComment($line, $value); return; |
|
| 58 | + case T::DIRECTIVE: return; //TODO |
|
| 59 | + case T::ITEM: self::buildItem($value, $parent); return; |
|
| 60 | + case T::KEY: self::buildKey($node, $parent); return; |
|
| 61 | 61 | case T::REF_DEF: //fall through |
| 62 | 62 | case T::REF_CALL://TODO: self::build returns what ? |
| 63 | 63 | $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | $prop = array_keys(get_object_vars($parent)); |
| 74 | 74 | $key = end($prop); |
| 75 | 75 | if (property_exists($value, "type") && in_array($value->type, [T::ITEM, T::MAPPING])) { |
| 76 | - $p = $value->type === T::ITEM ? [] : new \StdClass; |
|
| 76 | + $p = $value->type === T::ITEM ? [] : new \StdClass; |
|
| 77 | 77 | self::build($value, $p); |
| 78 | 78 | } else { |
| 79 | 79 | $p = self::build($value, $parent->{$key}); |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | return; |
| 83 | 83 | case T::TAG: |
| 84 | 84 | if ($parent === self::$root) { |
| 85 | - $parent->addTag($identifier);return; |
|
| 85 | + $parent->addTag($identifier); return; |
|
| 86 | 86 | } else { |
| 87 | 87 | if (in_array($identifier, ['!binary', '!str'])) { |
| 88 | 88 | if ($value->value instanceof DLL) $value->value->type = T::RAW; |
@@ -170,8 +170,8 @@ discard block |
||
| 170 | 170 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
| 171 | 171 | } else { |
| 172 | 172 | switch (true) { |
| 173 | - case $isSequence: $list->type = T::SEQUENCE;break; |
|
| 174 | - case $isSet: $list->type = T::SET;break; |
|
| 173 | + case $isSequence: $list->type = T::SEQUENCE; break; |
|
| 174 | + case $isSet: $list->type = T::SET; break; |
|
| 175 | 175 | case $isMapping://fall through |
| 176 | 176 | default:$list->type = T::MAPPING; |
| 177 | 177 | } |
@@ -195,10 +195,10 @@ discard block |
||
| 195 | 195 | $children->rewind(); |
| 196 | 196 | $refIndent = $children->current()->indent; |
| 197 | 197 | $separator = $type === T::RAW ? '' : "\n"; |
| 198 | - $action = function ($c) { return $c->value; }; |
|
| 198 | + $action = function($c) { return $c->value; }; |
|
| 199 | 199 | if ($type === T::LITTERAL_FOLDED) { |
| 200 | 200 | $separator = ' '; |
| 201 | - $action = function ($c) use ($refIndent) { |
|
| 201 | + $action = function($c) use ($refIndent) { |
|
| 202 | 202 | return $c->indent > $refIndent || $c->type === T::EMPTY ? "\n".$c->value : $c->value; |
| 203 | 203 | }; |
| 204 | 204 | } |
@@ -19,7 +19,9 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | private static function build(object $node, &$parent = null) |
| 21 | 21 | { |
| 22 | - if ($node instanceof DLL) return self::buildDLL($node, $parent); |
|
| 22 | + if ($node instanceof DLL) { |
|
| 23 | + return self::buildDLL($node, $parent); |
|
| 24 | + } |
|
| 23 | 25 | return self::buildNode($node, $parent); |
| 24 | 26 | } |
| 25 | 27 | |
@@ -61,12 +63,15 @@ discard block |
||
| 61 | 63 | case T::REF_DEF: //fall through |
| 62 | 64 | case T::REF_CALL://TODO: self::build returns what ? |
| 63 | 65 | $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
| 64 | - if ($type === T::REF_DEF) self::$root->addReference($identifier, $tmp); |
|
| 66 | + if ($type === T::REF_DEF) { |
|
| 67 | + self::$root->addReference($identifier, $tmp); |
|
| 68 | + } |
|
| 65 | 69 | return self::$root->getReference($identifier); |
| 66 | 70 | case T::SET_KEY: |
| 67 | 71 | $key = json_encode(self::build($value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 68 | - if (empty($key)) |
|
| 69 | - throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
|
| 72 | + if (empty($key)) { |
|
| 73 | + throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
|
| 74 | + } |
|
| 70 | 75 | $parent->{$key} = null; |
| 71 | 76 | return; |
| 72 | 77 | case T::SET_VALUE: |
@@ -85,8 +90,11 @@ discard block |
||
| 85 | 90 | $parent->addTag($identifier);return; |
| 86 | 91 | } else { |
| 87 | 92 | if (in_array($identifier, ['!binary', '!str'])) { |
| 88 | - if ($value->value instanceof DLL) $value->value->type = T::RAW; |
|
| 89 | - else $value->type = T::RAW; |
|
| 93 | + if ($value->value instanceof DLL) { |
|
| 94 | + $value->value->type = T::RAW; |
|
| 95 | + } else { |
|
| 96 | + $value->type = T::RAW; |
|
| 97 | + } |
|
| 90 | 98 | } |
| 91 | 99 | $val = is_null($value) ? null : self::build($value, $node); |
| 92 | 100 | return new Tag($identifier, $val); |
@@ -148,10 +156,14 @@ discard block |
||
| 148 | 156 | } |
| 149 | 157 | $root->value->setIteratorMode(DLL::IT_MODE_DELETE); |
| 150 | 158 | foreach ($root->value as $child) { |
| 151 | - if ($child->type === T::DOC_START) $totalDocStart++; |
|
| 159 | + if ($child->type === T::DOC_START) { |
|
| 160 | + $totalDocStart++; |
|
| 161 | + } |
|
| 152 | 162 | //if 0 or 1 DOC_START = we are still in first document |
| 153 | 163 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 154 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new DLL(); |
|
| 164 | + if (!isset($documents[$currentDoc])) { |
|
| 165 | + $documents[$currentDoc] = new DLL(); |
|
| 166 | + } |
|
| 155 | 167 | $documents[$currentDoc]->push($child); |
| 156 | 168 | } |
| 157 | 169 | $debug >= 2 && var_dump($documents); |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
| 30 | 30 | self::$options = is_int($options) ? $options : self::$options; |
| 31 | 31 | self::$result = new DLL; |
| 32 | - self::$result->setIteratorMode(DLL::IT_MODE_FIFO | DLL::IT_MODE_DELETE); |
|
| 32 | + self::$result->setIteratorMode(DLL::IT_MODE_FIFO|DLL::IT_MODE_DELETE); |
|
| 33 | 33 | if ($dataType instanceof YamlObject) { |
| 34 | 34 | self::dumpYamlObject($dataType); |
| 35 | 35 | } elseif (is_array($dataType) && $dataType[0] instanceof YamlObject) { |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | { |
| 55 | 55 | if (is_scalar($dataType)) { |
| 56 | 56 | switch (gettype($dataType)) { |
| 57 | - case 'boolean': return $dataType ? 'true' : 'false';break; |
|
| 57 | + case 'boolean': return $dataType ? 'true' : 'false'; break; |
|
| 58 | 58 | case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
| 59 | 59 | case 'double': if (is_nan($dataType)) return '.nan'; |
| 60 | 60 | default: |
@@ -89,9 +89,9 @@ discard block |
||
| 89 | 89 | foreach ($array as $key => $item) { |
| 90 | 90 | $lineStart = current($refKeys) === $key ? "- " : "- $key: "; |
| 91 | 91 | if (is_scalar($item)) { |
| 92 | - self::add($lineStart.$item, $indent ); |
|
| 92 | + self::add($lineStart.$item, $indent); |
|
| 93 | 93 | } else { |
| 94 | - self::add($lineStart, $indent ); |
|
| 94 | + self::add($lineStart, $indent); |
|
| 95 | 95 | self::dump($item, $indent + self::indent); |
| 96 | 96 | } |
| 97 | 97 | next($refKeys); |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | if ($dataType instanceof Tag) { |
| 111 | 111 | if (is_scalar($dataType->value)) { |
| 112 | 112 | return "!".$dataType->tagName.' '.$dataType->value; |
| 113 | - } else{ |
|
| 113 | + } else { |
|
| 114 | 114 | yield "!".$dataType->tagName; |
| 115 | 115 | self::dump($dataType->value, $indent + self::indent); |
| 116 | 116 | } |
@@ -21,12 +21,16 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | public function __construct(int $options = null) |
| 23 | 23 | { |
| 24 | - if (is_int($options)) self::$options = $options; |
|
| 24 | + if (is_int($options)) { |
|
| 25 | + self::$options = $options; |
|
| 26 | + } |
|
| 25 | 27 | } |
| 26 | 28 | |
| 27 | 29 | public static function toString($dataType, int $options):string |
| 28 | 30 | { |
| 29 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 31 | + if (is_null($dataType)) { |
|
| 32 | + throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 33 | + } |
|
| 30 | 34 | self::$options = is_int($options) ? $options : self::$options; |
| 31 | 35 | self::$result = new DLL; |
| 32 | 36 | self::$result->setIteratorMode(DLL::IT_MODE_FIFO | DLL::IT_MODE_DELETE); |
@@ -55,8 +59,12 @@ discard block |
||
| 55 | 59 | if (is_scalar($dataType)) { |
| 56 | 60 | switch (gettype($dataType)) { |
| 57 | 61 | case 'boolean': return $dataType ? 'true' : 'false';break; |
| 58 | - case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 59 | - case 'double': if (is_nan($dataType)) return '.nan'; |
|
| 62 | + case 'float': if (is_infinite($dataType)) { |
|
| 63 | + return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 64 | + } |
|
| 65 | + case 'double': if (is_nan($dataType)) { |
|
| 66 | + return '.nan'; |
|
| 67 | + } |
|
| 60 | 68 | default: |
| 61 | 69 | return $dataType; |
| 62 | 70 | } |