@@ -21,8 +21,8 @@ discard block |
||
21 | 21 | elseif ((bool) preg_match(Regex::KEY, $trimmed, $matches)) return new NodeKey($nodeString, $line, $matches); |
22 | 22 | else { |
23 | 23 | $first = $trimmed[0]; |
24 | - $stringGroups = ["-" ,'>|' ,'"\'',"#%" ,"{[" ,":?" ,'*&!']; |
|
25 | - $methodGroups = ['Hyphen','Literal','Quoted','Special','Compact','SetElement','NodeAction']; |
|
24 | + $stringGroups = ["-", '>|', '"\'', "#%", "{[", ":?", '*&!']; |
|
25 | + $methodGroups = ['Hyphen', 'Literal', 'Quoted', 'Special', 'Compact', 'SetElement', 'NodeAction']; |
|
26 | 26 | foreach ($stringGroups as $groupIndex => $stringRef) { |
27 | 27 | if (is_int(strpos($stringRef, $first))) { |
28 | 28 | $methodName = 'on'.$methodGroups[$groupIndex]; |
@@ -127,9 +127,9 @@ discard block |
||
127 | 127 | if (!((bool) preg_match(Regex::NODE_ACTIONS, trim($nodeString), $matches))) { |
128 | 128 | return new NodeScalar($nodeString, $line); |
129 | 129 | } |
130 | - $action = trim($matches['action']);//var_dump($matches); |
|
130 | + $action = trim($matches['action']); //var_dump($matches); |
|
131 | 131 | switch ($action[0]) { |
132 | - case '!': return new NodeTag ($nodeString, $line); |
|
132 | + case '!': return new NodeTag($nodeString, $line); |
|
133 | 133 | case '&': return new NodeAnchor($nodeString, $line); |
134 | 134 | case '*': return new NodeAnchor($nodeString, $line); |
135 | 135 | // default: |
@@ -16,10 +16,13 @@ discard block |
||
16 | 16 | final public static function get($nodeString = null, $line = 0):Node |
17 | 17 | { |
18 | 18 | $trimmed = ltrim($nodeString); |
19 | - if ($trimmed === '') return new NodeBlank($nodeString, $line); |
|
20 | - elseif (substr($trimmed, 0, 3) === '...') return new NodeDocEnd($nodeString, $line); |
|
21 | - elseif ((bool) preg_match(Regex::KEY, $trimmed, $matches)) return new NodeKey($nodeString, $line, $matches); |
|
22 | - else { |
|
19 | + if ($trimmed === '') { |
|
20 | + return new NodeBlank($nodeString, $line); |
|
21 | + } elseif (substr($trimmed, 0, 3) === '...') { |
|
22 | + return new NodeDocEnd($nodeString, $line); |
|
23 | + } elseif ((bool) preg_match(Regex::KEY, $trimmed, $matches)) { |
|
24 | + return new NodeKey($nodeString, $line, $matches); |
|
25 | + } else { |
|
23 | 26 | $first = $trimmed[0]; |
24 | 27 | $stringGroups = ["-" ,'>|' ,'"\'',"#%" ,"{[" ,":?" ,'*&!']; |
25 | 28 | $methodGroups = ['Hyphen','Literal','Quoted','Special','Compact','SetElement','NodeAction']; |
@@ -90,10 +93,13 @@ discard block |
||
90 | 93 | final private static function onCompact(string $first, string $nodeString, int $line):Node |
91 | 94 | { |
92 | 95 | json_decode($nodeString, false, 512, self::JSON_OPTIONS); |
93 | - if (json_last_error() === \JSON_ERROR_NONE) return new NodeJSON($nodeString, $line); |
|
94 | - elseif ((bool) preg_match(Regex::MAPPING, trim($nodeString))) return new NodeCompactMapping($nodeString, $line); |
|
95 | - elseif ((bool) preg_match(Regex::SEQUENCE, trim($nodeString))) return new NodeCompactSequence($nodeString, $line); |
|
96 | - else { |
|
96 | + if (json_last_error() === \JSON_ERROR_NONE) { |
|
97 | + return new NodeJSON($nodeString, $line); |
|
98 | + } elseif ((bool) preg_match(Regex::MAPPING, trim($nodeString))) { |
|
99 | + return new NodeCompactMapping($nodeString, $line); |
|
100 | + } elseif ((bool) preg_match(Regex::SEQUENCE, trim($nodeString))) { |
|
101 | + return new NodeCompactSequence($nodeString, $line); |
|
102 | + } else { |
|
97 | 103 | return new NodePartial($nodeString, $line); |
98 | 104 | } |
99 | 105 | } |
@@ -108,9 +114,11 @@ discard block |
||
108 | 114 | */ |
109 | 115 | final private static function onHyphen(string $first, string $nodeString, int $line):Node |
110 | 116 | { |
111 | - if (substr($nodeString, 0, 3) === '---') return new NodeDocStart($nodeString, $line); |
|
112 | - elseif ((bool) preg_match(Regex::ITEM, ltrim($nodeString))) return new NodeItem($nodeString, $line); |
|
113 | - else { |
|
117 | + if (substr($nodeString, 0, 3) === '---') { |
|
118 | + return new NodeDocStart($nodeString, $line); |
|
119 | + } elseif ((bool) preg_match(Regex::ITEM, ltrim($nodeString))) { |
|
120 | + return new NodeItem($nodeString, $line); |
|
121 | + } else { |
|
114 | 122 | return new NodeScalar($nodeString, $line); |
115 | 123 | } |
116 | 124 | } |
@@ -23,8 +23,8 @@ discard block |
||
23 | 23 | |
24 | 24 | public static $tagsNamespaces = []; |
25 | 25 | /** |
26 | - * The primary tag handle is a single “!” character. |
|
27 | - * # Global |
|
26 | + * The primary tag handle is a single “!” character. |
|
27 | + * # Global |
|
28 | 28 | %TAG ! tag:example.com,2000:app/ |
29 | 29 | --- |
30 | 30 | !foo "bar" |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | %TAG !e! tag:example.com,2000:app/ |
44 | 44 | --- |
45 | 45 | !e!foo "bar" |
46 | - */ |
|
46 | + */ |
|
47 | 47 | /** |
48 | 48 | * Add Handlers for legacy Yaml tags |
49 | 49 | * |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | { |
55 | 55 | $reflectAPI = new \ReflectionClass(self::class); |
56 | 56 | $methodsList = []; |
57 | - $list = $reflectAPI->getMethods(RM::IS_FINAL | RM::IS_STATIC & RM::IS_PRIVATE); |
|
57 | + $list = $reflectAPI->getMethods(RM::IS_FINAL|RM::IS_STATIC & RM::IS_PRIVATE); |
|
58 | 58 | foreach ($list as $method) { |
59 | 59 | $methodsList[$method->name] = $method->getClosure(); |
60 | 60 | } |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | public static function transform(string $identifier, $value) |
78 | 78 | { |
79 | 79 | if (self::isKnown($identifier)) { |
80 | - if (!($value instanceof Node) && !($value instanceof NodeList) ) { |
|
80 | + if (!($value instanceof Node) && !($value instanceof NodeList)) { |
|
81 | 81 | throw new \Exception(sprintf(self::WRONG_VALUE, $identifier, gettype($value))); |
82 | 82 | } |
83 | 83 | return self::$registeredHandlers[$identifier]($value); |
@@ -18,12 +18,12 @@ |
||
18 | 18 | class TagNamespaceLocal implements TagNamespaceInterface |
19 | 19 | { |
20 | 20 | public const LEGACY_TAGS_HANDLERS = ['!!str' => 'strHandler', |
21 | - '!!binary' => 'binaryHandler', |
|
22 | - '!set' => 'setHandler', |
|
23 | - '!!omap' => 'omapHandler', |
|
24 | - '!php/object' => 'symfonyPHPobjectHandler', |
|
25 | - '!inline' => 'inlineHandler', |
|
26 | - ]; |
|
21 | + '!!binary' => 'binaryHandler', |
|
22 | + '!set' => 'setHandler', |
|
23 | + '!!omap' => 'omapHandler', |
|
24 | + '!php/object' => 'symfonyPHPobjectHandler', |
|
25 | + '!inline' => 'inlineHandler', |
|
26 | + ]; |
|
27 | 27 | |
28 | 28 | |
29 | 29 | /** |
@@ -88,7 +88,7 @@ |
||
88 | 88 | // $list[] = self::strHandler($child)->raw; |
89 | 89 | $list[] = self::strHandler($child); |
90 | 90 | } |
91 | - return new NodeScalar(implode('',$list), 0); |
|
91 | + return new NodeScalar(implode('', $list), 0); |
|
92 | 92 | } |
93 | 93 | } |
94 | 94 |
@@ -18,10 +18,10 @@ discard block |
||
18 | 18 | //public |
19 | 19 | /* @var null|string */ |
20 | 20 | public static $error; |
21 | - public const IGNORE_DIRECTIVES = 1;//DONT include_directive |
|
22 | - public const IGNORE_COMMENTS = 2;//DONT include_comments |
|
23 | - public const NO_PARSING_EXCEPTIONS = 4;//DONT throw Exception on parsing errors |
|
24 | - public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
|
21 | + public const IGNORE_DIRECTIVES = 1; //DONT include_directive |
|
22 | + public const IGNORE_COMMENTS = 2; //DONT include_comments |
|
23 | + public const NO_PARSING_EXCEPTIONS = 4; //DONT throw Exception on parsing errors |
|
24 | + public const NO_OBJECT_FOR_DATE = 8; //DONT import date strings as dateTime Object |
|
25 | 25 | |
26 | 26 | //private |
27 | 27 | /* @var null|array */ |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | $previous = $target->add($node); |
143 | 143 | } |
144 | 144 | $this->_attachBlankLines($previous); |
145 | - if ($this->_debug === 1){ |
|
145 | + if ($this->_debug === 1) { |
|
146 | 146 | return; |
147 | 147 | } |
148 | 148 | return Builder::buildContent($root, $this->_debug); |
@@ -183,8 +183,8 @@ discard block |
||
183 | 183 | { |
184 | 184 | $deepest = $previous->getDeepestNode(); |
185 | 185 | if ($deepest instanceof NodePartial) { |
186 | - return $deepest->specialProcess($current, $this->_blankBuffer); |
|
187 | - } elseif(!($current instanceof NodePartial)) { |
|
186 | + return $deepest->specialProcess($current, $this->_blankBuffer); |
|
187 | + } elseif (!($current instanceof NodePartial)) { |
|
188 | 188 | return $current->specialProcess($previous, $this->_blankBuffer); |
189 | 189 | } |
190 | 190 | 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 get_class($node)."\n"; |
|
133 | - if ($this->needsSpecialProcess($node, $previous)) continue; |
|
132 | + if ($this->_debug === 1) { |
|
133 | + echo 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); |
@@ -88,8 +88,8 @@ |
||
88 | 88 | private static function dumpYamlObject(YamlObject $obj) |
89 | 89 | { |
90 | 90 | if ($this->multipleDocs || $obj->hasDocStart() || $obj->isTagged() || $obj->isScalar()) { |
91 | - $this->multipleDocs = true; |
|
92 | - // && self::$result instanceof DLL) self::$result->push("---"); |
|
91 | + $this->multipleDocs = true; |
|
92 | + // && self::$result instanceof DLL) self::$result->push("---"); |
|
93 | 93 | } |
94 | 94 | if (count($obj) > 0) { |
95 | 95 | self::dumpArray($obj->getArrayCopy(), 0); |
@@ -40,9 +40,9 @@ discard block |
||
40 | 40 | */ |
41 | 41 | public static function dump($dataType, int $indent):string |
42 | 42 | { |
43 | - if(is_null($dataType)) { |
|
43 | + if (is_null($dataType)) { |
|
44 | 44 | return ''; |
45 | - } elseif(is_resource($dataType)) { |
|
45 | + } elseif (is_resource($dataType)) { |
|
46 | 46 | return get_resource_type($dataType); |
47 | 47 | } elseif (is_scalar($dataType)) { |
48 | 48 | return self::dumpScalar($dataType, $indent); |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | foreach ($array as $key => $item) { |
127 | 127 | $lineStart = current($refKeys) === $key ? "- " : "- $key: "; |
128 | 128 | if (is_scalar($item)) { |
129 | - self::add($lineStart.self::dump($item,0), $indent); |
|
129 | + self::add($lineStart.self::dump($item, 0), $indent); |
|
130 | 130 | } else { |
131 | 131 | self::add(rtrim($lineStart), $indent); |
132 | 132 | self::dump($item, $indent + self::INDENT); |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | if (is_array($subject) || $subject instanceof \ArrayIterator) { |
184 | 184 | $max = count($subject); |
185 | 185 | $objectAsArray = is_array($subject) ? $subject : $subject->getArrayCopy(); |
186 | - if(array_keys($objectAsArray) !== range(0, $max-1)) { |
|
186 | + if (array_keys($objectAsArray) !== range(0, $max - 1)) { |
|
187 | 187 | $pairs = $objectAsArray; |
188 | 188 | } else { |
189 | 189 | $valuesList = []; |
@@ -53,8 +53,12 @@ discard block |
||
53 | 53 | |
54 | 54 | public function dumpScalar($dataType, $indent) |
55 | 55 | { |
56 | - if ($dataType === \INF) return '.inf'; |
|
57 | - if ($dataType === -\INF) return '-.inf'; |
|
56 | + if ($dataType === \INF) { |
|
57 | + return '.inf'; |
|
58 | + } |
|
59 | + if ($dataType === -\INF) { |
|
60 | + return '-.inf'; |
|
61 | + } |
|
58 | 62 | switch (gettype($dataType)) { |
59 | 63 | case 'boolean': return $dataType ? 'true' : 'false'; |
60 | 64 | case 'float': //fall through |
@@ -75,8 +79,9 @@ discard block |
||
75 | 79 | } elseif (is_object($dataType)) { |
76 | 80 | if ($obj instanceof Tag) { |
77 | 81 | if ($obj instanceof Compact |
78 | - if ($obj instanceof \DateTime) |
|
79 | - return self::dumpArray($dataType, $indent); |
|
82 | + if ($obj instanceof \DateTime) { |
|
83 | + return self::dumpArray($dataType, $indent); |
|
84 | + } |
|
80 | 85 | $iterator = new ArrayIterator(); |
81 | 86 | } |
82 | 87 | } |
@@ -152,9 +157,13 @@ discard block |
||
152 | 157 | self::add(self::dump($obj->value, $indent + self::INDENT), $indent + self::INDENT); |
153 | 158 | } |
154 | 159 | } |
155 | - if ($obj instanceof Compact) return self::dumpCompact($obj, $indent); |
|
160 | + if ($obj instanceof Compact) { |
|
161 | + return self::dumpCompact($obj, $indent); |
|
162 | + } |
|
156 | 163 | //TODO: consider dumping datetime as date strings according to a format provided by user or default |
157 | - if ($obj instanceof \DateTime) return $obj->format(self::DATE_FORMAT); |
|
164 | + if ($obj instanceof \DateTime) { |
|
165 | + return $obj->format(self::DATE_FORMAT); |
|
166 | + } |
|
158 | 167 | $propList = get_object_vars($obj); |
159 | 168 | foreach ($propList as $property => $value) { |
160 | 169 | if (is_scalar($value) || $value instanceof Compact || $value instanceof \DateTime) { |
@@ -47,7 +47,7 @@ |
||
47 | 47 | self::$options = is_int($options) ? $options : self::OPTIONS; |
48 | 48 | if (is_scalar($dataType)) { |
49 | 49 | // TODO: what to woth comments ??? |
50 | - return "--- ".DumperHandlers::dumpScalar($dataType, 0). self::LINEFEED ; |
|
50 | + return "--- ".DumperHandlers::dumpScalar($dataType, 0).self::LINEFEED; |
|
51 | 51 | } |
52 | 52 | return DumperHandlers::dump($dataType, $options); |
53 | 53 | } |
@@ -43,7 +43,9 @@ discard block |
||
43 | 43 | */ |
44 | 44 | public static function toString($dataType, int $options = null):string |
45 | 45 | { |
46 | - if (empty($dataType)) throw new \Exception(self::class.": No content to convert to Yaml"); |
|
46 | + if (empty($dataType)) { |
|
47 | + throw new \Exception(self::class.": No content to convert to Yaml"); |
|
48 | + } |
|
47 | 49 | self::$options = is_int($options) ? $options : self::OPTIONS; |
48 | 50 | if (is_scalar($dataType)) { |
49 | 51 | // TODO: what to woth comments ??? |
@@ -79,8 +81,12 @@ discard block |
||
79 | 81 | private static function dump($dataType, int $indent) |
80 | 82 | { |
81 | 83 | if (is_scalar($dataType)) { |
82 | - if ($dataType === \INF) return '.inf'; |
|
83 | - if ($dataType === -\INF) return '-.inf'; |
|
84 | + if ($dataType === \INF) { |
|
85 | + return '.inf'; |
|
86 | + } |
|
87 | + if ($dataType === -\INF) { |
|
88 | + return '-.inf'; |
|
89 | + } |
|
84 | 90 | switch (gettype($dataType)) { |
85 | 91 | case 'boolean': return $dataType ? 'true' : 'false'; |
86 | 92 | case 'float': //fall through |
@@ -102,7 +108,9 @@ discard block |
||
102 | 108 | */ |
103 | 109 | private static function dumpYamlObject(YamlObject $obj) |
104 | 110 | { |
105 | - if ($obj->hasDocStart() && self::$result instanceof DLL) self::$result->push("---"); |
|
111 | + if ($obj->hasDocStart() && self::$result instanceof DLL) { |
|
112 | + self::$result->push("---"); |
|
113 | + } |
|
106 | 114 | // self::dump($obj, 0); |
107 | 115 | if (count($obj) > 0) { |
108 | 116 | self::dumpArray($obj->getArrayCopy(), 0); |
@@ -1,7 +1,7 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Dallgoot\Yaml; |
3 | 3 | |
4 | -require_once __DIR__ . '/dependencies/autoload.php'; |
|
4 | +require_once __DIR__.'/dependencies/autoload.php'; |
|
5 | 5 | |
6 | 6 | use Dallgoot\Yaml\{Loader, Dumper}; |
7 | 7 |
@@ -21,4 +21,6 @@ |
||
21 | 21 | if ($nameResultPair[$testName] !== $text) { |
22 | 22 | var_dump('EXPECTED', $nameResultPair[$testName]); |
23 | 23 | var_dump('RECEIVED', $text); |
24 | -} else echo 'OK'; |
|
24 | +} else { |
|
25 | + echo 'OK'; |
|
26 | +} |
@@ -6,7 +6,7 @@ discard block |
||
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 | |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | * load and parse if file exists |
15 | 15 | */ |
16 | 16 | // $content = file_get_contents('./tests/cases/parsing/multidoc_mapping.yml');//var_dump($content); |
17 | -$content = file_get_contents('./tests/definitions/examples_tests.yml');//var_dump($content); |
|
17 | +$content = file_get_contents('./tests/definitions/examples_tests.yml'); //var_dump($content); |
|
18 | 18 | $yaml = Yaml::parse($content, 0, $debug); |
19 | 19 | // var_dump($yaml); |
20 | 20 | var_dump(json_encode($yaml, JSON_OPTIONS)); |