@@ -1,5 +1,5 @@ |
||
| 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 | |
@@ -15,6 +15,6 @@ |
||
| 15 | 15 | |
| 16 | 16 | public function __debugInfo():array |
| 17 | 17 | { |
| 18 | - return ['type' => Y::getName($this->type) , "dllist" => $this->dllist]; |
|
| 18 | + return ['type' => Y::getName($this->type), "dllist" => $this->dllist]; |
|
| 19 | 19 | } |
| 20 | 20 | } |
| 21 | 21 | \ No newline at end of file |
@@ -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 NO_PARSING_EXCEPTIONS = 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 NO_PARSING_EXCEPTIONS = 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 | $deepest = $previous->getDeepestNode(); |
| 87 | 87 | if ($deepest->type & Y\PARTIAL) { |
| 88 | 88 | //TODO:verify this edge case |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | } |
| 102 | 102 | $emptyLines = []; |
| 103 | 103 | switch ($n->indent <=> $previous->indent) { |
| 104 | - case -1: $previous->getParent($n->indent)->add($n);break; |
|
| 105 | - case 0: $previous->getParent()->add($n);break; |
|
| 104 | + case -1: $previous->getParent($n->indent)->add($n); break; |
|
| 105 | + case 0: $previous->getParent()->add($n); break; |
|
| 106 | 106 | default: |
| 107 | 107 | if ($this->onDeepestType($n, $previous, $lineString)) continue 2; |
| 108 | 108 | $previous->add($n); |
@@ -114,11 +114,11 @@ 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 (\Error|\Exception|\ParseError $e) { |
| 120 | 120 | $file = basename($this->filePath); |
| 121 | - $message = basename($e->getFile())."@".$e->getLine().":".$e->getMessage()." in '$file' @".($lineNb+1)."\n"; |
|
| 121 | + $message = basename($e->getFile())."@".$e->getLine().":".$e->getMessage()." in '$file' @".($lineNb + 1)."\n"; |
|
| 122 | 122 | if ($e instanceof \ParseError && ($this->options & self::NO_PARSING_EXCEPTIONS)) { |
| 123 | 123 | trigger_error($message, E_USER_WARNING); |
| 124 | 124 | $this->errors[] = $message; |
@@ -158,7 +158,7 @@ discard block |
||
| 158 | 158 | // // if ($previous->type & Y\ITEM && $n->type & Y\KEY) { |
| 159 | 159 | // // $previous |
| 160 | 160 | // // } |
| 161 | - if ($deepest->type & (Y\BLANK | Y\SCALAR) ) {//|| ($previous->type & (Y\ITEM | Y\SET_KEY))) { |
|
| 161 | + if ($deepest->type & (Y\BLANK|Y\SCALAR)) {//|| ($previous->type & (Y\ITEM | Y\SET_KEY))) { |
|
| 162 | 162 | $previous = $deepest->getParent(); |
| 163 | 163 | // // if ($n->type === Y\SCALAR && ($deepest->getParent()->type & Y\LITTERALS)) { |
| 164 | 164 | // // // $deepest->type = Y\SCALAR; |
@@ -25,14 +25,14 @@ discard block |
||
| 25 | 25 | private static function buildNodeList(NodeList $node, &$parent) |
| 26 | 26 | { |
| 27 | 27 | $type = property_exists($node, "type") ? $node->type : null; |
| 28 | - if ($type & (Y\RAW | Y\LITTERALS)) { |
|
| 28 | + if ($type&(Y\RAW|Y\LITTERALS)) { |
|
| 29 | 29 | return self::litteral($node, $type); |
| 30 | 30 | } |
| 31 | 31 | $p = $parent; |
| 32 | 32 | switch ($type) { |
| 33 | 33 | case Y\MAPPING: //fall through |
| 34 | - case Y\SET: $p = new \StdClass;break; |
|
| 35 | - case Y\SEQUENCE: $p = [];break; |
|
| 34 | + case Y\SET: $p = new \StdClass; break; |
|
| 35 | + case Y\SEQUENCE: $p = []; break; |
|
| 36 | 36 | // case Y\KEY: $p = $parent;break; |
| 37 | 37 | } |
| 38 | 38 | $out = null; |
@@ -53,10 +53,10 @@ discard block |
||
| 53 | 53 | { |
| 54 | 54 | list($line, $type, $value, $identifier) = [$node->line, $node->type, $node->value, $node->identifier]; |
| 55 | 55 | switch ($type) { |
| 56 | - case Y\COMMENT: self::$_root->addComment($line, $value);return; |
|
| 57 | - case Y\DIRECTIVE: return;//TODO |
|
| 58 | - case Y\ITEM: self::buildItem($value, $parent);return; |
|
| 59 | - case Y\KEY: self::buildKey($node, $parent);return; |
|
| 56 | + case Y\COMMENT: self::$_root->addComment($line, $value); return; |
|
| 57 | + case Y\DIRECTIVE: return; //TODO |
|
| 58 | + case Y\ITEM: self::buildItem($value, $parent); return; |
|
| 59 | + case Y\KEY: self::buildKey($node, $parent); return; |
|
| 60 | 60 | case Y\REF_DEF: //fall through |
| 61 | 61 | case Y\REF_CALL://TODO: self::build returns what ? |
| 62 | 62 | $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
@@ -71,8 +71,8 @@ discard block |
||
| 71 | 71 | case Y\SET_VALUE: |
| 72 | 72 | $prop = array_keys(get_object_vars($parent)); |
| 73 | 73 | $key = end($prop); |
| 74 | - if (property_exists($value, "type") && ($value->type & (Y\ITEM | Y\MAPPING))) { |
|
| 75 | - $p = $value->type === Y\ITEM ? [] : new \StdClass; |
|
| 74 | + if (property_exists($value, "type") && ($value->type & (Y\ITEM|Y\MAPPING))) { |
|
| 75 | + $p = $value->type === Y\ITEM ? [] : new \StdClass; |
|
| 76 | 76 | self::build($value, $p); |
| 77 | 77 | } else { |
| 78 | 78 | $p = self::build($value, $parent->{$key}); |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | return; |
| 82 | 82 | case Y\TAG: |
| 83 | 83 | if ($parent === self::$_root) { |
| 84 | - $parent->addTag($identifier);return; |
|
| 84 | + $parent->addTag($identifier); return; |
|
| 85 | 85 | } else {//TODO: have somewhere a list of common tags and their treatment |
| 86 | 86 | if (in_array($identifier, ['!binary', '!str'])) { |
| 87 | 87 | if ($value->value instanceof NodeList) $value->value->type = Y\RAW; |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | if (is_null($identifier)) { |
| 110 | 110 | throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $node->line)); |
| 111 | 111 | } else { |
| 112 | - if ($value instanceof Node && ($value->type & (Y\KEY | Y\ITEM))) { |
|
| 112 | + if ($value instanceof Node && ($value->type & (Y\KEY|Y\ITEM))) { |
|
| 113 | 113 | $parent->{$identifier} = $value->type & Y\KEY ? new \StdClass : []; |
| 114 | 114 | self::build($value, $parent->{$identifier}); |
| 115 | 115 | } elseif (is_object($value)) { |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | |
| 123 | 123 | private static function buildItem($value, &$parent):void |
| 124 | 124 | { |
| 125 | - if(!is_array($parent) && !($parent instanceof \ArrayIterator)) { |
|
| 125 | + if (!is_array($parent) && !($parent instanceof \ArrayIterator)) { |
|
| 126 | 126 | throw new \Exception("parent must be an Iterable not ".(is_object($parent) ? get_class($parent) : gettype($parent)), 1); |
| 127 | 127 | } |
| 128 | 128 | if ($value instanceof Node && $value->type === Y\KEY) { |
@@ -175,8 +175,8 @@ discard block |
||
| 175 | 175 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
| 176 | 176 | } else { |
| 177 | 177 | switch (true) { |
| 178 | - case $isSequence: $list->type = Y\SEQUENCE;break; |
|
| 179 | - case $isSet: $list->type = Y\SET;break; |
|
| 178 | + case $isSequence: $list->type = Y\SEQUENCE; break; |
|
| 179 | + case $isSet: $list->type = Y\SET; break; |
|
| 180 | 180 | case $isMapping://fall through |
| 181 | 181 | default: $list->type = Y\MAPPING; |
| 182 | 182 | } |
@@ -200,10 +200,10 @@ discard block |
||
| 200 | 200 | $children->rewind(); |
| 201 | 201 | $refIndent = $children->current()->indent; |
| 202 | 202 | $separator = $type === Y\RAW ? '' : "\n"; |
| 203 | - $action = function ($c) { return $c->value; }; |
|
| 204 | - if ($type & Y\LITT_FOLDED) { |
|
| 203 | + $action = function($c) { return $c->value; }; |
|
| 204 | + if ($type&Y\LITT_FOLDED) { |
|
| 205 | 205 | $separator = ' '; |
| 206 | - $action = function ($c) use ($refIndent) { |
|
| 206 | + $action = function($c) use ($refIndent) { |
|
| 207 | 207 | return $c->indent > $refIndent || ($c->type & Y\BLANK) ? "\n".$c->value : $c->value; |
| 208 | 208 | }; |
| 209 | 209 | } |
@@ -89,8 +89,8 @@ discard block |
||
| 89 | 89 | //modify type according to child |
| 90 | 90 | switch ($child->type) { |
| 91 | 91 | case Y\COMMENT: //fall through |
| 92 | - case Y\KEY: $this->value->type = Y\MAPPING;break; |
|
| 93 | - case Y\ITEM: $this->value->type = Y\SEQUENCE;break; |
|
| 92 | + case Y\KEY: $this->value->type = Y\MAPPING; break; |
|
| 93 | + case Y\ITEM: $this->value->type = Y\SEQUENCE; break; |
|
| 94 | 94 | // case Y\COMMENT: $this->value->type = Y\RAW;break; |
| 95 | 95 | } |
| 96 | 96 | } |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | */ |
| 116 | 116 | public function parse(string $nodeString):Node |
| 117 | 117 | { |
| 118 | - $nodeValue = preg_replace("/^\t+/m", " ", $nodeString);//permissive to tabs but replacement |
|
| 118 | + $nodeValue = preg_replace("/^\t+/m", " ", $nodeString); //permissive to tabs but replacement |
|
| 119 | 119 | $this->indent = strspn($nodeValue, ' '); |
| 120 | 120 | $nodeValue = ltrim($nodeValue); |
| 121 | 121 | if ($nodeValue === '') { |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | case '#': return [Y\COMMENT, ltrim($v)]; |
| 153 | 153 | case "-": return $this->onHyphen($nodeValue); |
| 154 | 154 | case '%': return [Y\DIRECTIVE, ltrim($v)]; |
| 155 | - case '?': return [Y\SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 155 | + case '?': return [Y\SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 156 | 156 | case ':': return [Y\SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
| 157 | 157 | case '>': return [Y\LITT_FOLDED, null]; |
| 158 | 158 | case '|': return [Y\LITT, null]; |
@@ -177,7 +177,7 @@ discard block |
||
| 177 | 177 | if (!is_bool($hasComment)) { |
| 178 | 178 | $tmpNode = new Node(trim(substr($keyValue, 0, $hasComment)), $this->line); |
| 179 | 179 | if ($tmpNode->type !== Y\PARTIAL) { |
| 180 | - $comment = new Node(trim(substr($keyValue, $hasComment+1)), $this->line); |
|
| 180 | + $comment = new Node(trim(substr($keyValue, $hasComment + 1)), $this->line); |
|
| 181 | 181 | //TODO: modify "identifier" to specify if fullline comment or not |
| 182 | 182 | $this->add($comment); |
| 183 | 183 | $n = $tmpNode; |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | $type = ['!' => Y\TAG, '&' => Y\REF_DEF, '*' => Y\REF_CALL][$nodeValue[0]]; |
| 247 | 247 | $pos = strpos($v, ' '); |
| 248 | 248 | $this->identifier = is_bool($pos) ? $v : strstr($v, ' ', true); |
| 249 | - $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos+1)), $this->line))->setParent($this); |
|
| 249 | + $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos + 1)), $this->line))->setParent($this); |
|
| 250 | 250 | return [$type, $n]; |
| 251 | 251 | } |
| 252 | 252 | |
@@ -260,8 +260,8 @@ discard block |
||
| 260 | 260 | { |
| 261 | 261 | $v = $this->value; |
| 262 | 262 | if (is_null($v)) return null; |
| 263 | - if ($this->type & (Y\REF_CALL | Y\SCALAR)) return self::getScalar($v); |
|
| 264 | - if ($this->type & (Y\COMPACT_MAPPING | Y\COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 263 | + if ($this->type & (Y\REF_CALL|Y\SCALAR)) return self::getScalar($v); |
|
| 264 | + if ($this->type & (Y\COMPACT_MAPPING|Y\COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 265 | 265 | switch ($this->type) { |
| 266 | 266 | case Y\JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
| 267 | 267 | case Y\QUOTED: return substr($v, 1, -1); |
@@ -317,7 +317,7 @@ discard block |
||
| 317 | 317 | { |
| 318 | 318 | $out = new Compact(); |
| 319 | 319 | if ($type === Y\COMPACT_SEQUENCE) { |
| 320 | - $f = function ($e) { return self::getScalar(trim($e));}; |
|
| 320 | + $f = function($e) { return self::getScalar(trim($e)); }; |
|
| 321 | 321 | //TODO : that's not robust enough, improve it |
| 322 | 322 | foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) { |
| 323 | 323 | $out[$key] = $value; |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | 'COMPACT_SEQUENCE', |
| 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]; |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | // |
| 22 | 22 | public function __construct() |
| 23 | 23 | { |
| 24 | - parent::__construct([], 1);//1 = Array indices can be accessed as properties in read/write. |
|
| 24 | + parent::__construct([], 1); //1 = Array indices can be accessed as properties in read/write. |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | public function jsonSerialize() |