@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | } |
102 | 102 | } |
103 | 103 | $previousCompactMode = $this->dumper->_compactMode; |
104 | - $this->dumper->_compactMode = true; |
|
104 | + $this->dumper->_compactMode = true; |
|
105 | 105 | $result = $this->dumper->iteratorToString(new \ArrayIterator($source), $keyMask, ', ', $indent); |
106 | 106 | $this->dumper->_compactMode = $previousCompactMode; |
107 | 107 | return sprintf($structureFormat, $result); |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | // Example 5.13. Escaped Characters |
124 | 124 | |
125 | 125 | $str = json_encode(ltrim($str)); |
126 | - return strspn(substr($str,1,-1), "-:?{}[]#,&*!>|'\"%") > 0 ? $str : trim($str, '"'); |
|
126 | + return strspn(substr($str, 1, -1), "-:?{}[]#,&*!>|'\"%") > 0 ? $str : trim($str, '"'); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | public function dumpTagged(Tagged $obj, int $indent):string |
@@ -22,8 +22,12 @@ |
||
22 | 22 | |
23 | 23 | public function dumpScalar($dataType):string |
24 | 24 | { |
25 | - if ($dataType === \INF) return '.inf'; |
|
26 | - if ($dataType === -\INF) return '-.inf'; |
|
25 | + if ($dataType === \INF) { |
|
26 | + return '.inf'; |
|
27 | + } |
|
28 | + if ($dataType === -\INF) { |
|
29 | + return '-.inf'; |
|
30 | + } |
|
27 | 31 | $precision = "%.".$this->dumper->floatPrecision."F"; |
28 | 32 | switch (gettype($dataType)) { |
29 | 33 | case 'boolean': return $dataType ? 'true' : 'false'; |
@@ -105,8 +105,8 @@ discard block |
||
105 | 105 | public function dumpYamlObject(YamlObject $obj):string |
106 | 106 | { |
107 | 107 | if ($this->multipleDocs || $obj->hasDocStart() || $obj->isTagged()) { |
108 | - $this->multipleDocs = true; |
|
109 | - // && $this->$result instanceof DLL) $this->$result->push("---"); |
|
108 | + $this->multipleDocs = true; |
|
109 | + // && $this->$result instanceof DLL) $this->$result->push("---"); |
|
110 | 110 | } |
111 | 111 | // $this->insertComments($obj->getComment()); |
112 | 112 | if (count($obj) > 0) { |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | |
119 | 119 | |
120 | 120 | public function iteratorToString(\Iterator $iterable, |
121 | - string $keyMask, string $itemSeparator, int $indent):string |
|
121 | + string $keyMask, string $itemSeparator, int $indent):string |
|
122 | 122 | { |
123 | 123 | $pairs = []; |
124 | 124 | $valueIndent = $indent + self::INDENT; |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | private $handler; |
32 | 32 | |
33 | 33 | |
34 | - public function __construct($options=null) |
|
34 | + public function __construct($options = null) |
|
35 | 35 | { |
36 | 36 | $this->options = is_int($options) ? $options : self::OPTIONS; |
37 | 37 | $this->handler = new DumperHandlers($this); |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | { |
51 | 51 | if (empty($dataType)) throw new \Exception(self::class.": No content to convert to Yaml"); |
52 | 52 | if (is_scalar($dataType)) { |
53 | - return "--- ".$this->handler->dumpScalar($dataType). self::LINEFEED ; |
|
53 | + return "--- ".$this->handler->dumpScalar($dataType).self::LINEFEED; |
|
54 | 54 | } |
55 | 55 | return $this->dump($dataType, 0); |
56 | 56 | } |
@@ -48,7 +48,9 @@ |
||
48 | 48 | */ |
49 | 49 | public function toString($dataType, int $options = null):string |
50 | 50 | { |
51 | - if (empty($dataType)) throw new \Exception(self::class.": No content to convert to Yaml"); |
|
51 | + if (empty($dataType)) { |
|
52 | + throw new \Exception(self::class.": No content to convert to Yaml"); |
|
53 | + } |
|
52 | 54 | if (is_scalar($dataType)) { |
53 | 55 | return "--- ".$this->handler->dumpScalar($dataType). self::LINEFEED ; |
54 | 56 | } |
@@ -100,35 +100,35 @@ |
||
100 | 100 | if (Regex::isDate($v)) return ($this->getRoot()->getYamlObject()->getOptions() & Loader::NO_OBJECT_FOR_DATE) && !$onlyScalar ? date_create($v) : $v; |
101 | 101 | if (Regex::isNumber($v)) return $this->getNumber($v); |
102 | 102 | $types = ['yes' => true, |
103 | - 'no' => false, |
|
104 | - 'true' => true, |
|
105 | - 'false' => false, |
|
106 | - 'null' => null, |
|
107 | - '.inf' => \INF, |
|
108 | - '-.inf' => -\INF, |
|
109 | - '.nan' => \NAN |
|
103 | + 'no' => false, |
|
104 | + 'true' => true, |
|
105 | + 'false' => false, |
|
106 | + 'null' => null, |
|
107 | + '.inf' => \INF, |
|
108 | + '-.inf' => -\INF, |
|
109 | + '.nan' => \NAN |
|
110 | 110 | ]; |
111 | 111 | return array_key_exists(strtolower($v), $types) ? $types[strtolower($v)] : $this->replaceSequences($v); |
112 | 112 | } |
113 | 113 | |
114 | 114 | public function replaceSequences($value='') |
115 | 115 | { |
116 | - $replaceUnicodeSeq = function ($matches) { |
|
116 | + $replaceUnicodeSeq = function ($matches) { |
|
117 | 117 | return json_decode('"'.$matches[1].'"'); |
118 | - }; |
|
119 | - $replaceNonPrintable = function ($matches) { |
|
118 | + }; |
|
119 | + $replaceNonPrintable = function ($matches) { |
|
120 | 120 | // var_dump($matches[1]); |
121 | 121 | return $matches[1].""; |
122 | - }; |
|
122 | + }; |
|
123 | 123 | // preg_match( "/[^\x{06F0}-\x{06F9}\x]+/u" , '۱۲۳۴۵۶۷۸۹۰' ); |
124 | - return preg_replace_callback_array( |
|
125 | - [ |
|
126 | - '/((?<![\\\\])\\\\x[0-9a-f]{2})/i' => $replaceUnicodeSeq, |
|
127 | - '/((?<![\\\\])\\\\u[0-9a-f]{4,})/i' => $replaceUnicodeSeq, |
|
128 | - '/(\\\\b|\\\\n|\\\\t|\\\\r)/' => $replaceNonPrintable |
|
129 | - ], |
|
130 | - $value |
|
131 | - ); |
|
124 | + return preg_replace_callback_array( |
|
125 | + [ |
|
126 | + '/((?<![\\\\])\\\\x[0-9a-f]{2})/i' => $replaceUnicodeSeq, |
|
127 | + '/((?<![\\\\])\\\\u[0-9a-f]{4,})/i' => $replaceUnicodeSeq, |
|
128 | + '/(\\\\b|\\\\n|\\\\t|\\\\r)/' => $replaceNonPrintable |
|
129 | + ], |
|
130 | + $value |
|
131 | + ); |
|
132 | 132 | } |
133 | 133 | |
134 | 134 |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | |
51 | 51 | public function getTargetOnLessIndent(NodeGeneric &$node):NodeGeneric |
52 | 52 | { |
53 | - if ($node instanceof Scalar || $node instanceof Blank ) { |
|
53 | + if ($node instanceof Scalar || $node instanceof Blank) { |
|
54 | 54 | return $this->getParent(); |
55 | 55 | } else { |
56 | 56 | return $this->getParent($node->indent); |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | \.nan | \.NaN | \.NAN tag:yaml.org,2002:float (Not a number) |
98 | 98 | * tag:yaml.org,2002:str (Default) |
99 | 99 | */ |
100 | - if (Regex::isDate($v)) return ($this->getRoot()->getYamlObject()->getOptions() & Loader::NO_OBJECT_FOR_DATE) && !$onlyScalar ? date_create($v) : $v; |
|
100 | + if (Regex::isDate($v)) return ($this->getRoot()->getYamlObject()->getOptions()&Loader::NO_OBJECT_FOR_DATE) && !$onlyScalar ? date_create($v) : $v; |
|
101 | 101 | if (Regex::isNumber($v)) return $this->getNumber($v); |
102 | 102 | $types = ['yes' => true, |
103 | 103 | 'no' => false, |
@@ -111,12 +111,12 @@ discard block |
||
111 | 111 | return array_key_exists(strtolower($v), $types) ? $types[strtolower($v)] : $this->replaceSequences($v); |
112 | 112 | } |
113 | 113 | |
114 | - public function replaceSequences($value='') |
|
114 | + public function replaceSequences($value = '') |
|
115 | 115 | { |
116 | - $replaceUnicodeSeq = function ($matches) { |
|
116 | + $replaceUnicodeSeq = function($matches) { |
|
117 | 117 | return json_decode('"'.$matches[1].'"'); |
118 | 118 | }; |
119 | - $replaceNonPrintable = function ($matches) { |
|
119 | + $replaceNonPrintable = function($matches) { |
|
120 | 120 | // var_dump($matches[1]); |
121 | 121 | return $matches[1].""; |
122 | 122 | }; |
@@ -97,8 +97,12 @@ discard block |
||
97 | 97 | \.nan | \.NaN | \.NAN tag:yaml.org,2002:float (Not a number) |
98 | 98 | * tag:yaml.org,2002:str (Default) |
99 | 99 | */ |
100 | - if (Regex::isDate($v)) return ($this->getRoot()->getYamlObject()->getOptions() & Loader::NO_OBJECT_FOR_DATE) && !$onlyScalar ? date_create($v) : $v; |
|
101 | - if (Regex::isNumber($v)) return $this->getNumber($v); |
|
100 | + if (Regex::isDate($v)) { |
|
101 | + return ($this->getRoot()->getYamlObject()->getOptions() & Loader::NO_OBJECT_FOR_DATE) && !$onlyScalar ? date_create($v) : $v; |
|
102 | + } |
|
103 | + if (Regex::isNumber($v)) { |
|
104 | + return $this->getNumber($v); |
|
105 | + } |
|
102 | 106 | $types = ['yes' => true, |
103 | 107 | 'no' => false, |
104 | 108 | 'true' => true, |
@@ -142,8 +146,12 @@ discard block |
||
142 | 146 | */ |
143 | 147 | private function getNumber(string $v) |
144 | 148 | { |
145 | - if ((bool) preg_match(Regex::OCTAL_NUM, $v)) return intval(base_convert($v, 8, 10)); |
|
146 | - if ((bool) preg_match(Regex::HEX_NUM, $v)) return intval(base_convert($v, 16, 10)); |
|
149 | + if ((bool) preg_match(Regex::OCTAL_NUM, $v)) { |
|
150 | + return intval(base_convert($v, 8, 10)); |
|
151 | + } |
|
152 | + if ((bool) preg_match(Regex::HEX_NUM, $v)) { |
|
153 | + return intval(base_convert($v, 16, 10)); |
|
154 | + } |
|
147 | 155 | return is_bool(strpos($v, '.')) || substr_count($v, '.') > 1 ? intval($v) : floatval($v); |
148 | 156 | } |
149 | 157 |
@@ -19,7 +19,7 @@ |
||
19 | 19 | // var_dump($yamlObject);exit(); |
20 | 20 | return $yamlObject->getReference($name); |
21 | 21 | } catch (\Throwable $e) { |
22 | - throw new \ParseError("Unknown anchor : '$name' this:".$this->anchor,1,$e); |
|
22 | + throw new \ParseError("Unknown anchor : '$name' this:".$this->anchor, 1, $e); |
|
23 | 23 | } |
24 | 24 | } else { |
25 | 25 | $built = is_null($this->value) ? null : $this->value->build($parent); |
@@ -28,8 +28,8 @@ discard block |
||
28 | 28 | elseif ((bool) preg_match(Regex::KEY, $trimmed, $matches)) return new Nodes\Key($nodeString, $line, $matches); |
29 | 29 | else { |
30 | 30 | $first = $trimmed[0]; |
31 | - $stringGroups = ['-', '>|' , '"\'', "#%" , "{[" , ":?" , '*&!']; |
|
32 | - $methodGroups = ['Hyphen','Literal','Quoted','Special','Compact','SetElement','NodeAction']; |
|
31 | + $stringGroups = ['-', '>|', '"\'', "#%", "{[", ":?", '*&!']; |
|
32 | + $methodGroups = ['Hyphen', 'Literal', 'Quoted', 'Special', 'Compact', 'SetElement', 'NodeAction']; |
|
33 | 33 | foreach ($stringGroups as $groupIndex => $stringRef) { |
34 | 34 | if (is_int(strpos($stringRef, $first))) { |
35 | 35 | $methodName = 'on'.$methodGroups[$groupIndex]; |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | ini_restore($backtrack_setting); |
116 | 116 | |
117 | 117 | if (is_bool($isMapping) || is_bool($isSequence)) { |
118 | - throw new \Exception("Regex Error for ".(is_bool($isMapping) ? 'mapping:' :'sequence:').preg_last_error()); |
|
118 | + throw new \Exception("Regex Error for ".(is_bool($isMapping) ? 'mapping:' : 'sequence:').preg_last_error()); |
|
119 | 119 | } |
120 | 120 | if ($isMapping) { |
121 | 121 | // var_dump(Regex::MAPPING, trim($nodeString)); |
@@ -23,10 +23,13 @@ discard block |
||
23 | 23 | final public static function get($nodeString = null, $line = 0):NodeGeneric |
24 | 24 | { |
25 | 25 | $trimmed = ltrim($nodeString); |
26 | - if ($trimmed === '') return new Nodes\Blank($nodeString, $line); |
|
27 | - elseif (substr($trimmed, 0, 3) === '...') return new Nodes\DocEnd($nodeString, $line); |
|
28 | - elseif ((bool) preg_match(Regex::KEY, $trimmed, $matches)) return new Nodes\Key($nodeString, $line, $matches); |
|
29 | - else { |
|
26 | + if ($trimmed === '') { |
|
27 | + return new Nodes\Blank($nodeString, $line); |
|
28 | + } elseif (substr($trimmed, 0, 3) === '...') { |
|
29 | + return new Nodes\DocEnd($nodeString, $line); |
|
30 | + } elseif ((bool) preg_match(Regex::KEY, $trimmed, $matches)) { |
|
31 | + return new Nodes\Key($nodeString, $line, $matches); |
|
32 | + } else { |
|
30 | 33 | $first = $trimmed[0]; |
31 | 34 | $stringGroups = ['-', '>|' , '"\'', "#%" , "{[" , ":?" , '*&!']; |
32 | 35 | $methodGroups = ['Hyphen','Literal','Quoted','Special','Compact','SetElement','NodeAction']; |
@@ -137,9 +140,11 @@ discard block |
||
137 | 140 | */ |
138 | 141 | final private static function onHyphen(string $first, string $nodeString, int $line):NodeGeneric |
139 | 142 | { |
140 | - if (substr($nodeString, 0, 3) === '---') return new Nodes\DocStart($nodeString, $line); |
|
141 | - elseif ((bool) preg_match(Regex::ITEM, ltrim($nodeString))) return new Nodes\Item($nodeString, $line); |
|
142 | - else { |
|
143 | + if (substr($nodeString, 0, 3) === '---') { |
|
144 | + return new Nodes\DocStart($nodeString, $line); |
|
145 | + } elseif ((bool) preg_match(Regex::ITEM, ltrim($nodeString))) { |
|
146 | + return new Nodes\Item($nodeString, $line); |
|
147 | + } else { |
|
143 | 148 | return new Nodes\Scalar($nodeString, $line); |
144 | 149 | } |
145 | 150 | } |
@@ -56,7 +56,7 @@ |
||
56 | 56 | $buffer->push($child); |
57 | 57 | } |
58 | 58 | } |
59 | - $documents[] = $this->buildDocument($buffer, count($documents) +1); |
|
59 | + $documents[] = $this->buildDocument($buffer, count($documents) + 1); |
|
60 | 60 | } catch (\Throwable $e) { |
61 | 61 | throw new \Exception($e->getMessage(), 1, $e); |
62 | 62 | } |
@@ -20,10 +20,10 @@ discard block |
||
20 | 20 | //public |
21 | 21 | /* @var null|string */ |
22 | 22 | public static $error; |
23 | - public const IGNORE_DIRECTIVES = 0b0001;//DONT include_directive |
|
24 | - public const IGNORE_COMMENTS = 0b0010;//DONT include_comments |
|
25 | - public const NO_PARSING_EXCEPTIONS = 0b0100;//DONT throw Exception on parsing errors |
|
26 | - public const NO_OBJECT_FOR_DATE = 0b1000;//DONT import date strings as dateTime Object |
|
23 | + public const IGNORE_DIRECTIVES = 0b0001; //DONT include_directive |
|
24 | + public const IGNORE_COMMENTS = 0b0010; //DONT include_comments |
|
25 | + public const NO_PARSING_EXCEPTIONS = 0b0100; //DONT throw Exception on parsing errors |
|
26 | + public const NO_OBJECT_FOR_DATE = 0b1000; //DONT import date strings as dateTime Object |
|
27 | 27 | |
28 | 28 | //private |
29 | 29 | /* @var null|array */ |
@@ -178,8 +178,8 @@ discard block |
||
178 | 178 | { |
179 | 179 | $deepest = $previous->getDeepestNode(); |
180 | 180 | if ($deepest instanceof Nodes\Partial) { |
181 | - return $deepest->specialProcess($current, $this->_blankBuffer); |
|
182 | - } elseif(!($current instanceof Nodes\Partial)) { |
|
181 | + return $deepest->specialProcess($current, $this->_blankBuffer); |
|
182 | + } elseif (!($current instanceof Nodes\Partial)) { |
|
183 | 183 | return $current->specialProcess($previous, $this->_blankBuffer); |
184 | 184 | } |
185 | 185 | return false; |
@@ -129,8 +129,12 @@ |
||
129 | 129 | try { |
130 | 130 | foreach ($generator as $lineNB => $lineString) { |
131 | 131 | $node = NodeFactory::get($lineString, $lineNB); |
132 | - if ($this->_debug === 1) echo $lineNB.":".get_class($node)."\n"; |
|
133 | - if ($this->needsSpecialProcess($node, $previous)) continue; |
|
132 | + if ($this->_debug === 1) { |
|
133 | + echo $lineNB.":".get_class($node)."\n"; |
|
134 | + } |
|
135 | + if ($this->needsSpecialProcess($node, $previous)) { |
|
136 | + continue; |
|
137 | + } |
|
134 | 138 | $this->_attachBlankLines($previous); |
135 | 139 | switch ($node->indent <=> $previous->indent) { |
136 | 140 | case -1: $target = $previous->getTargetOnLessIndent($node); |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | */ |
58 | 58 | public function str($node, &$parent = null) |
59 | 59 | { |
60 | - if($node instanceof Nodes\Literals){ |
|
60 | + if ($node instanceof Nodes\Literals) { |
|
61 | 61 | $node = $node->value; |
62 | 62 | } |
63 | 63 | if ($node instanceof Nodes\NodeGeneric) { |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | $list[] = $this->str($child); |
73 | 73 | } |
74 | 74 | // return new Nodes\Scalar(implode('',$list), 0); |
75 | - return implode('',$list); |
|
75 | + return implode('', $list); |
|
76 | 76 | } |
77 | 77 | } |
78 | 78 | |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | $keyValue = $key->value->build(); |
134 | 134 | if (is_null($parent)) { |
135 | 135 | return [$keyName => $keyValue]; |
136 | - } else{ |
|
136 | + } else { |
|
137 | 137 | $parent[$keyName] = $keyValue; |
138 | 138 | } |
139 | 139 | } else { |
@@ -6,18 +6,18 @@ |
||
6 | 6 | /** |
7 | 7 | * Display some use cases for Yaml library |
8 | 8 | */ |
9 | -const JSON_OPTIONS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_LINE_TERMINATORS | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_PARTIAL_OUTPUT_ON_ERROR; |
|
9 | +const JSON_OPTIONS = JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_LINE_TERMINATORS|JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION|JSON_PARTIAL_OUTPUT_ON_ERROR; |
|
10 | 10 | |
11 | 11 | $debug = (int) (isset($argv[1]) ? $argv[1] : null); |
12 | 12 | |
13 | -echo memory_get_usage() . "\n"; |
|
13 | +echo memory_get_usage()."\n"; |
|
14 | 14 | /* USE CASE 1 |
15 | 15 | * load and parse if file exists |
16 | 16 | */ |
17 | -$content = file_get_contents('./examples/dummy.yml');//var_dump($content); |
|
17 | +$content = file_get_contents('./examples/dummy.yml'); //var_dump($content); |
|
18 | 18 | $yaml = Yaml::parse($content, 0, $debug); |
19 | 19 | |
20 | -echo memory_get_usage() . "\n"; |
|
20 | +echo memory_get_usage()."\n"; |
|
21 | 21 | // var_dump($yaml); |
22 | 22 | var_dump(json_encode($yaml, JSON_OPTIONS)); |
23 | 23 | exit(0); |