@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -require_once __DIR__ . '/vendor/autoload.php'; |
|
| 2 | +require_once __DIR__.'/vendor/autoload.php'; |
|
| 3 | 3 | |
| 4 | 4 | use \Dallgoot\Yaml as Y; |
| 5 | 5 | |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | /* USE CASE 1 |
| 11 | 11 | * load and parse if file exists |
| 12 | 12 | */ |
| 13 | -$yaml = Y::parseFile('./tests/cases/examples/Example_2_18.yml', null, 2);//->parse(); |
|
| 13 | +$yaml = Y::parseFile('./tests/cases/examples/Example_2_18.yml', null, 2); //->parse(); |
|
| 14 | 14 | // $yaml = Y::parseFile('./references/Example 2.27.yml', null, 1); |
| 15 | 15 | var_dump($yaml); |
| 16 | 16 | exit(0); |
@@ -13,7 +13,7 @@ |
||
| 13 | 13 | class NodeList extends \SplDoublyLinkedList |
| 14 | 14 | { |
| 15 | 15 | /* @var null|int */ |
| 16 | - public $type = null;//Y::LITT_FOLDED; |
|
| 16 | + public $type = null; //Y::LITT_FOLDED; |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * NodeList constructor |
@@ -21,14 +21,14 @@ |
||
| 21 | 21 | const ITEM = 128; |
| 22 | 22 | const JSON = 256; |
| 23 | 23 | const KEY = 512; |
| 24 | - const LITT = 1024;//litteral |
|
| 25 | - const LITT_FOLDED = 2048;//litteral |
|
| 24 | + const LITT = 1024; //litteral |
|
| 25 | + const LITT_FOLDED = 2048; //litteral |
|
| 26 | 26 | const MAPPING = 4096; |
| 27 | 27 | const PARTIAL = 8192; |
| 28 | 28 | const QUOTED = 16384; |
| 29 | 29 | const RAW = 32768; |
| 30 | - const REF_CALL = 65536;//reference |
|
| 31 | - const REF_DEF = 131072;//reference |
|
| 30 | + const REF_CALL = 65536; //reference |
|
| 31 | + const REF_DEF = 131072; //reference |
|
| 32 | 32 | const ROOT = 262144; |
| 33 | 33 | const SCALAR = 524288; |
| 34 | 34 | const SEQUENCE = 1048576; |
@@ -37,7 +37,7 @@ |
||
| 37 | 37 | $matchSpaced = preg_match($spaced, $v); |
| 38 | 38 | $matchIso = preg_match($iso8601, $v); |
| 39 | 39 | if (is_bool($matchDate) || is_bool($matchCanonical) || is_bool($matchSpaced) || is_bool($matchIso)) |
| 40 | - throw new \Exception("Regex date error"); |
|
| 40 | + throw new \Exception("Regex date error"); |
|
| 41 | 41 | |
| 42 | 42 | return $matchDate || $matchCanonical || $matchSpaced || $matchIso; |
| 43 | 43 | } |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | private static function buildNodeList(NodeList $node, &$parent) |
| 30 | 30 | { |
| 31 | 31 | $type = &$node->type; |
| 32 | - if ($type & (Y::RAW | Y::LITTERALS)) { |
|
| 32 | + if ($type&(Y::RAW|Y::LITTERALS)) { |
|
| 33 | 33 | return self::litteral($node, $type); |
| 34 | 34 | } |
| 35 | 35 | $p = $parent; |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | private static function buildNode(Node $node, &$parent) |
| 57 | 57 | { |
| 58 | 58 | extract((array) $node, EXTR_REFS); |
| 59 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
| 59 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
| 60 | 60 | if (is_object($value)) { |
| 61 | 61 | $tmp = self::build($value, $parent) ?? $parent; |
| 62 | 62 | } else { |
@@ -176,15 +176,15 @@ discard block |
||
| 176 | 176 | { |
| 177 | 177 | self::$_root = new YamlObject(); |
| 178 | 178 | $childTypes = $list->getTypes(); |
| 179 | - $isMapping = (bool) (Y::KEY | Y::MAPPING) & $childTypes; |
|
| 179 | + $isMapping = (bool) (Y::KEY|Y::MAPPING)&$childTypes; |
|
| 180 | 180 | $isSequence = (bool) Y::ITEM & $childTypes; |
| 181 | 181 | $isSet = (bool) Y::SET_VALUE & $childTypes; |
| 182 | 182 | if ($isMapping && $isSequence) { |
| 183 | 183 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
| 184 | 184 | } else { |
| 185 | 185 | switch (true) { |
| 186 | - case $isSequence: $list->type = Y::SEQUENCE;break; |
|
| 187 | - case $isSet: $list->type = Y::SET;break; |
|
| 186 | + case $isSequence: $list->type = Y::SEQUENCE; break; |
|
| 187 | + case $isSet: $list->type = Y::SET; break; |
|
| 188 | 188 | default: $list->type = Y::MAPPING; |
| 189 | 189 | } |
| 190 | 190 | } |
@@ -206,11 +206,11 @@ discard block |
||
| 206 | 206 | $children->rewind(); |
| 207 | 207 | $refIndent = $children->current()->indent; |
| 208 | 208 | $separator = $type === Y::RAW ? '' : "\n"; |
| 209 | - $action = function ($c) { return (string) $c->value; }; |
|
| 209 | + $action = function($c) { return (string) $c->value; }; |
|
| 210 | 210 | //TODO: use iterator_to_array for LITT & RAW |
| 211 | - if ($type & Y::LITT_FOLDED) { |
|
| 211 | + if ($type&Y::LITT_FOLDED) { |
|
| 212 | 212 | $separator = ' '; |
| 213 | - $action = function ($c) use ($refIndent) { |
|
| 213 | + $action = function($c) use ($refIndent) { |
|
| 214 | 214 | return $c->indent > $refIndent || ($c->type & Y::BLANK) ? "\n".$c->value : $c->value; |
| 215 | 215 | }; |
| 216 | 216 | } |
@@ -22,7 +22,9 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | private static function build(object $node, &$parent = null) |
| 24 | 24 | { |
| 25 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
| 25 | + if ($node instanceof NodeList) { |
|
| 26 | + return self::buildNodeList($node, $parent); |
|
| 27 | + } |
|
| 26 | 28 | return self::buildNode($node, $parent); |
| 27 | 29 | } |
| 28 | 30 | |
@@ -62,7 +64,9 @@ discard block |
||
| 62 | 64 | } else { |
| 63 | 65 | $tmp = $node->getPhpValue(); |
| 64 | 66 | } |
| 65 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 67 | + if ($type === Y::REF_DEF) { |
|
| 68 | + self::$_root->addReference($identifier, $tmp); |
|
| 69 | + } |
|
| 66 | 70 | return self::$_root->getReference($identifier); |
| 67 | 71 | } |
| 68 | 72 | switch ($type) { |
@@ -72,8 +76,9 @@ discard block |
||
| 72 | 76 | case Y::KEY: self::buildKey($node, $parent); return; |
| 73 | 77 | case Y::SET_KEY: |
| 74 | 78 | $key = json_encode(self::build($value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 75 | - if (empty($key)) |
|
| 76 | - throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
|
| 79 | + if (empty($key)) { |
|
| 80 | + throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
|
| 81 | + } |
|
| 77 | 82 | $parent->{$key} = null; |
| 78 | 83 | return; |
| 79 | 84 | case Y::SET_VALUE: |
@@ -92,8 +97,11 @@ discard block |
||
| 92 | 97 | $parent->addTag($identifier); return; |
| 93 | 98 | } else {//TODO: have somewhere a list of common tags and their treatment |
| 94 | 99 | if (in_array($identifier, ['!binary', '!str'])) { |
| 95 | - if ($value->value instanceof NodeList) $value->value->type = Y::RAW; |
|
| 96 | - else $value->type = Y::RAW; |
|
| 100 | + if ($value->value instanceof NodeList) { |
|
| 101 | + $value->value->type = Y::RAW; |
|
| 102 | + } else { |
|
| 103 | + $value->type = Y::RAW; |
|
| 104 | + } |
|
| 97 | 105 | } |
| 98 | 106 | $val = is_null($value) ? null : self::build(/** @scrutinizer ignore-type */ $value, $node); |
| 99 | 107 | return new Tag($identifier, $val); |
@@ -162,10 +170,14 @@ discard block |
||
| 162 | 170 | } |
| 163 | 171 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 164 | 172 | foreach ($_root->value as $child) { |
| 165 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 173 | + if ($child->type & Y::DOC_START) { |
|
| 174 | + $totalDocStart++; |
|
| 175 | + } |
|
| 166 | 176 | //if 0 or 1 DOC_START = we are still in first document |
| 167 | 177 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 168 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 178 | + if (!isset($documents[$currentDoc])) { |
|
| 179 | + $documents[$currentDoc] = new NodeList(); |
|
| 180 | + } |
|
| 169 | 181 | $documents[$currentDoc]->push($child); |
| 170 | 182 | } |
| 171 | 183 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
@@ -94,8 +94,8 @@ discard block |
||
| 94 | 94 | //modify type according to child |
| 95 | 95 | switch ($child->type) { |
| 96 | 96 | case Y::COMMENT: //fall through |
| 97 | - case Y::KEY: $this->value->type = Y::MAPPING;break; |
|
| 98 | - case Y::ITEM: $this->value->type = Y::SEQUENCE;break; |
|
| 97 | + case Y::KEY: $this->value->type = Y::MAPPING; break; |
|
| 98 | + case Y::ITEM: $this->value->type = Y::SEQUENCE; break; |
|
| 99 | 99 | } |
| 100 | 100 | $this->type & Y::LITTERALS && $this->value->type = $this->type; |
| 101 | 101 | } |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | */ |
| 120 | 120 | public function parse(string $nodeString):Node |
| 121 | 121 | { |
| 122 | - $nodeValue = preg_replace("/^\t+/m", " ", $nodeString);//permissive to tabs but replacement |
|
| 122 | + $nodeValue = preg_replace("/^\t+/m", " ", $nodeString); //permissive to tabs but replacement |
|
| 123 | 123 | $this->indent = strspn($nodeValue, ' '); |
| 124 | 124 | $nodeValue = ltrim($nodeValue); |
| 125 | 125 | if ($nodeValue === '') { |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | case '#': return [Y::COMMENT, ltrim($v)]; |
| 157 | 157 | case "-": return $this->onHyphen($nodeValue); |
| 158 | 158 | case '%': return [Y::DIRECTIVE, ltrim($v)]; |
| 159 | - case '?': return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 159 | + case '?': return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 160 | 160 | case ':': return [Y::SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
| 161 | 161 | case '>': return [Y::LITT_FOLDED, null]; |
| 162 | 162 | case '|': return [Y::LITT, null]; |
@@ -181,8 +181,8 @@ discard block |
||
| 181 | 181 | if (!is_bool($hasComment)) { |
| 182 | 182 | $tmpNode = new Node(trim(substr($value, 0, $hasComment)), $this->line); |
| 183 | 183 | if ($tmpNode->type !== Y::PARTIAL) { |
| 184 | - $comment = new Node(trim(substr($value, $hasComment+1)), $this->line); |
|
| 185 | - $comment->identifier = true;//to specify it is NOT a fullline comment |
|
| 184 | + $comment = new Node(trim(substr($value, $hasComment + 1)), $this->line); |
|
| 185 | + $comment->identifier = true; //to specify it is NOT a fullline comment |
|
| 186 | 186 | $this->add($comment); |
| 187 | 187 | $n = $tmpNode; |
| 188 | 188 | } |
@@ -250,7 +250,7 @@ discard block |
||
| 250 | 250 | $type = ['!' => Y::TAG, '&' => Y::REF_DEF, '*' => Y::REF_CALL][$nodeValue[0]]; |
| 251 | 251 | $pos = strpos($v, ' '); |
| 252 | 252 | $this->identifier = is_bool($pos) ? $v : strstr($v, ' ', true); |
| 253 | - $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos+1)), $this->line))->setParent($this); |
|
| 253 | + $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos + 1)), $this->line))->setParent($this); |
|
| 254 | 254 | return [$type, $n]; |
| 255 | 255 | } |
| 256 | 256 | |
@@ -263,8 +263,8 @@ discard block |
||
| 263 | 263 | { |
| 264 | 264 | $v = &$this->value; |
| 265 | 265 | if (is_null($v)) return null; |
| 266 | - if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
|
| 267 | - if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 266 | + if ($this->type & (Y::REF_CALL|Y::SCALAR)) return self::getScalar($v); |
|
| 267 | + if ($this->type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 268 | 268 | switch ($this->type) { |
| 269 | 269 | case Y::JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
| 270 | 270 | case Y::QUOTED: return substr($v, 1, -1); |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | { |
| 321 | 321 | $out = new Compact(); |
| 322 | 322 | if ($type === Y::COMPACT_SEQUENCE) { |
| 323 | - $f = function ($e) { return self::getScalar(trim($e));}; |
|
| 323 | + $f = function($e) { return self::getScalar(trim($e)); }; |
|
| 324 | 324 | //TODO : that's not robust enough, improve it |
| 325 | 325 | foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) { |
| 326 | 326 | $out[$key] = $value; |
@@ -59,7 +59,9 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function getParent(int $indent = null):Node |
| 61 | 61 | { |
| 62 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
| 62 | + if (!is_int($indent)) { |
|
| 63 | + return $this->parent ?? $this; |
|
| 64 | + } |
|
| 63 | 65 | $cursor = $this; |
| 64 | 66 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
| 65 | 67 | $cursor = $cursor->parent; |
@@ -150,8 +152,12 @@ discard block |
||
| 150 | 152 | $type = R::isProperlyQuoted($nodeValue) ? Y::QUOTED : Y::PARTIAL; |
| 151 | 153 | return [$type, $nodeValue]; |
| 152 | 154 | } |
| 153 | - if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
|
| 154 | - if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
|
| 155 | + if (in_array($first, ['{', '['])) { |
|
| 156 | + return $this->onObject($nodeValue); |
|
| 157 | + } |
|
| 158 | + if (in_array($first, ['!', '&', '*'])) { |
|
| 159 | + return $this->onNodeAction($nodeValue); |
|
| 160 | + } |
|
| 155 | 161 | switch ($first) { |
| 156 | 162 | case '#': return [Y::COMMENT, ltrim($v)]; |
| 157 | 163 | case "-": return $this->onHyphen($nodeValue); |
@@ -203,9 +209,15 @@ discard block |
||
| 203 | 209 | private function onObject($value):array |
| 204 | 210 | { |
| 205 | 211 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 206 | - if (json_last_error() === JSON_ERROR_NONE) return [Y::JSON, $value]; |
|
| 207 | - if (preg_match(R::MAPPING, $value)) return [Y::COMPACT_MAPPING, $value]; |
|
| 208 | - if (preg_match(R::SEQUENCE, $value)) return [Y::COMPACT_SEQUENCE, $value]; |
|
| 212 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
| 213 | + return [Y::JSON, $value]; |
|
| 214 | + } |
|
| 215 | + if (preg_match(R::MAPPING, $value)) { |
|
| 216 | + return [Y::COMPACT_MAPPING, $value]; |
|
| 217 | + } |
|
| 218 | + if (preg_match(R::SEQUENCE, $value)) { |
|
| 219 | + return [Y::COMPACT_SEQUENCE, $value]; |
|
| 220 | + } |
|
| 209 | 221 | return [Y::PARTIAL, $value]; |
| 210 | 222 | } |
| 211 | 223 | |
@@ -220,7 +232,9 @@ discard block |
||
| 220 | 232 | { |
| 221 | 233 | if (substr($nodeValue, 0, 3) === '---') { |
| 222 | 234 | $rest = trim(substr($nodeValue, 3)); |
| 223 | - if (empty($rest)) return [Y::DOC_START, null]; |
|
| 235 | + if (empty($rest)) { |
|
| 236 | + return [Y::DOC_START, null]; |
|
| 237 | + } |
|
| 224 | 238 | $n = new Node($rest, $this->line); |
| 225 | 239 | $n->indent = $this->indent + 4; |
| 226 | 240 | return [Y::DOC_START, $n->setParent($this)]; |
@@ -262,9 +276,15 @@ discard block |
||
| 262 | 276 | public function getPhpValue() |
| 263 | 277 | { |
| 264 | 278 | $v = &$this->value; |
| 265 | - if (is_null($v)) return null; |
|
| 266 | - if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
|
| 267 | - if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 279 | + if (is_null($v)) { |
|
| 280 | + return null; |
|
| 281 | + } |
|
| 282 | + if ($this->type & (Y::REF_CALL | Y::SCALAR)) { |
|
| 283 | + return self::getScalar($v); |
|
| 284 | + } |
|
| 285 | + if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) { |
|
| 286 | + return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 287 | + } |
|
| 268 | 288 | switch ($this->type) { |
| 269 | 289 | case Y::JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
| 270 | 290 | case Y::QUOTED: return substr($v, 1, -1); |
@@ -293,9 +313,15 @@ discard block |
||
| 293 | 313 | '-.inf' => -INF, |
| 294 | 314 | '.nan' => NAN |
| 295 | 315 | ]; |
| 296 | - if (isset($types[strtolower($v)])) return $types[strtolower($v)]; |
|
| 297 | - if (R::isDate($v)) return date_create($v); |
|
| 298 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 316 | + if (isset($types[strtolower($v)])) { |
|
| 317 | + return $types[strtolower($v)]; |
|
| 318 | + } |
|
| 319 | + if (R::isDate($v)) { |
|
| 320 | + return date_create($v); |
|
| 321 | + } |
|
| 322 | + if (R::isNumber($v)) { |
|
| 323 | + return self::getNumber($v); |
|
| 324 | + } |
|
| 299 | 325 | return strval($v); |
| 300 | 326 | } |
| 301 | 327 | |
@@ -308,8 +334,12 @@ discard block |
||
| 308 | 334 | */ |
| 309 | 335 | private static function getNumber(string $v) |
| 310 | 336 | { |
| 311 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
| 312 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
| 337 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
| 338 | + return intval(base_convert($v, 8, 10)); |
|
| 339 | + } |
|
| 340 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
| 341 | + return intval(base_convert($v, 16, 10)); |
|
| 342 | + } |
|
| 313 | 343 | // TODO: remove these if not needed |
| 314 | 344 | // if preg_match("/^([\d.]+e[-+]\d{1,2})$/", $v)://fall through |
| 315 | 345 | // if preg_match("/^([-+]?(?:\d+|\d*.\d+))$/", $v): |
@@ -19,17 +19,17 @@ discard block |
||
| 19 | 19 | /* @var null|string */ |
| 20 | 20 | public $error; |
| 21 | 21 | |
| 22 | - public const EXCLUDE_DIRECTIVES = 1;//DONT include_directive |
|
| 23 | - public const IGNORE_COMMENTS = 2;//DONT include_comments |
|
| 24 | - public const NO_PARSING_EXCEPTIONS = 4;//THROW Exception on parsing Errors |
|
| 25 | - public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
|
| 22 | + public const EXCLUDE_DIRECTIVES = 1; //DONT include_directive |
|
| 23 | + public const IGNORE_COMMENTS = 2; //DONT include_comments |
|
| 24 | + public const NO_PARSING_EXCEPTIONS = 4; //THROW Exception on parsing Errors |
|
| 25 | + public const NO_OBJECT_FOR_DATE = 8; //DONT import date strings as dateTime Object |
|
| 26 | 26 | //privates |
| 27 | 27 | /* @var null|string */ |
| 28 | 28 | private $content; |
| 29 | 29 | /* @var null|string */ |
| 30 | 30 | private $filePath; |
| 31 | 31 | /* @var integer */ |
| 32 | - private $debug = 0;///TODO: determine levels |
|
| 32 | + private $debug = 0; ///TODO: determine levels |
|
| 33 | 33 | /* @var integer */ |
| 34 | 34 | private $options = 0; |
| 35 | 35 | //Exceptions messages |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | $target->add($n); |
| 118 | 118 | $previous = $n; |
| 119 | 119 | } |
| 120 | - if ($this->debug === 2) echo "\033[33mParsed Structure\033[0m\n",var_export($root, true); |
|
| 120 | + if ($this->debug === 2) echo "\033[33mParsed Structure\033[0m\n", var_export($root, true); |
|
| 121 | 121 | $out = Builder::buildContent($root, $this->debug); |
| 122 | 122 | return $out; |
| 123 | 123 | } catch (\Error|\Exception|\ParseError $e) { |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | $deepest->parse($deepest->value.' '.ltrim($lineString)); |
| 156 | 156 | return true; |
| 157 | 157 | } |
| 158 | - if(($previous->type & Y::LITTERALS) || (($deepest->type & Y::LITTERALS) && is_null($deepest->value))) { |
|
| 158 | + if (($previous->type & Y::LITTERALS) || (($deepest->type & Y::LITTERALS) && is_null($deepest->value))) { |
|
| 159 | 159 | $n->type = Y::SCALAR; |
| 160 | 160 | $n->identifier = null; |
| 161 | 161 | $n->value = trim($lineString); |
@@ -87,7 +87,9 @@ discard block |
||
| 87 | 87 | { |
| 88 | 88 | $source = $this->content ?? preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
| 89 | 89 | //TODO : be more permissive on $strContent values |
| 90 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 90 | + if (!is_array($source) || !count($source)) { |
|
| 91 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 92 | + } |
|
| 91 | 93 | $previous = $root = new Node(); |
| 92 | 94 | $emptyLines = []; |
| 93 | 95 | try { |
@@ -99,7 +101,9 @@ discard block |
||
| 99 | 101 | foreach ($gen() as $lineNb => $lineString) { |
| 100 | 102 | $n = new Node($lineString, $lineNb); |
| 101 | 103 | if ($n->type & (Y::LITTERALS|Y::BLANK)) { |
| 102 | - if ($this->onSpecialType($n, $previous, $emptyLines)) continue; |
|
| 104 | + if ($this->onSpecialType($n, $previous, $emptyLines)) { |
|
| 105 | + continue; |
|
| 106 | + } |
|
| 103 | 107 | } else { |
| 104 | 108 | foreach ($emptyLines as $blankNode) { |
| 105 | 109 | $blankNode->getParent()->add($blankNode); |
@@ -113,11 +117,15 @@ discard block |
||
| 113 | 117 | break; |
| 114 | 118 | default: $target = $previous; |
| 115 | 119 | } |
| 116 | - if ($this->onContextType($n, $target, $lineString)) continue; |
|
| 120 | + if ($this->onContextType($n, $target, $lineString)) { |
|
| 121 | + continue; |
|
| 122 | + } |
|
| 117 | 123 | $target->add($n); |
| 118 | 124 | $previous = $n; |
| 119 | 125 | } |
| 120 | - if ($this->debug === 2) echo "\033[33mParsed Structure\033[0m\n",var_export($root, true); |
|
| 126 | + if ($this->debug === 2) { |
|
| 127 | + echo "\033[33mParsed Structure\033[0m\n",var_export($root, true); |
|
| 128 | + } |
|
| 121 | 129 | $out = Builder::buildContent($root, $this->debug); |
| 122 | 130 | return $out; |
| 123 | 131 | } catch (\Error|\Exception|\ParseError $e) { |
@@ -137,8 +145,12 @@ discard block |
||
| 137 | 145 | { |
| 138 | 146 | $deepest = $previous->getDeepestNode(); |
| 139 | 147 | if ($n->type & Y::BLANK) { |
| 140 | - if ($previous->type & Y::SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 141 | - if ($deepest->type & Y::LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
| 148 | + if ($previous->type & Y::SCALAR) { |
|
| 149 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 150 | + } |
|
| 151 | + if ($deepest->type & Y::LITTERALS) { |
|
| 152 | + $emptyLines[] = $n->setParent($deepest); |
|
| 153 | + } |
|
| 142 | 154 | return true; |
| 143 | 155 | } |
| 144 | 156 | return false; |
@@ -54,7 +54,7 @@ |
||
| 54 | 54 | // foreach (self::$result as $value) { |
| 55 | 55 | // $out .= $value."\n"; |
| 56 | 56 | // } |
| 57 | - $out = implode("\n", iterator_to_array(self::$result));//TODO: replace by iterator_to_array and implode ??? |
|
| 57 | + $out = implode("\n", iterator_to_array(self::$result)); //TODO: replace by iterator_to_array and implode ??? |
|
| 58 | 58 | self::$result = null; |
| 59 | 59 | return $out; |
| 60 | 60 | } |