@@ -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 | } |
@@ -74,9 +74,13 @@ discard block |
||
74 | 74 | public function parse($strContent = null) |
75 | 75 | { |
76 | 76 | $source = $this->content; |
77 | - if (is_null($source)) $source = preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
|
77 | + if (is_null($source)) { |
|
78 | + $source = preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
|
79 | + } |
|
78 | 80 | //TODO : be more permissive on $strContent values |
79 | - if (!is_array($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
81 | + if (!is_array($source)) { |
|
82 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
83 | + } |
|
80 | 84 | $previous = $root = new Node(); |
81 | 85 | $emptyLines = []; |
82 | 86 | $specialTypes = Y\LITTERALS | Y\BLANK; |
@@ -93,7 +97,9 @@ discard block |
||
93 | 97 | $deepest->parse($deepest->value.' '.ltrim($lineString)); |
94 | 98 | } else { |
95 | 99 | if ($n->type & $specialTypes) { |
96 | - if ($this->onSpecialType($n, $parent, $previous, $emptyLines)) continue; |
|
100 | + if ($this->onSpecialType($n, $parent, $previous, $emptyLines)) { |
|
101 | + continue; |
|
102 | + } |
|
97 | 103 | } |
98 | 104 | foreach ($emptyLines as $key => $node) { |
99 | 105 | $node->getParent()->add($node); |
@@ -104,7 +110,9 @@ discard block |
||
104 | 110 | } elseif ($n->indent === $previous->indent) { |
105 | 111 | $parent = $previous->getParent(); |
106 | 112 | } elseif ($n->indent > $previous->indent) { |
107 | - if ($this->onDeepestType($n, $parent, $previous, $lineString)) continue; |
|
113 | + if ($this->onDeepestType($n, $parent, $previous, $lineString)) { |
|
114 | + continue; |
|
115 | + } |
|
108 | 116 | } |
109 | 117 | $parent->add($n); |
110 | 118 | $previous = $n; |
@@ -139,8 +147,12 @@ discard block |
||
139 | 147 | } |
140 | 148 | } |
141 | 149 | if ($n->type & Y\BLANK) { |
142 | - if ($previous->type & Y\SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
143 | - if ($deepest->type & Y\LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
150 | + if ($previous->type & Y\SCALAR) { |
|
151 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
152 | + } |
|
153 | + if ($deepest->type & Y\LITTERALS) { |
|
154 | + $emptyLines[] = $n->setParent($deepest); |
|
155 | + } |
|
144 | 156 | return true; |
145 | 157 | } |
146 | 158 | return false; |
@@ -2,15 +2,15 @@ |
||
2 | 2 | namespace Dallgoot\Yaml; |
3 | 3 | |
4 | 4 | /** |
5 | - * The returned object representing a YAML file content |
|
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) |
|
5 | + * The returned object representing a YAML file content |
|
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) |
|
14 | 14 | */ |
15 | 15 | class YamlObject extends \ArrayIterator implements \JsonSerializable |
16 | 16 | { |
@@ -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 | } |
@@ -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 Y\REF_DEF: //fall through |
62 | 64 | case Y\REF_CALL://TODO: self::build returns what ? |
63 | 65 | $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
64 | - if ($type === Y\REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
66 | + if ($type === Y\REF_DEF) { |
|
67 | + self::$_root->addReference($identifier, $tmp); |
|
68 | + } |
|
65 | 69 | return self::$_root->getReference($identifier); |
66 | 70 | case Y\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 Y\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 = Y\RAW; |
|
89 | - else $value->type = Y\RAW; |
|
93 | + if ($value->value instanceof DLL) { |
|
94 | + $value->value->type = Y\RAW; |
|
95 | + } else { |
|
96 | + $value->type = Y\RAW; |
|
97 | + } |
|
90 | 98 | } |
91 | 99 | $val = is_null($value) ? null : self::build($value, $node); |
92 | 100 | return new Tag($identifier, $val); |
@@ -151,10 +159,14 @@ discard block |
||
151 | 159 | } |
152 | 160 | $_root->value->setIteratorMode(DLL::IT_MODE_DELETE); |
153 | 161 | foreach ($_root->value as $child) { |
154 | - if ($child->type & Y\DOC_START) $totalDocStart++; |
|
162 | + if ($child->type & Y\DOC_START) { |
|
163 | + $totalDocStart++; |
|
164 | + } |
|
155 | 165 | //if 0 or 1 DOC_START = we are still in first document |
156 | 166 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
157 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new DLL(); |
|
167 | + if (!isset($documents[$currentDoc])) { |
|
168 | + $documents[$currentDoc] = new DLL(); |
|
169 | + } |
|
158 | 170 | $documents[$currentDoc]->push($child); |
159 | 171 | } |
160 | 172 | $_debug >= 2 && var_dump($documents);//var_dump($documents);die("documents"); |
@@ -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 | } |
@@ -21,7 +21,9 @@ 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 | /** |
@@ -36,7 +38,9 @@ discard block |
||
36 | 38 | */ |
37 | 39 | public static function toString($dataType, int $options):string |
38 | 40 | { |
39 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
41 | + if (is_null($dataType)) { |
|
42 | + throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
43 | + } |
|
40 | 44 | self::$options = is_int($options) ? $options : self::OPTIONS; |
41 | 45 | self::$result = new DLL; |
42 | 46 | self::$result->setIteratorMode(DLL::IT_MODE_FIFO | DLL::IT_MODE_DELETE); |
@@ -74,8 +78,12 @@ discard block |
||
74 | 78 | if (is_scalar($dataType)) { |
75 | 79 | switch (gettype($dataType)) { |
76 | 80 | case 'boolean': return $dataType ? 'true' : 'false'; |
77 | - case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
|
78 | - case 'double': if (is_nan($dataType)) return '.nan'; |
|
81 | + case 'float': if (is_infinite($dataType)) { |
|
82 | + return $dataType > 0 ? '.inf' : '-.inf'; |
|
83 | + } |
|
84 | + case 'double': if (is_nan($dataType)) { |
|
85 | + return '.nan'; |
|
86 | + } |
|
79 | 87 | default: |
80 | 88 | return $dataType; |
81 | 89 | } |
@@ -288,13 +288,13 @@ |
||
288 | 288 | private static function getScalar(string $v) |
289 | 289 | { |
290 | 290 | $types = ['yes' => true, |
291 | - 'no' => false, |
|
292 | - 'true' => true, |
|
293 | - 'false' => false, |
|
294 | - 'null' => null, |
|
295 | - '.inf' => INF, |
|
296 | - '-.inf' => -INF, |
|
297 | - '.nan' => NAN |
|
291 | + 'no' => false, |
|
292 | + 'true' => true, |
|
293 | + 'false' => false, |
|
294 | + 'null' => null, |
|
295 | + '.inf' => INF, |
|
296 | + '-.inf' => -INF, |
|
297 | + '.nan' => NAN |
|
298 | 298 | ]; |
299 | 299 | if (isset($types[strtolower($v)])) return $types[strtolower($v)]; |
300 | 300 | if (R::isDate($v)) return date_create($v); |
@@ -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); |
@@ -56,7 +56,9 @@ discard block |
||
56 | 56 | */ |
57 | 57 | public function getParent(int $indent = null):Node |
58 | 58 | { |
59 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
59 | + if (!is_int($indent)) { |
|
60 | + return $this->parent ?? $this; |
|
61 | + } |
|
60 | 62 | $cursor = $this; |
61 | 63 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
62 | 64 | $cursor = $cursor->parent; |
@@ -76,7 +78,9 @@ discard block |
||
76 | 78 | { |
77 | 79 | $child->setParent($this); |
78 | 80 | $current = $this->value; |
79 | - if ($this->type & Y\LITTERALS) $child->type = Y\SCALAR; |
|
81 | + if ($this->type & Y\LITTERALS) { |
|
82 | + $child->type = Y\SCALAR; |
|
83 | + } |
|
80 | 84 | if (is_null($current)) { |
81 | 85 | $this->value = $child; |
82 | 86 | return; |
@@ -147,8 +151,12 @@ discard block |
||
147 | 151 | $type = R::isProperlyQuoted($nodeValue) ? Y\QUOTED : Y\PARTIAL; |
148 | 152 | return [$type, $nodeValue]; |
149 | 153 | } |
150 | - if (in_array($nodeValue[0], ['{', '['])) return $this->onObject($nodeValue); |
|
151 | - if (in_array($nodeValue[0], ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
|
154 | + if (in_array($nodeValue[0], ['{', '['])) { |
|
155 | + return $this->onObject($nodeValue); |
|
156 | + } |
|
157 | + if (in_array($nodeValue[0], ['!', '&', '*'])) { |
|
158 | + return $this->onNodeAction($nodeValue); |
|
159 | + } |
|
152 | 160 | switch ($nodeValue[0]) { |
153 | 161 | case '#': return [Y\COMMENT, ltrim($v)]; |
154 | 162 | case "-": return $this->onHyphen($nodeValue); |
@@ -200,9 +208,15 @@ discard block |
||
200 | 208 | private function onObject($value):array |
201 | 209 | { |
202 | 210 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
203 | - if (json_last_error() === JSON_ERROR_NONE) return [Y\JSON, $value]; |
|
204 | - if (preg_match(R::MAPPING, $value)) return [Y\MAPPING_SHORT, $value]; |
|
205 | - if (preg_match(R::SEQUENCE, $value)) return [Y\SEQUENCE_SHORT, $value]; |
|
211 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
212 | + return [Y\JSON, $value]; |
|
213 | + } |
|
214 | + if (preg_match(R::MAPPING, $value)) { |
|
215 | + return [Y\MAPPING_SHORT, $value]; |
|
216 | + } |
|
217 | + if (preg_match(R::SEQUENCE, $value)) { |
|
218 | + return [Y\SEQUENCE_SHORT, $value]; |
|
219 | + } |
|
206 | 220 | return [Y\PARTIAL, $value]; |
207 | 221 | } |
208 | 222 | |
@@ -217,7 +231,9 @@ discard block |
||
217 | 231 | { |
218 | 232 | if (substr($nodeValue, 0, 3) === '---') { |
219 | 233 | $rest = trim(substr($nodeValue, 3)); |
220 | - if (empty($rest)) return [Y\DOC_START, null]; |
|
234 | + if (empty($rest)) { |
|
235 | + return [Y\DOC_START, null]; |
|
236 | + } |
|
221 | 237 | $n = new Node($rest, $this->line); |
222 | 238 | $n->indent = $this->indent + 4; |
223 | 239 | return [Y\DOC_START, $n->setParent($this)]; |
@@ -260,7 +276,9 @@ discard block |
||
260 | 276 | public function getPhpValue() |
261 | 277 | { |
262 | 278 | $v = $this->value; |
263 | - if (is_null($v)) return null; |
|
279 | + if (is_null($v)) { |
|
280 | + return null; |
|
281 | + } |
|
264 | 282 | switch ($this->type) { |
265 | 283 | case Y\JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
266 | 284 | case Y\QUOTED: return substr($v, 1, -1); |
@@ -296,9 +314,15 @@ discard block |
||
296 | 314 | '-.inf' => -INF, |
297 | 315 | '.nan' => NAN |
298 | 316 | ]; |
299 | - if (isset($types[strtolower($v)])) return $types[strtolower($v)]; |
|
300 | - if (R::isDate($v)) return date_create($v); |
|
301 | - if (R::isNumber($v)) return self::getNumber($v); |
|
317 | + if (isset($types[strtolower($v)])) { |
|
318 | + return $types[strtolower($v)]; |
|
319 | + } |
|
320 | + if (R::isDate($v)) { |
|
321 | + return date_create($v); |
|
322 | + } |
|
323 | + if (R::isNumber($v)) { |
|
324 | + return self::getNumber($v); |
|
325 | + } |
|
302 | 326 | return strval($v); |
303 | 327 | } |
304 | 328 | |
@@ -311,8 +335,12 @@ discard block |
||
311 | 335 | */ |
312 | 336 | private static function getNumber(string $v) |
313 | 337 | { |
314 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
315 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
338 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
339 | + return intval(base_convert($v, 8, 10)); |
|
340 | + } |
|
341 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
342 | + return intval(base_convert($v, 16, 10)); |
|
343 | + } |
|
316 | 344 | // TODO: remove these if not needed |
317 | 345 | // if preg_match("/^([\d.]+e[-+]\d{1,2})$/", $v)://fall through |
318 | 346 | // if preg_match("/^([-+]?(?:\d+|\d*.\d+))$/", $v): |