@@ -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]; |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | } |
| 146 | 146 | // $action = trim($matches['action']);//var_dump($matches); |
| 147 | 147 | switch ($first) { |
| 148 | - case '!': return new Nodes\Tag ($nodeString, $line); |
|
| 148 | + case '!': return new Nodes\Tag($nodeString, $line); |
|
| 149 | 149 | default : |
| 150 | 150 | return new Nodes\Anchor($nodeString, $line); |
| 151 | 151 | // case '&': return new NodeAnchor($nodeString, $line); |
@@ -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']; |
@@ -105,10 +108,13 @@ discard block |
||
| 105 | 108 | final private static function onCompact(string $first, string $nodeString, int $line):NodeGeneric |
| 106 | 109 | { |
| 107 | 110 | json_decode($nodeString, false, 512, self::JSON_OPTIONS); |
| 108 | - if (json_last_error() === \JSON_ERROR_NONE) return new Nodes\JSON($nodeString, $line); |
|
| 109 | - elseif ((bool) preg_match(Regex::MAPPING, trim($nodeString))) return new Nodes\CompactMapping($nodeString, $line); |
|
| 110 | - elseif ((bool) preg_match(Regex::SEQUENCE, trim($nodeString))) return new Nodes\CompactSequence($nodeString, $line); |
|
| 111 | - else { |
|
| 111 | + if (json_last_error() === \JSON_ERROR_NONE) { |
|
| 112 | + return new Nodes\JSON($nodeString, $line); |
|
| 113 | + } elseif ((bool) preg_match(Regex::MAPPING, trim($nodeString))) { |
|
| 114 | + return new Nodes\CompactMapping($nodeString, $line); |
|
| 115 | + } elseif ((bool) preg_match(Regex::SEQUENCE, trim($nodeString))) { |
|
| 116 | + return new Nodes\CompactSequence($nodeString, $line); |
|
| 117 | + } else { |
|
| 112 | 118 | return new Nodes\Partial($nodeString, $line); |
| 113 | 119 | } |
| 114 | 120 | } |
@@ -123,9 +129,11 @@ discard block |
||
| 123 | 129 | */ |
| 124 | 130 | final private static function onHyphen(string $first, string $nodeString, int $line):NodeGeneric |
| 125 | 131 | { |
| 126 | - if (substr($nodeString, 0, 3) === '---') return new Nodes\DocStart($nodeString, $line); |
|
| 127 | - elseif ((bool) preg_match(Regex::ITEM, ltrim($nodeString))) return new Nodes\Item($nodeString, $line); |
|
| 128 | - else { |
|
| 132 | + if (substr($nodeString, 0, 3) === '---') { |
|
| 133 | + return new Nodes\DocStart($nodeString, $line); |
|
| 134 | + } elseif ((bool) preg_match(Regex::ITEM, ltrim($nodeString))) { |
|
| 135 | + return new Nodes\Item($nodeString, $line); |
|
| 136 | + } else { |
|
| 129 | 137 | return new Nodes\Scalar($nodeString, $line); |
| 130 | 138 | } |
| 131 | 139 | } |
@@ -130,8 +130,12 @@ |
||
| 130 | 130 | try { |
| 131 | 131 | foreach ($generator as $lineNB => $lineString) { |
| 132 | 132 | $node = NodeFactory::get($lineString, $lineNB); |
| 133 | - if ($this->_debug === 1) echo get_class($node)."\n"; |
|
| 134 | - if ($this->needsSpecialProcess($node, $previous)) continue; |
|
| 133 | + if ($this->_debug === 1) { |
|
| 134 | + echo get_class($node)."\n"; |
|
| 135 | + } |
|
| 136 | + if ($this->needsSpecialProcess($node, $previous)) { |
|
| 137 | + continue; |
|
| 138 | + } |
|
| 135 | 139 | $this->_attachBlankLines($previous); |
| 136 | 140 | switch ($node->indent <=> $previous->indent) { |
| 137 | 141 | case -1: $target = $previous->getTargetOnLessIndent($node); |
@@ -20,10 +20,10 @@ discard block |
||
| 20 | 20 | //public |
| 21 | 21 | /* @var null|string */ |
| 22 | 22 | public static $error; |
| 23 | - public const IGNORE_DIRECTIVES = 1;//DONT include_directive |
|
| 24 | - public const IGNORE_COMMENTS = 2;//DONT include_comments |
|
| 25 | - public const NO_PARSING_EXCEPTIONS = 4;//DONT throw Exception on parsing errors |
|
| 26 | - public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
|
| 23 | + public const IGNORE_DIRECTIVES = 1; //DONT include_directive |
|
| 24 | + public const IGNORE_COMMENTS = 2; //DONT include_comments |
|
| 25 | + public const NO_PARSING_EXCEPTIONS = 4; //DONT throw Exception on parsing errors |
|
| 26 | + public const NO_OBJECT_FOR_DATE = 8; //DONT import date strings as dateTime Object |
|
| 27 | 27 | |
| 28 | 28 | //private |
| 29 | 29 | /* @var null|array */ |
@@ -179,8 +179,8 @@ discard block |
||
| 179 | 179 | { |
| 180 | 180 | $deepest = $previous->getDeepestNode(); |
| 181 | 181 | if ($deepest instanceof Nodes\Partial) { |
| 182 | - return $deepest->specialProcess($current, $this->_blankBuffer); |
|
| 183 | - } elseif(!($current instanceof Nodes\Partial)) { |
|
| 182 | + return $deepest->specialProcess($current, $this->_blankBuffer); |
|
| 183 | + } elseif (!($current instanceof Nodes\Partial)) { |
|
| 184 | 184 | return $current->specialProcess($previous, $this->_blankBuffer); |
| 185 | 185 | } |
| 186 | 186 | return false; |
@@ -12,6 +12,6 @@ |
||
| 12 | 12 | { |
| 13 | 13 | public function build(&$parent = null) |
| 14 | 14 | { |
| 15 | - return substr(Scalar::replaceSequences(trim($this->raw)), 1,-1); |
|
| 15 | + return substr(Scalar::replaceSequences(trim($this->raw)), 1, -1); |
|
| 16 | 16 | } |
| 17 | 17 | } |
| 18 | 18 | \ No newline at end of file |
@@ -96,8 +96,12 @@ discard block |
||
| 96 | 96 | \.nan | \.NaN | \.NAN tag:yaml.org,2002:float (Not a number) |
| 97 | 97 | * tag:yaml.org,2002:str (Default) |
| 98 | 98 | */ |
| 99 | - if (Regex::isDate($v)) return Builder::$dateAsObject && !$onlyScalar ? date_create($v) : $v; |
|
| 100 | - if (Regex::isNumber($v)) return self::getNumber($v); |
|
| 99 | + if (Regex::isDate($v)) { |
|
| 100 | + return Builder::$dateAsObject && !$onlyScalar ? date_create($v) : $v; |
|
| 101 | + } |
|
| 102 | + if (Regex::isNumber($v)) { |
|
| 103 | + return self::getNumber($v); |
|
| 104 | + } |
|
| 101 | 105 | $types = ['yes' => true, |
| 102 | 106 | 'no' => false, |
| 103 | 107 | 'true' => true, |
@@ -141,8 +145,12 @@ discard block |
||
| 141 | 145 | */ |
| 142 | 146 | private static function getNumber(string $v) |
| 143 | 147 | { |
| 144 | - if ((bool) preg_match(Regex::OCTAL_NUM, $v)) return intval(base_convert($v, 8, 10)); |
|
| 145 | - if ((bool) preg_match(Regex::HEX_NUM, $v)) return intval(base_convert($v, 16, 10)); |
|
| 148 | + if ((bool) preg_match(Regex::OCTAL_NUM, $v)) { |
|
| 149 | + return intval(base_convert($v, 8, 10)); |
|
| 150 | + } |
|
| 151 | + if ((bool) preg_match(Regex::HEX_NUM, $v)) { |
|
| 152 | + return intval(base_convert($v, 16, 10)); |
|
| 153 | + } |
|
| 146 | 154 | return is_bool(strpos($v, '.')) || substr_count($v, '.') > 1 ? intval($v) : floatval($v); |
| 147 | 155 | } |
| 148 | 156 | |
@@ -99,35 +99,35 @@ |
||
| 99 | 99 | if (Regex::isDate($v)) return Builder::$dateAsObject && !$onlyScalar ? date_create($v) : $v; |
| 100 | 100 | if (Regex::isNumber($v)) return self::getNumber($v); |
| 101 | 101 | $types = ['yes' => true, |
| 102 | - 'no' => false, |
|
| 103 | - 'true' => true, |
|
| 104 | - 'false' => false, |
|
| 105 | - 'null' => null, |
|
| 106 | - '.inf' => \INF, |
|
| 107 | - '-.inf' => -\INF, |
|
| 108 | - '.nan' => \NAN |
|
| 102 | + 'no' => false, |
|
| 103 | + 'true' => true, |
|
| 104 | + 'false' => false, |
|
| 105 | + 'null' => null, |
|
| 106 | + '.inf' => \INF, |
|
| 107 | + '-.inf' => -\INF, |
|
| 108 | + '.nan' => \NAN |
|
| 109 | 109 | ]; |
| 110 | 110 | return array_key_exists(strtolower($v), $types) ? $types[strtolower($v)] : self::replaceSequences($v); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | public static function replaceSequences($value='') |
| 114 | 114 | { |
| 115 | - $replaceUnicodeSeq = function ($matches) { |
|
| 115 | + $replaceUnicodeSeq = function ($matches) { |
|
| 116 | 116 | return json_decode('"'.$matches[1].'"'); |
| 117 | - }; |
|
| 118 | - $replaceNonPrintable = function ($matches) { |
|
| 117 | + }; |
|
| 118 | + $replaceNonPrintable = function ($matches) { |
|
| 119 | 119 | // var_dump($matches[1]); |
| 120 | 120 | return $matches[1].""; |
| 121 | - }; |
|
| 121 | + }; |
|
| 122 | 122 | // preg_match( "/[^\x{06F0}-\x{06F9}\x]+/u" , '۱۲۳۴۵۶۷۸۹۰' ); |
| 123 | - return preg_replace_callback_array( |
|
| 124 | - [ |
|
| 125 | - '/((?<![\\\\])\\\\x[0-9a-f]{2})/i' => $replaceUnicodeSeq, |
|
| 126 | - '/((?<![\\\\])\\\\u[0-9a-f]{4,})/i' => $replaceUnicodeSeq, |
|
| 127 | - '/(\\\\b|\\\\n|\\\\t|\\\\r)/' => $replaceNonPrintable |
|
| 128 | - ], |
|
| 129 | - $value |
|
| 130 | - ); |
|
| 123 | + return preg_replace_callback_array( |
|
| 124 | + [ |
|
| 125 | + '/((?<![\\\\])\\\\x[0-9a-f]{2})/i' => $replaceUnicodeSeq, |
|
| 126 | + '/((?<![\\\\])\\\\u[0-9a-f]{4,})/i' => $replaceUnicodeSeq, |
|
| 127 | + '/(\\\\b|\\\\n|\\\\t|\\\\r)/' => $replaceNonPrintable |
|
| 128 | + ], |
|
| 129 | + $value |
|
| 130 | + ); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | |
| 50 | 50 | public function getTargetOnLessIndent(NodeGeneric &$node):NodeGeneric |
| 51 | 51 | { |
| 52 | - if ($node instanceof Scalar || $node instanceof Blank ) { |
|
| 52 | + if ($node instanceof Scalar || $node instanceof Blank) { |
|
| 53 | 53 | return $this->getParent(); |
| 54 | 54 | } else { |
| 55 | 55 | return $this->getParent($node->indent); |
@@ -110,12 +110,12 @@ discard block |
||
| 110 | 110 | return array_key_exists(strtolower($v), $types) ? $types[strtolower($v)] : self::replaceSequences($v); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - public static function replaceSequences($value='') |
|
| 113 | + public static function replaceSequences($value = '') |
|
| 114 | 114 | { |
| 115 | - $replaceUnicodeSeq = function ($matches) { |
|
| 115 | + $replaceUnicodeSeq = function($matches) { |
|
| 116 | 116 | return json_decode('"'.$matches[1].'"'); |
| 117 | 117 | }; |
| 118 | - $replaceNonPrintable = function ($matches) { |
|
| 118 | + $replaceNonPrintable = function($matches) { |
|
| 119 | 119 | // var_dump($matches[1]); |
| 120 | 120 | return $matches[1].""; |
| 121 | 121 | }; |
@@ -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('./tests/cases/examples/Example_2_25.yml');//var_dump($content); |
|
| 17 | +$content = file_get_contents('./tests/cases/examples/Example_2_25.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); |
@@ -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'; |
@@ -51,7 +51,7 @@ |
||
| 51 | 51 | $buffer->push($child); |
| 52 | 52 | } |
| 53 | 53 | } |
| 54 | - $documents[] = self::buildDocument($buffer, count($documents) +1); |
|
| 54 | + $documents[] = self::buildDocument($buffer, count($documents) + 1); |
|
| 55 | 55 | } catch (\Throwable $e) { |
| 56 | 56 | throw new \Exception($e->getMessage(), 1, $e); |
| 57 | 57 | } |
@@ -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 | } |