@@ -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 | |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | * load and parse if file exists |
| 8 | 8 | */ |
| 9 | 9 | // $yaml = Y::loadFile('./dummy.yml');//->parse(); |
| 10 | -$yaml = Y::parseFile('./references/Example 2.18.yml');//->parse(); |
|
| 10 | +$yaml = Y::parseFile('./references/Example 2.18.yml'); //->parse(); |
|
| 11 | 11 | var_dump($yaml); |
| 12 | 12 | exit(0); |
| 13 | 13 | // USE CASE 2 |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | 'SEQUENCE_SHORT', |
| 18 | 18 | 'PARTIAL', |
| 19 | 19 | 'LITT', //litteral |
| 20 | - 'LITT_FOLDED',//litteral |
|
| 20 | + 'LITT_FOLDED', //litteral |
|
| 21 | 21 | 'SCALAR', |
| 22 | 22 | 'TAG', |
| 23 | 23 | 'JSON', |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | define(__NAMESPACE__."\\$name", 2**$power); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | -const LITTERALS = Y\LITT | Y\LITT_FOLDED; |
|
| 37 | +const LITTERALS = Y\LITT|Y\LITT_FOLDED; |
|
| 38 | 38 | // print_r(get_defined_constants(true)['user']); |
| 39 | 39 | |
| 40 | 40 | namespace Dallgoot; |
@@ -52,8 +52,8 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public static function getName($typeInteger) |
| 54 | 54 | { |
| 55 | - if(is_null(self::$TYPE_NAMES)) { |
|
| 56 | - $f = function ($v) { return str_replace('Dallgoot\Yaml\\', '', $v);}; |
|
| 55 | + if (is_null(self::$TYPE_NAMES)) { |
|
| 56 | + $f = function($v) { return str_replace('Dallgoot\Yaml\\', '', $v); }; |
|
| 57 | 57 | self::$TYPE_NAMES = array_map($f, array_flip(get_defined_constants(true)['user'])); |
| 58 | 58 | } |
| 59 | 59 | return self::$TYPE_NAMES[$typeInteger]; |
@@ -10,15 +10,15 @@ discard block |
||
| 10 | 10 | //public |
| 11 | 11 | public $errors = []; |
| 12 | 12 | |
| 13 | - public const EXCLUDE_DIRECTIVES = 1;//DONT include_directive |
|
| 14 | - public const IGNORE_COMMENTS = 2;//DONT include_comments |
|
| 15 | - public const EXCEPTIONS_PARSING = 4;//THROW Exception on parsing Errors |
|
| 16 | - public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
|
| 13 | + public const EXCLUDE_DIRECTIVES = 1; //DONT include_directive |
|
| 14 | + public const IGNORE_COMMENTS = 2; //DONT include_comments |
|
| 15 | + public const EXCEPTIONS_PARSING = 4; //THROW Exception on parsing Errors |
|
| 16 | + public const NO_OBJECT_FOR_DATE = 8; //DONT import date strings as dateTime Object |
|
| 17 | 17 | //privates |
| 18 | - private $content;/* @var null|string */ |
|
| 19 | - private $filePath;/* @var null|string */ |
|
| 20 | - private $debug = 0;//TODO: determine levels |
|
| 21 | - private $options = 0;/* @var int */ |
|
| 18 | + private $content; /* @var null|string */ |
|
| 19 | + private $filePath; /* @var null|string */ |
|
| 20 | + private $debug = 0; //TODO: determine levels |
|
| 21 | + private $options = 0; /* @var int */ |
|
| 22 | 22 | //Exceptions messages |
| 23 | 23 | private const INVALID_VALUE = self::class.": at line %d"; |
| 24 | 24 | private const EXCEPTION_NO_FILE = self::class.": file '%s' does not exists (or path is incorrect?)"; |
@@ -27,8 +27,8 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | public function __construct($absolutePath = null, $options = null, $debug = 0) |
| 29 | 29 | { |
| 30 | - $this->debug = is_int($debug) ? min($debug, 3) : 1; |
|
| 31 | - $this->options = is_int($options) ? $options : $this->options; |
|
| 30 | + $this->debug = is_int($debug) ? min($debug, 3) : 1; |
|
| 31 | + $this->options = is_int($options) ? $options : $this->options; |
|
| 32 | 32 | if (is_string($absolutePath)) { |
| 33 | 33 | $this->load($absolutePath); |
| 34 | 34 | } |
@@ -79,10 +79,10 @@ discard block |
||
| 79 | 79 | if (!is_array($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
| 80 | 80 | $previous = $root = new Node(); |
| 81 | 81 | $emptyLines = []; |
| 82 | - $specialTypes = Y\LITTERALS | Y\BLANK; |
|
| 82 | + $specialTypes = Y\LITTERALS|Y\BLANK; |
|
| 83 | 83 | try { |
| 84 | 84 | foreach ($source as $lineNb => $lineString) { |
| 85 | - $n = new Node($lineString, $lineNb + 1);//TODO: useful???-> $this->debug && var_dump($n); |
|
| 85 | + $n = new Node($lineString, $lineNb + 1); //TODO: useful???-> $this->debug && var_dump($n); |
|
| 86 | 86 | $parent = $previous; |
| 87 | 87 | $deepest = $previous->getDeepestNode(); |
| 88 | 88 | if ($deepest->type & Y\PARTIAL) { |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | var_dump("\033[33mParsed Structure\033[0m\n", $root); |
| 115 | 115 | die("Debug of root structure requested (remove debug level to suppress these)"); |
| 116 | 116 | } |
| 117 | - $out = Builder::buildContent($root, $this->debug);//var_dump($out);exit(); |
|
| 117 | + $out = Builder::buildContent($root, $this->debug); //var_dump($out);exit(); |
|
| 118 | 118 | return $out; |
| 119 | 119 | } catch (\ParseError $pe) { |
| 120 | 120 | $message = $pe->getMessage()." on line ".$pe->getLine()." for '$this->filePath'\n"; |
@@ -150,19 +150,19 @@ discard block |
||
| 150 | 150 | { |
| 151 | 151 | $deepest = $previous->getDeepestNode(); |
| 152 | 152 | if ($deepest->type & Y\LITTERALS) { |
| 153 | - $n->value = trim($lineString);//fall through |
|
| 153 | + $n->value = trim($lineString); //fall through |
|
| 154 | 154 | } |
| 155 | - if ($deepest->type & (Y\LITTERALS | Y\REF_DEF | Y\SET_VALUE | Y\TAG)) { |
|
| 155 | + if ($deepest->type & (Y\LITTERALS|Y\REF_DEF|Y\SET_VALUE|Y\TAG)) { |
|
| 156 | 156 | $parent = $deepest; |
| 157 | 157 | return false; |
| 158 | 158 | } |
| 159 | - if ($deepest->type & (Y\BLANK | Y\SCALAR) ) { |
|
| 159 | + if ($deepest->type & (Y\BLANK|Y\SCALAR)) { |
|
| 160 | 160 | if ($n->type === Y\SCALAR && ($deepest->getParent()->type & Y\LITTERALS)) { |
| 161 | 161 | $deepest->type = Y\SCALAR; |
| 162 | 162 | $deepest->value .= "\n".$n->value; |
| 163 | 163 | return true; |
| 164 | 164 | } else { |
| 165 | - if ($previous->type & (Y\ITEM | Y\SET_KEY)) { |
|
| 165 | + if ($previous->type & (Y\ITEM|Y\SET_KEY)) { |
|
| 166 | 166 | $parent = $deepest->getParent(); |
| 167 | 167 | } |
| 168 | 168 | } |
@@ -26,14 +26,14 @@ discard block |
||
| 26 | 26 | private static function buildDLL(DLL $node, &$parent) |
| 27 | 27 | { |
| 28 | 28 | $type = property_exists($node, "type") ? $node->type : null; |
| 29 | - if ($type & (Y\RAW | Y\LITTERALS)) { |
|
| 29 | + if ($type&(Y\RAW|Y\LITTERALS)) { |
|
| 30 | 30 | return self::litteral($node, $type); |
| 31 | 31 | } |
| 32 | 32 | $p = $parent; |
| 33 | 33 | switch ($type) { |
| 34 | 34 | case Y\MAPPING: //fall through |
| 35 | - case Y\SET: $p = new \StdClass;break; |
|
| 36 | - case Y\SEQUENCE: $p = [];break; |
|
| 35 | + case Y\SET: $p = new \StdClass; break; |
|
| 36 | + case Y\SEQUENCE: $p = []; break; |
|
| 37 | 37 | // case Y\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 Y\COMMENT: self::$_root->addComment($line, $value);return; |
|
| 58 | - case Y\DIRECTIVE: return;//TODO |
|
| 59 | - case Y\ITEM: self::buildItem($value, $parent);return; |
|
| 60 | - case Y\KEY: self::buildKey($node, $parent);return; |
|
| 57 | + case Y\COMMENT: self::$_root->addComment($line, $value); return; |
|
| 58 | + case Y\DIRECTIVE: return; //TODO |
|
| 59 | + case Y\ITEM: self::buildItem($value, $parent); return; |
|
| 60 | + case Y\KEY: self::buildKey($node, $parent); return; |
|
| 61 | 61 | case Y\REF_DEF: //fall through |
| 62 | 62 | case Y\REF_CALL://TODO: self::build returns what ? |
| 63 | 63 | $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
@@ -72,8 +72,8 @@ discard block |
||
| 72 | 72 | case Y\SET_VALUE: |
| 73 | 73 | $prop = array_keys(get_object_vars($parent)); |
| 74 | 74 | $key = end($prop); |
| 75 | - if (property_exists($value, "type") && ($value->type & (Y\ITEM | Y\MAPPING))) { |
|
| 76 | - $p = $value->type === Y\ITEM ? [] : new \StdClass; |
|
| 75 | + if (property_exists($value, "type") && ($value->type & (Y\ITEM|Y\MAPPING))) { |
|
| 76 | + $p = $value->type === Y\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 Y\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 = Y\RAW; |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | if (is_null($identifier)) { |
| 111 | 111 | throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $node->line)); |
| 112 | 112 | } else { |
| 113 | - if ($value instanceof Node && ($value->type & (Y\KEY | Y\ITEM)) { |
|
| 113 | + if ($value instanceof Node && ($value->type & (Y\KEY|Y\ITEM)) { |
|
| 114 | 114 | $parent->{$identifier} = $value->type === Y\KEY ? new \StdClass : []; |
| 115 | 115 | self::build($value, $parent->{$identifier}); |
| 116 | 116 | } elseif (is_object($value)) { |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new DLL(); |
| 158 | 158 | $documents[$currentDoc]->push($child); |
| 159 | 159 | } |
| 160 | - $_debug >= 2 && var_dump($documents);//var_dump($documents);die("documents"); |
|
| 160 | + $_debug >= 2 && var_dump($documents); //var_dump($documents);die("documents"); |
|
| 161 | 161 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
| 162 | 162 | return count($content) === 1 ? $content[0] : $content; |
| 163 | 163 | } |
@@ -173,8 +173,8 @@ discard block |
||
| 173 | 173 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
| 174 | 174 | } else { |
| 175 | 175 | switch (true) { |
| 176 | - case $isSequence: $list->type = Y\SEQUENCE;break; |
|
| 177 | - case $isSet: $list->type = Y\SET;break; |
|
| 176 | + case $isSequence: $list->type = Y\SEQUENCE; break; |
|
| 177 | + case $isSet: $list->type = Y\SET; break; |
|
| 178 | 178 | case $isMapping://fall through |
| 179 | 179 | default: $list->type = Y\MAPPING; |
| 180 | 180 | } |
@@ -198,10 +198,10 @@ discard block |
||
| 198 | 198 | $children->rewind(); |
| 199 | 199 | $refIndent = $children->current()->indent; |
| 200 | 200 | $separator = $type === Y\RAW ? '' : "\n"; |
| 201 | - $action = function ($c) { return $c->value; }; |
|
| 202 | - if ($type & Y\LITT_FOLDED) { |
|
| 201 | + $action = function($c) { return $c->value; }; |
|
| 202 | + if ($type&Y\LITT_FOLDED) { |
|
| 203 | 203 | $separator = ' '; |
| 204 | - $action = function ($c) use ($refIndent) { |
|
| 204 | + $action = function($c) use ($refIndent) { |
|
| 205 | 205 | return $c->indent > $refIndent || ($c->type & Y\EMPTY) ? "\n".$c->value : $c->value; |
| 206 | 206 | }; |
| 207 | 207 | } |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
| 40 | 40 | self::$options = is_int($options) ? $options : self::OPTIONS; |
| 41 | 41 | self::$result = new DLL; |
| 42 | - self::$result->setIteratorMode(DLL::IT_MODE_FIFO | DLL::IT_MODE_DELETE); |
|
| 42 | + self::$result->setIteratorMode(DLL::IT_MODE_FIFO|DLL::IT_MODE_DELETE); |
|
| 43 | 43 | if ($dataType instanceof YamlObject) { |
| 44 | 44 | self::dumpYamlObject($dataType); |
| 45 | 45 | } elseif (is_array($dataType) && $dataType[0] instanceof YamlObject) { |
@@ -108,9 +108,9 @@ discard block |
||
| 108 | 108 | foreach ($array as $key => $item) { |
| 109 | 109 | $lineStart = current($refKeys) === $key ? "- " : "- $key: "; |
| 110 | 110 | if (is_scalar($item)) { |
| 111 | - self::add($lineStart.$item, $indent ); |
|
| 111 | + self::add($lineStart.$item, $indent); |
|
| 112 | 112 | } else { |
| 113 | - self::add($lineStart, $indent ); |
|
| 113 | + self::add($lineStart, $indent); |
|
| 114 | 114 | self::dump($item, $indent + self::INDENT); |
| 115 | 115 | } |
| 116 | 116 | next($refKeys); |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | if ($object instanceof Tag) { |
| 130 | 130 | if (is_scalar($object->value)) { |
| 131 | 131 | return "!".$object->tagName.' '.$object->value; |
| 132 | - } else{ |
|
| 132 | + } else { |
|
| 133 | 133 | yield "!".$object->tagName; |
| 134 | 134 | self::dump($object->value, $indent + self::INDENT); |
| 135 | 135 | } |
@@ -92,9 +92,9 @@ discard block |
||
| 92 | 92 | //modify type according to child |
| 93 | 93 | if ($this->value instanceof DLL && !property_exists($this->value, "type")) { |
| 94 | 94 | switch ($child->type) { |
| 95 | - case Y\KEY: $this->value->type = Y\MAPPING;break; |
|
| 96 | - case Y\ITEM: $this->value->type = Y\SEQUENCE;break; |
|
| 97 | - case Y\SCALAR: $this->value->type = $this->type;break; |
|
| 95 | + case Y\KEY: $this->value->type = Y\MAPPING; break; |
|
| 96 | + case Y\ITEM: $this->value->type = Y\SEQUENCE; break; |
|
| 97 | + case Y\SCALAR: $this->value->type = $this->type; break; |
|
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | 100 | } |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | public function parse(string $nodeString):Node |
| 119 | 119 | { |
| 120 | - $nodeValue = preg_replace("/^\t+/m", " ", $nodeString);//permissive to tabs but replacement |
|
| 120 | + $nodeValue = preg_replace("/^\t+/m", " ", $nodeString); //permissive to tabs but replacement |
|
| 121 | 121 | $this->indent = strspn($nodeValue, ' '); |
| 122 | 122 | $nodeValue = ltrim($nodeValue); |
| 123 | 123 | if ($nodeValue === '') { |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | case '#': return [Y\COMMENT, ltrim($v)]; |
| 154 | 154 | case "-": return $this->onHyphen($nodeValue); |
| 155 | 155 | case '%': return [Y\DIRECTIVE, ltrim($v)]; |
| 156 | - case '?': return [Y\SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 156 | + case '?': return [Y\SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 157 | 157 | case ':': return [Y\SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
| 158 | 158 | case '>': return [Y\LITT_FOLDED, null]; |
| 159 | 159 | case '|': return [Y\LITT, null]; |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | if (!is_bool($hasComment)) { |
| 179 | 179 | $tmpNode = new Node(trim(substr($keyValue, 0, $hasComment)), $this->line); |
| 180 | 180 | if ($tmpNode->type !== Y\PARTIAL) { |
| 181 | - $comment = new Node(trim(substr($keyValue, $hasComment+1)), $this->line); |
|
| 181 | + $comment = new Node(trim(substr($keyValue, $hasComment + 1)), $this->line); |
|
| 182 | 182 | //TODO: modify "identifier" to specify if fullline comment or not |
| 183 | 183 | $this->add($comment); |
| 184 | 184 | $n = $tmpNode; |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | $type = ['!' => Y\TAG, '&' => Y\REF_DEF, '*' => Y\REF_CALL][$nodeValue[0]]; |
| 248 | 248 | $pos = strpos($v, ' '); |
| 249 | 249 | $this->identifier = is_bool($pos) ? $v : strstr($v, ' ', true); |
| 250 | - $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos+1)), $this->line))->setParent($this); |
|
| 250 | + $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos + 1)), $this->line))->setParent($this); |
|
| 251 | 251 | return [$type, $n]; |
| 252 | 252 | } |
| 253 | 253 | |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | case Y\MAPPING_SHORT: return self::getShortMapping(substr($v, 1, -1)); |
| 271 | 271 | //TODO : that's not robust enough, improve it |
| 272 | 272 | case Y\SEQUENCE_SHORT: |
| 273 | - $f = function ($e) { return self::getScalar(trim($e));}; |
|
| 273 | + $f = function($e) { return self::getScalar(trim($e)); }; |
|
| 274 | 274 | return array_map($f, explode(",", substr($v, 1, -1))); |
| 275 | 275 | default: |
| 276 | 276 | trigger_error("Error can not get PHP type for ".Y\getName($this->type), E_USER_WARNING); |