@@ -32,7 +32,7 @@ |
||
| 32 | 32 | public function __call($funcName, $arguments) |
| 33 | 33 | { |
| 34 | 34 | $reflectAPI = new \ReflectionClass(get_class($this->__yaml__object__api)); |
| 35 | - $getName = function ($o) { return $o->name; }; |
|
| 35 | + $getName = function($o) { return $o->name; }; |
|
| 36 | 36 | $publicApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PUBLIC)); |
| 37 | 37 | $privateApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PRIVATE)); |
| 38 | 38 | if (!in_array($funcName, $publicApi) && !in_array($funcName, $privateApi)) { |
@@ -52,8 +52,12 @@ |
||
| 52 | 52 | { |
| 53 | 53 | $prop = get_object_vars($this); |
| 54 | 54 | unset($prop["__yaml__object__api"]); |
| 55 | - if (count($prop) > 0) return $prop; |
|
| 56 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 55 | + if (count($prop) > 0) { |
|
| 56 | + return $prop; |
|
| 57 | + } |
|
| 58 | + if (count($this) > 0) { |
|
| 59 | + return iterator_to_array($this); |
|
| 60 | + } |
|
| 57 | 61 | return $this->__yaml__object__api->value ?? "Empty YamlObject"; |
| 58 | 62 | } |
| 59 | 63 | } |
@@ -74,7 +74,7 @@ |
||
| 74 | 74 | switch (gettype($dataType)) { |
| 75 | 75 | case 'boolean': return $dataType ? 'true' : 'false'; |
| 76 | 76 | case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
| 77 | - return sprintf('%.2F', $dataType); |
|
| 77 | + return sprintf('%.2F', $dataType); |
|
| 78 | 78 | case 'double': if (is_nan((float) $dataType)) return '.nan'; |
| 79 | 79 | default: |
| 80 | 80 | return $dataType; |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | foreach ($array as $key => $item) { |
| 117 | 117 | $lineStart = current($refKeys) === $key ? "- " : "- $key: "; |
| 118 | 118 | if (is_scalar($item)) { |
| 119 | - self::add($lineStart.self::dump($item,0), $indent); |
|
| 119 | + self::add($lineStart.self::dump($item, 0), $indent); |
|
| 120 | 120 | } else { |
| 121 | 121 | self::add($lineStart, $indent); |
| 122 | 122 | self::dump($item, $indent + self::INDENT); |
@@ -163,10 +163,10 @@ discard block |
||
| 163 | 163 | if (is_array($subject) || $subject instanceof \ArrayIterator) { |
| 164 | 164 | $max = count($subject); |
| 165 | 165 | $objectAsArray = is_array($subject) ? $subject : $subject->getArrayCopy(); |
| 166 | - if(array_keys($objectAsArray) !== range(0, $max)) { |
|
| 166 | + if (array_keys($objectAsArray) !== range(0, $max)) { |
|
| 167 | 167 | $pairs = $objectAsArray; |
| 168 | 168 | } else { |
| 169 | - $valuesList = array_map(self::dump, $objectAsArray, array_fill( 0 , $max , $indent )); |
|
| 169 | + $valuesList = array_map(self::dump, $objectAsArray, array_fill(0, $max, $indent)); |
|
| 170 | 170 | return '['.implode(', ', $valuesList).']'; |
| 171 | 171 | } |
| 172 | 172 | } else { |
@@ -40,7 +40,9 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public static function toString($dataType, int $options = null):string |
| 42 | 42 | { |
| 43 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 43 | + if (is_null($dataType)) { |
|
| 44 | + throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
| 45 | + } |
|
| 44 | 46 | self::$options = is_int($options) ? $options : self::OPTIONS; |
| 45 | 47 | self::$result = new DLL; |
| 46 | 48 | if ($dataType instanceof YamlObject) { |
@@ -76,9 +78,13 @@ discard block |
||
| 76 | 78 | if (is_scalar($dataType)) { |
| 77 | 79 | switch (gettype($dataType)) { |
| 78 | 80 | case 'boolean': return $dataType ? 'true' : 'false'; |
| 79 | - case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 81 | + case 'float': if (is_infinite($dataType)) { |
|
| 82 | + return $dataType > 0 ? '.inf' : '-.inf'; |
|
| 83 | + } |
|
| 80 | 84 | return sprintf('%.2F', $dataType); |
| 81 | - case 'double': if (is_nan((float) $dataType)) return '.nan'; |
|
| 85 | + case 'double': if (is_nan((float) $dataType)) { |
|
| 86 | + return '.nan'; |
|
| 87 | + } |
|
| 82 | 88 | default: |
| 83 | 89 | return $dataType; |
| 84 | 90 | } |
@@ -91,7 +97,9 @@ discard block |
||
| 91 | 97 | |
| 92 | 98 | private static function dumpYamlObject(YamlObject $dataType) |
| 93 | 99 | { |
| 94 | - if ($dataType->hasDocStart() && self::$result instanceof DLL) self::$result->push("---"); |
|
| 100 | + if ($dataType->hasDocStart() && self::$result instanceof DLL) { |
|
| 101 | + self::$result->push("---"); |
|
| 102 | + } |
|
| 95 | 103 | // self::dump($dataType, 0); |
| 96 | 104 | if (count($dataType) > 0) { |
| 97 | 105 | self::dumpSequence($dataType->getArrayCopy(), 0); |
@@ -142,9 +150,13 @@ discard block |
||
| 142 | 150 | self::add(self::dump($obj->value, $indent + self::INDENT), $indent + self::INDENT); |
| 143 | 151 | } |
| 144 | 152 | } |
| 145 | - if ($obj instanceof Compact) return self::dumpCompact($obj, $indent); |
|
| 153 | + if ($obj instanceof Compact) { |
|
| 154 | + return self::dumpCompact($obj, $indent); |
|
| 155 | + } |
|
| 146 | 156 | //TODO: consider dumping datetime as date strings according to a format provided by user or default |
| 147 | - if ($obj instanceof \DateTime) return $obj->format('Y-m-d'); |
|
| 157 | + if ($obj instanceof \DateTime) { |
|
| 158 | + return $obj->format('Y-m-d'); |
|
| 159 | + } |
|
| 148 | 160 | // if ($obj instanceof \SplString) {var_dump('splstrin',$obj);return '"'.$obj.'"';} |
| 149 | 161 | $propList = get_object_vars($obj); |
| 150 | 162 | foreach ($propList as $property => $value) { |
@@ -102,6 +102,9 @@ |
||
| 102 | 102 | //TODO: $references = $dataType->getAllReferences(); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | + /** |
|
| 106 | + * @param integer $indent |
|
| 107 | + */ |
|
| 105 | 108 | private static function add($value, $indent) |
| 106 | 109 | { |
| 107 | 110 | $newVal = str_repeat(" ", $indent).$value; |
@@ -181,9 +181,9 @@ |
||
| 181 | 181 | return; |
| 182 | 182 | } |
| 183 | 183 | if (in_array($first, ['{', '['])) { |
| 184 | - $this->onCompact(trim($nodeValue)); |
|
| 185 | - return; |
|
| 186 | - } |
|
| 184 | + $this->onCompact(trim($nodeValue)); |
|
| 185 | + return; |
|
| 186 | + } |
|
| 187 | 187 | if (in_array($first, ['!', '&', '*'])) { |
| 188 | 188 | $this->onNodeAction($nodeValue); |
| 189 | 189 | return; |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | return; |
| 190 | 190 | } |
| 191 | 191 | // Note : php don't like '?' as an array key -_-' |
| 192 | - if(in_array($first, ['?', ':'])) { |
|
| 192 | + if (in_array($first, ['?', ':'])) { |
|
| 193 | 193 | $this->type = $first === '?' ? Y::SET_KEY : Y::SET_VALUE; |
| 194 | 194 | if (!empty(trim($v))) { |
| 195 | 195 | $this->value = new NodeList; |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | $this->onHyphen($nodeValue); |
| 202 | 202 | return; |
| 203 | 203 | } |
| 204 | - $characters = [ '#' => [Y::COMMENT, ltrim($v)], |
|
| 204 | + $characters = ['#' => [Y::COMMENT, ltrim($v)], |
|
| 205 | 205 | '%' => [Y::DIRECTIVE, ltrim($v)], |
| 206 | 206 | '>' => [Y::LITT_FOLDED, null], |
| 207 | 207 | '|' => [Y::LITT, null] |
@@ -254,28 +254,28 @@ discard block |
||
| 254 | 254 | private function onCompact($value) |
| 255 | 255 | { |
| 256 | 256 | $this->value = json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 257 | - if (json_last_error() === JSON_ERROR_NONE){ |
|
| 257 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
| 258 | 258 | $this->type = Y::JSON; |
| 259 | 259 | return; |
| 260 | 260 | } |
| 261 | 261 | $this->value = new NodeList(); |
| 262 | - if (preg_match(R::MAPPING, $value)){ |
|
| 262 | + if (preg_match(R::MAPPING, $value)) { |
|
| 263 | 263 | $this->type = Y::COMPACT_MAPPING; |
| 264 | 264 | $this->value->type = Y::COMPACT_MAPPING; |
| 265 | - preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1,-1)), $matches); |
|
| 265 | + preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1, -1)), $matches); |
|
| 266 | 266 | foreach ($matches['k'] as $index => $property) { |
| 267 | 267 | $n = new Node('', $this->line); |
| 268 | 268 | $n->type = Y::KEY; |
| 269 | - $n->identifier = trim($property, '"\' ');//TODO : maybe check for proper quoting first ? |
|
| 269 | + $n->identifier = trim($property, '"\' '); //TODO : maybe check for proper quoting first ? |
|
| 270 | 270 | $n->value = new Node($matches['v'][$index], $this->line); |
| 271 | 271 | $this->value->push($n); |
| 272 | 272 | } |
| 273 | 273 | return; |
| 274 | 274 | } |
| 275 | - if (preg_match(R::SEQUENCE, $value)){ |
|
| 275 | + if (preg_match(R::SEQUENCE, $value)) { |
|
| 276 | 276 | $this->type = Y::COMPACT_SEQUENCE; |
| 277 | 277 | $this->value->type = Y::COMPACT_SEQUENCE; |
| 278 | - $count = preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1,-1)), $matches); |
|
| 278 | + $count = preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1, -1)), $matches); |
|
| 279 | 279 | foreach ($matches['item'] as $key => $item) { |
| 280 | 280 | $i = new Node('', $this->line); |
| 281 | 281 | $i->type = Y::ITEM; |
@@ -68,7 +68,9 @@ discard block |
||
| 68 | 68 | if ($this->type === Y::ROOT) { |
| 69 | 69 | return $this; |
| 70 | 70 | } |
| 71 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
| 71 | + if (!is_int($indent)) { |
|
| 72 | + return $this->parent ?? $this; |
|
| 73 | + } |
|
| 72 | 74 | $cursor = $this; |
| 73 | 75 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
| 74 | 76 | if ($cursor->indent === $indent && $cursor->type !== $type) { |
@@ -99,7 +101,7 @@ discard block |
||
| 99 | 101 | if (is_null($current)) { |
| 100 | 102 | $this->value = $child; |
| 101 | 103 | return; |
| 102 | - }elseif ($current instanceof Node) { |
|
| 104 | + } elseif ($current instanceof Node) { |
|
| 103 | 105 | $this->value = new NodeList(); |
| 104 | 106 | if ($current->type & Y::LITTERALS) { |
| 105 | 107 | $this->value->type = $current->type; |
@@ -109,13 +111,21 @@ discard block |
||
| 109 | 111 | } |
| 110 | 112 | //modify type according to child |
| 111 | 113 | if (is_null($this->value->type)) { |
| 112 | - if ($child->type & Y::SET_KEY) $this->value->type = Y::SET; |
|
| 113 | - if ($child->type & Y::KEY) $this->value->type = Y::MAPPING; |
|
| 114 | - if ($child->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
|
| 114 | + if ($child->type & Y::SET_KEY) { |
|
| 115 | + $this->value->type = Y::SET; |
|
| 116 | + } |
|
| 117 | + if ($child->type & Y::KEY) { |
|
| 118 | + $this->value->type = Y::MAPPING; |
|
| 119 | + } |
|
| 120 | + if ($child->type & Y::ITEM) { |
|
| 121 | + $this->value->type = Y::SEQUENCE; |
|
| 122 | + } |
|
| 115 | 123 | } |
| 116 | 124 | $this->value->push($child); |
| 117 | 125 | |
| 118 | - if ($this->type & Y::LITTERALS) $this->value->type = $this->type; |
|
| 126 | + if ($this->type & Y::LITTERALS) { |
|
| 127 | + $this->value->type = $this->type; |
|
| 128 | + } |
|
| 119 | 129 | } |
| 120 | 130 | |
| 121 | 131 | /** |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * Create the Node object and parses $nodeString IF not null (else assume a root type Node) |
| 33 | 33 | * |
| 34 | 34 | * @param string|null $nodeString The node string |
| 35 | - * @param int|null $line The line |
|
| 35 | + * @param integer $line The line |
|
| 36 | 36 | */ |
| 37 | 37 | public function __construct($nodeString = null, $line = 0) |
| 38 | 38 | { |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | * Process when a "key: value" syntax is found in the parsed string |
| 222 | 222 | * Note : key is match 1, value is match 2 as per regex from R::KEY |
| 223 | 223 | * |
| 224 | - * @param array $matches The matches provided by 'preg_match' function in Node::parse |
|
| 224 | + * @param string[] $matches The matches provided by 'preg_match' function in Node::parse |
|
| 225 | 225 | */ |
| 226 | 226 | private function onKey(array $matches) |
| 227 | 227 | { |
@@ -29,7 +29,7 @@ |
||
| 29 | 29 | return $n->value; |
| 30 | 30 | } |
| 31 | 31 | $expected = [Y::QUOTED => trim((string) $n->value, "\"'"), |
| 32 | - Y::RAW => strval((string) $n->value)]; |
|
| 32 | + Y::RAW => strval((string) $n->value)]; |
|
| 33 | 33 | return $expected[$n->type] ?? null; |
| 34 | 34 | } |
| 35 | 35 | |
@@ -23,8 +23,12 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | public static function get(Node $n) |
| 25 | 25 | { |
| 26 | - if (is_null($n->value)) return null; |
|
| 27 | - if ($n->type & (Y::REF_CALL|Y::SCALAR)) return self::getScalar((string) $n->value); |
|
| 26 | + if (is_null($n->value)) { |
|
| 27 | + return null; |
|
| 28 | + } |
|
| 29 | + if ($n->type & (Y::REF_CALL|Y::SCALAR)) { |
|
| 30 | + return self::getScalar((string) $n->value); |
|
| 31 | + } |
|
| 28 | 32 | if ($n->type & Y::JSON) { |
| 29 | 33 | return $n->value; |
| 30 | 34 | } |
@@ -43,8 +47,12 @@ discard block |
||
| 43 | 47 | */ |
| 44 | 48 | private static function getScalar(string $v) |
| 45 | 49 | { |
| 46 | - if (R::isDate($v)) return date_create($v); |
|
| 47 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 50 | + if (R::isDate($v)) { |
|
| 51 | + return date_create($v); |
|
| 52 | + } |
|
| 53 | + if (R::isNumber($v)) { |
|
| 54 | + return self::getNumber($v); |
|
| 55 | + } |
|
| 48 | 56 | $types = ['yes' => true, |
| 49 | 57 | 'no' => false, |
| 50 | 58 | 'true' => true, |
@@ -66,8 +74,12 @@ discard block |
||
| 66 | 74 | */ |
| 67 | 75 | private static function getNumber(string $v) |
| 68 | 76 | { |
| 69 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
| 70 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
| 77 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
| 78 | + return intval(base_convert($v, 8, 10)); |
|
| 79 | + } |
|
| 80 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
| 81 | + return intval(base_convert($v, 16, 10)); |
|
| 82 | + } |
|
| 71 | 83 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
| 72 | 84 | } |
| 73 | 85 | } |
| 74 | 86 | \ No newline at end of file |
@@ -6,13 +6,13 @@ |
||
| 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 | /* USE CASE 1 |
| 12 | 12 | * load and parse if file exists |
| 13 | 13 | */ |
| 14 | 14 | ini_set("auto_detect_line_endings", 1); |
| 15 | -$content = file_get_contents('./tests/cases/parsing/multiline_quoted_with_blank.yml');//var_dump($content); |
|
| 15 | +$content = file_get_contents('./tests/cases/parsing/multiline_quoted_with_blank.yml'); //var_dump($content); |
|
| 16 | 16 | // $content = file_get_contents('./tests/cases/examples/Example_2_17.yml');//var_dump($content); |
| 17 | 17 | $yaml = Y::parse($content, null, 3); |
| 18 | 18 | // $yaml = Y::parseFile('./references/Example 2.27.yml', null, 1); |
@@ -19,17 +19,17 @@ discard block |
||
| 19 | 19 | /* @var null|string */ |
| 20 | 20 | public static $error; |
| 21 | 21 | |
| 22 | - public const EXCLUDE_DIRECTIVES = 1;//DONT include_directive |
|
| 23 | - public const IGNORE_COMMENTS = 2;//DONT include_comments |
|
| 24 | - public const NO_PARSING_EXCEPTIONS = 4;//THROW Exception on parsing Errors |
|
| 25 | - public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
|
| 22 | + public const EXCLUDE_DIRECTIVES = 1; //DONT include_directive |
|
| 23 | + public const IGNORE_COMMENTS = 2; //DONT include_comments |
|
| 24 | + public const NO_PARSING_EXCEPTIONS = 4; //THROW Exception on parsing Errors |
|
| 25 | + public const NO_OBJECT_FOR_DATE = 8; //DONT import date strings as dateTime Object |
|
| 26 | 26 | //privates |
| 27 | 27 | /* @var null|false|array| */ |
| 28 | 28 | private $content; |
| 29 | 29 | /* @var null|string */ |
| 30 | 30 | private $filePath; |
| 31 | 31 | /* @var integer */ |
| 32 | - private $debug = 0;///TODO: determine levels |
|
| 32 | + private $debug = 0; ///TODO: determine levels |
|
| 33 | 33 | /* @var integer */ |
| 34 | 34 | private $options = 0; |
| 35 | 35 | //Exceptions messages |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | $previous = $root = new Node(); |
| 93 | 93 | $emptyLines = []; |
| 94 | 94 | try { //var_dump($source); |
| 95 | - $gen = function () use($source) { |
|
| 95 | + $gen = function() use($source) { |
|
| 96 | 96 | foreach ($source as $key => $value) { |
| 97 | 97 | yield ++$key => $value; |
| 98 | 98 | } |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | case 0: |
| 117 | 117 | if ($n->type & Y::KEY && $n->indent === 0) { |
| 118 | 118 | $target = $root; |
| 119 | - } elseif($n->type & Y::ITEM && $deepest->type & Y::KEY && is_null($deepest->value)) { |
|
| 119 | + } elseif ($n->type & Y::ITEM && $deepest->type & Y::KEY && is_null($deepest->value)) { |
|
| 120 | 120 | $target = $deepest; |
| 121 | 121 | } else { |
| 122 | 122 | $target = $previous->getParent(); |
@@ -88,7 +88,9 @@ discard block |
||
| 88 | 88 | { |
| 89 | 89 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
| 90 | 90 | //TODO : be more permissive on $strContent values |
| 91 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 91 | + if (!is_array($source) || !count($source)) { |
|
| 92 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 93 | + } |
|
| 92 | 94 | $previous = $root = new Node(); |
| 93 | 95 | $emptyLines = []; |
| 94 | 96 | try { //var_dump($source); |
@@ -101,7 +103,9 @@ discard block |
||
| 101 | 103 | $n = new Node($lineString, $lineNb); |
| 102 | 104 | $deepest = $previous->getDeepestNode(); |
| 103 | 105 | if ($n->type & (Y::LITTERALS|Y::BLANK|Y::COMMENT|Y::TAG) || $deepest->type & Y::PARTIAL) { |
| 104 | - if ($this->onSpecialType($n, $previous, $emptyLines, $lineString)) continue; |
|
| 106 | + if ($this->onSpecialType($n, $previous, $emptyLines, $lineString)) { |
|
| 107 | + continue; |
|
| 108 | + } |
|
| 105 | 109 | } |
| 106 | 110 | //Note: 6.6 comments: Note that outside scalar content, a line containing only white space characters is taken to be a comment line. |
| 107 | 111 | foreach ($emptyLines as $blankNode) { |
@@ -130,7 +134,9 @@ discard block |
||
| 130 | 134 | } |
| 131 | 135 | } |
| 132 | 136 | } |
| 133 | - if ($this->onContextType($n, $target, $lineString)) continue; |
|
| 137 | + if ($this->onContextType($n, $target, $lineString)) { |
|
| 138 | + continue; |
|
| 139 | + } |
|
| 134 | 140 | $target->add($n); |
| 135 | 141 | $previous = $n; |
| 136 | 142 | } |
@@ -167,8 +173,12 @@ discard block |
||
| 167 | 173 | return true; |
| 168 | 174 | } |
| 169 | 175 | if ($n->type & Y::BLANK) { |
| 170 | - if ($previous->type & Y::SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 171 | - if ($deepest->type & Y::LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
| 176 | + if ($previous->type & Y::SCALAR) { |
|
| 177 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 178 | + } |
|
| 179 | + if ($deepest->type & Y::LITTERALS) { |
|
| 180 | + $emptyLines[] = $n->setParent($deepest); |
|
| 181 | + } |
|
| 172 | 182 | return true; |
| 173 | 183 | } elseif ($n->type & Y::COMMENT |
| 174 | 184 | && !($previous->getParent()->value->type & Y::LITTERALS) |
@@ -38,6 +38,9 @@ |
||
| 38 | 38 | private const EXCEPTION_READ_ERROR = self::class.": file '%s' failed to be loaded (permission denied ?)"; |
| 39 | 39 | private const EXCEPTION_LINE_SPLIT = self::class.": content is not a string(maybe a file error?)"; |
| 40 | 40 | |
| 41 | + /** |
|
| 42 | + * @param string $absolutePath |
|
| 43 | + */ |
|
| 41 | 44 | public function __construct($absolutePath = null, $options = null, $debug = 0) |
| 42 | 45 | { |
| 43 | 46 | $this->debug = is_int($debug) ? min($debug, 3) : 1; |
@@ -162,11 +162,11 @@ |
||
| 162 | 162 | } |
| 163 | 163 | if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
| 164 | 164 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
| 165 | - Y::ITEM => 'buildItem', |
|
| 166 | - Y::KEY => 'buildKey', |
|
| 167 | - Y::SET_KEY => 'buildSetKey', |
|
| 168 | - Y::SET_VALUE => 'buildSetValue', |
|
| 169 | - Y::TAG => 'buildTag', |
|
| 165 | + Y::ITEM => 'buildItem', |
|
| 166 | + Y::KEY => 'buildKey', |
|
| 167 | + Y::SET_KEY => 'buildSetKey', |
|
| 168 | + Y::SET_VALUE => 'buildSetValue', |
|
| 169 | + Y::TAG => 'buildTag', |
|
| 170 | 170 | ]; |
| 171 | 171 | if (isset($typesActions[$type])) { |
| 172 | 172 | return self::{$typesActions[$type]}($node, $parent); |
@@ -238,7 +238,6 @@ |
||
| 238 | 238 | /** |
| 239 | 239 | * Builds a litteral (folded or not) or any NodeList that has YAML::RAW type (like a multiline value) |
| 240 | 240 | * |
| 241 | - * @param NodeList $children The children |
|
| 242 | 241 | * @param integer $type The type |
| 243 | 242 | * |
| 244 | 243 | * @return string The litteral. |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | $q = new NodeList; |
| 38 | 38 | $q->push($_root->value); |
| 39 | 39 | self::$_root = new YamlObject; |
| 40 | - $tmp = self::buildNodeList($q, self::$_root); |
|
| 40 | + $tmp = self::buildNodeList($q, self::$_root); |
|
| 41 | 41 | return $tmp; |
| 42 | 42 | } |
| 43 | 43 | $_root->value instanceof NodeList && $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
@@ -82,13 +82,13 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @return mixed The parent (object|array) or a string representing the NodeList. |
| 84 | 84 | */ |
| 85 | - private static function buildNodeList(NodeList $node, &$parent=null) |
|
| 85 | + private static function buildNodeList(NodeList $node, &$parent = null) |
|
| 86 | 86 | { |
| 87 | 87 | $node->forceType(); |
| 88 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
| 88 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
| 89 | 89 | return self::buildLitteral($node, (int) $node->type); |
| 90 | 90 | } |
| 91 | - $action = function ($child, &$parent, &$out) { |
|
| 91 | + $action = function($child, &$parent, &$out) { |
|
| 92 | 92 | self::build($child, $out); |
| 93 | 93 | }; |
| 94 | 94 | if ($node->type & (Y::COMPACT_MAPPING|Y::MAPPING|Y::SET)) { |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | $out = $parent ?? []; |
| 98 | 98 | } else { |
| 99 | 99 | $out = ''; |
| 100 | - $action = function ($child, &$parent, &$out) { |
|
| 100 | + $action = function($child, &$parent, &$out) { |
|
| 101 | 101 | if ($child->type & (Y::SCALAR|Y::QUOTED)) { |
| 102 | 102 | if ($parent) { |
| 103 | 103 | $parent->setText(self::build($child)); |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | private static function buildNode(Node $node, &$parent) |
| 128 | 128 | { |
| 129 | 129 | extract((array) $node, EXTR_REFS); |
| 130 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
| 130 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
| 131 | 131 | if (is_object($value)) { |
| 132 | 132 | $tmp = self::build($value, $parent) ?? $parent; |
| 133 | 133 | } else { |
@@ -136,10 +136,10 @@ discard block |
||
| 136 | 136 | if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
| 137 | 137 | return self::$_root->getReference($identifier); |
| 138 | 138 | } |
| 139 | - if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
| 139 | + if ($type&(Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
| 140 | 140 | return self::buildNodeList($node->value, $parent); |
| 141 | 141 | } |
| 142 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
| 142 | + if ($type&Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
| 143 | 143 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
| 144 | 144 | Y::ITEM => 'buildItem', |
| 145 | 145 | Y::KEY => 'buildKey', |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
| 163 | 163 | * @return null |
| 164 | 164 | */ |
| 165 | - private static function buildKey(Node $node, &$parent=null) |
|
| 165 | + private static function buildKey(Node $node, &$parent = null) |
|
| 166 | 166 | { |
| 167 | 167 | extract((array) $node, EXTR_REFS); |
| 168 | 168 | if (is_null($identifier)) { |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | } |
| 181 | 181 | if ($value instanceof NodeList) { |
| 182 | 182 | $childTypes = $value->getTypes(); |
| 183 | - if (is_null($value->type) && $childTypes & Y::SCALAR && !($childTypes & Y::COMMENT)) { |
|
| 183 | + if (is_null($value->type) && $childTypes&Y::SCALAR && !($childTypes&Y::COMMENT)) { |
|
| 184 | 184 | $result = self::buildLitteral($value, Y::LITT_FOLDED); |
| 185 | 185 | } else { |
| 186 | 186 | $result = self::buildNodeList($value); |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | */ |
| 210 | 210 | private static function buildItem(Node $node, &$parent) |
| 211 | 211 | { |
| 212 | - extract((array) $node, EXTR_REFS);//var_dump(__METHOD__); |
|
| 212 | + extract((array) $node, EXTR_REFS); //var_dump(__METHOD__); |
|
| 213 | 213 | if (!is_array($parent) && !($parent instanceof \ArrayIterator)) { |
| 214 | 214 | throw new \Exception("parent must be an Iterable not ".(is_object($parent) ? get_class($parent) : gettype($parent)), 1); |
| 215 | 215 | } |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | $numKeys = array_filter(array_keys($ref), 'is_int'); |
| 218 | 218 | $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0; |
| 219 | 219 | if ($value instanceof Node) { |
| 220 | - if($value->type & Y::KEY) { |
|
| 220 | + if ($value->type & Y::KEY) { |
|
| 221 | 221 | self::buildKey($node->value, $parent); |
| 222 | 222 | return; |
| 223 | 223 | } elseif ($value->type & Y::ITEM) { |
@@ -254,14 +254,14 @@ discard block |
||
| 254 | 254 | } |
| 255 | 255 | $result = ''; |
| 256 | 256 | $separator = ''; |
| 257 | - if ($type & Y::LITT) $separator = "\n"; |
|
| 258 | - if ($type & Y::LITT_FOLDED) $separator = ' '; |
|
| 257 | + if ($type&Y::LITT) $separator = "\n"; |
|
| 258 | + if ($type&Y::LITT_FOLDED) $separator = ' '; |
|
| 259 | 259 | foreach ($list as $child) { |
| 260 | 260 | if ($child->value instanceof NodeList) { |
| 261 | 261 | $result .= self::buildLitteral($child->value, $type).$separator; |
| 262 | 262 | } else { |
| 263 | 263 | $val = $child->type & (Y::SCALAR|Y::BLANK) ? $child->value : substr($child->raw, $refIndent); |
| 264 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 264 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 265 | 265 | if ($result[-1] === $separator) |
| 266 | 266 | $result[-1] = "\n"; |
| 267 | 267 | if ($result[-1] === "\n") |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | private function buildSetKey(Node $node, &$parent) |
| 286 | 286 | { |
| 287 | 287 | $built = is_object($node->value) ? self::build($node->value) : null; |
| 288 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
| 288 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
| 289 | 289 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 290 | 290 | // if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
| 291 | 291 | $parent->{trim($key, '\'" ')} = null; |
@@ -42,10 +42,14 @@ discard block |
||
| 42 | 42 | } |
| 43 | 43 | $_root->value instanceof NodeList && $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 44 | 44 | foreach ($_root->value as $child) { |
| 45 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 45 | + if ($child->type & Y::DOC_START) { |
|
| 46 | + $totalDocStart++; |
|
| 47 | + } |
|
| 46 | 48 | //if 0 or 1 DOC_START = we are still in first document |
| 47 | 49 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 48 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 50 | + if (!isset($documents[$currentDoc])) { |
|
| 51 | + $documents[$currentDoc] = new NodeList(); |
|
| 52 | + } |
|
| 49 | 53 | $documents[$currentDoc]->push($child); |
| 50 | 54 | } |
| 51 | 55 | $content = []; |
@@ -70,7 +74,9 @@ discard block |
||
| 70 | 74 | */ |
| 71 | 75 | private static function build(object $node, &$parent = null) |
| 72 | 76 | { |
| 73 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
| 77 | + if ($node instanceof NodeList) { |
|
| 78 | + return self::buildNodeList($node, $parent); |
|
| 79 | + } |
|
| 74 | 80 | return self::buildNode($node, $parent); |
| 75 | 81 | } |
| 76 | 82 | |
@@ -133,13 +139,17 @@ discard block |
||
| 133 | 139 | } else { |
| 134 | 140 | $tmp = Node2PHP::get($node); |
| 135 | 141 | } |
| 136 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 142 | + if ($type === Y::REF_DEF) { |
|
| 143 | + self::$_root->addReference($identifier, $tmp); |
|
| 144 | + } |
|
| 137 | 145 | return self::$_root->getReference($identifier); |
| 138 | 146 | } |
| 139 | 147 | if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
| 140 | 148 | return self::buildNodeList($node->value, $parent); |
| 141 | 149 | } |
| 142 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
| 150 | + if ($type & Y::COMMENT) { |
|
| 151 | + self::$_root->addComment($node->line, $node->value); |
|
| 152 | + } |
|
| 143 | 153 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
| 144 | 154 | Y::ITEM => 'buildItem', |
| 145 | 155 | Y::KEY => 'buildKey', |
@@ -254,18 +264,24 @@ discard block |
||
| 254 | 264 | } |
| 255 | 265 | $result = ''; |
| 256 | 266 | $separator = ''; |
| 257 | - if ($type & Y::LITT) $separator = "\n"; |
|
| 258 | - if ($type & Y::LITT_FOLDED) $separator = ' '; |
|
| 267 | + if ($type & Y::LITT) { |
|
| 268 | + $separator = "\n"; |
|
| 269 | + } |
|
| 270 | + if ($type & Y::LITT_FOLDED) { |
|
| 271 | + $separator = ' '; |
|
| 272 | + } |
|
| 259 | 273 | foreach ($list as $child) { |
| 260 | 274 | if ($child->value instanceof NodeList) { |
| 261 | 275 | $result .= self::buildLitteral($child->value, $type).$separator; |
| 262 | 276 | } else { |
| 263 | 277 | $val = $child->type & (Y::SCALAR|Y::BLANK) ? $child->value : substr($child->raw, $refIndent); |
| 264 | 278 | if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
| 265 | - if ($result[-1] === $separator) |
|
| 266 | - $result[-1] = "\n"; |
|
| 267 | - if ($result[-1] === "\n") |
|
| 268 | - $result .= $val; |
|
| 279 | + if ($result[-1] === $separator) { |
|
| 280 | + $result[-1] = "\n"; |
|
| 281 | + } |
|
| 282 | + if ($result[-1] === "\n") { |
|
| 283 | + $result .= $val; |
|
| 284 | + } |
|
| 269 | 285 | continue; |
| 270 | 286 | } |
| 271 | 287 | $result .= $val.$separator; |
@@ -33,8 +33,12 @@ |
||
| 33 | 33 | public function jsonSerialize():array |
| 34 | 34 | { |
| 35 | 35 | $prop = get_object_vars($this); |
| 36 | - if (count($prop) > 0) return $prop; |
|
| 37 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 36 | + if (count($prop) > 0) { |
|
| 37 | + return $prop; |
|
| 38 | + } |
|
| 39 | + if (count($this) > 0) { |
|
| 40 | + return iterator_to_array($this); |
|
| 41 | + } |
|
| 38 | 42 | return new \Stdclass; |
| 39 | 43 | } |
| 40 | 44 | |
@@ -40,18 +40,18 @@ |
||
| 40 | 40 | public function forceType() |
| 41 | 41 | { |
| 42 | 42 | if (is_null($this->type)) { |
| 43 | - $childTypes = $this->getTypes(); |
|
| 44 | - if ($childTypes & (Y::KEY|Y::SET_KEY)) { |
|
| 45 | - if ($childTypes & Y::ITEM) { |
|
| 43 | + $childTypes = $this->getTypes(); |
|
| 44 | + if ($childTypes&(Y::KEY|Y::SET_KEY)) { |
|
| 45 | + if ($childTypes&Y::ITEM) { |
|
| 46 | 46 | // TODO: replace the document index in HERE ----------v |
| 47 | 47 | throw new \ParseError(self::class.": Error conflicting types found"); |
| 48 | 48 | } else { |
| 49 | 49 | $this->type = Y::MAPPING; |
| 50 | 50 | } |
| 51 | 51 | } else { |
| 52 | - if ($childTypes & Y::ITEM) { |
|
| 52 | + if ($childTypes&Y::ITEM) { |
|
| 53 | 53 | $this->type = Y::SEQUENCE; |
| 54 | - } elseif (!($childTypes & Y::COMMENT)) { |
|
| 54 | + } elseif (!($childTypes&Y::COMMENT)) { |
|
| 55 | 55 | $this->type = Y::LITT_FOLDED; |
| 56 | 56 | } |
| 57 | 57 | } |