@@ -16,7 +16,7 @@ |
||
| 16 | 16 | if (is_null($tagName)) { |
| 17 | 17 | throw new \Exception(self::class.": a tag MUST have a name", 1); |
| 18 | 18 | } |
| 19 | - $this->tagName = $tagName; |
|
| 19 | + $this->tagName = $tagName; |
|
| 20 | 20 | $this->value = $value; |
| 21 | 21 | } |
| 22 | 22 | |
@@ -14,10 +14,10 @@ discard block |
||
| 14 | 14 | const AN = "[\w ]+"; |
| 15 | 15 | const NUM = "-?[\d.e]+"; |
| 16 | 16 | const SIMPLE = "(?P<sv>".self::NULL."|". |
| 17 | - self::FALSE."|". |
|
| 18 | - self::TRUE."|". |
|
| 19 | - self::AN."|". |
|
| 20 | - self::NUM.")"; |
|
| 17 | + self::FALSE."|". |
|
| 18 | + self::TRUE."|". |
|
| 19 | + self::AN."|". |
|
| 20 | + self::NUM.")"; |
|
| 21 | 21 | private const seqForMap = "(?P<seq>\[(?:(?:(?P>sv)|(?P>seq)|(?P>map)),?\s*)+\])"; |
| 22 | 22 | private const mapForSeq = "(?P<map>{\s*(?:".self::AN."\s*:\s*(?:(?P>sv)|(?P>seq)|(?P>map)),?\s*)+})"; |
| 23 | 23 | |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | $matchSpaced = preg_match($spaced, $v); |
| 42 | 42 | $matchIso = preg_match($iso8601, $v); |
| 43 | 43 | if (is_bool($matchDate) || is_bool($matchCanonical) || is_bool($matchSpaced) || is_bool($matchIso)) |
| 44 | - throw new \Exception("Regex date error"); |
|
| 44 | + throw new \Exception("Regex date error"); |
|
| 45 | 45 | |
| 46 | 46 | return $matchDate || $matchCanonical || $matchSpaced || $matchIso; |
| 47 | 47 | } |
@@ -32,10 +32,10 @@ |
||
| 32 | 32 | { |
| 33 | 33 | $d = "\\d{4}([-\\/])\\d{2}\\1\\d{2}"; |
| 34 | 34 | $h = "\\d{2}(:)\\d{2}\\2\\d{2}"; |
| 35 | - $date = "/^$d$/";// 2002-12-14, 2002/12/14 |
|
| 36 | - $canonical = "/^$d(?:t| )$h\\.\\dz?$/im";// 2001-12-15T02:59:43.1Z |
|
| 37 | - $spaced = "/^$d(?:t| )$h\\.\\d{2} [-+]\\d$/im";// 2001-12-14 21:59:43.10 -5 |
|
| 38 | - $iso8601 = "/^$d(?:t| )$h\\.\\d{2}[-+]\\d{2}\\2\\d{2}/im";// 2001-12-14t21:59:43.10-05:00 |
|
| 35 | + $date = "/^$d$/"; // 2002-12-14, 2002/12/14 |
|
| 36 | + $canonical = "/^$d(?:t| )$h\\.\\dz?$/im"; // 2001-12-15T02:59:43.1Z |
|
| 37 | + $spaced = "/^$d(?:t| )$h\\.\\d{2} [-+]\\d$/im"; // 2001-12-14 21:59:43.10 -5 |
|
| 38 | + $iso8601 = "/^$d(?:t| )$h\\.\\d{2}[-+]\\d{2}\\2\\d{2}/im"; // 2001-12-14t21:59:43.10-05:00 |
|
| 39 | 39 | $matchDate = preg_match($date, $v); |
| 40 | 40 | $matchCanonical = preg_match($canonical, $v); |
| 41 | 41 | $matchSpaced = preg_match($spaced, $v); |
@@ -40,8 +40,9 @@ |
||
| 40 | 40 | $matchCanonical = preg_match($canonical, $v); |
| 41 | 41 | $matchSpaced = preg_match($spaced, $v); |
| 42 | 42 | $matchIso = preg_match($iso8601, $v); |
| 43 | - if (is_bool($matchDate) || is_bool($matchCanonical) || is_bool($matchSpaced) || is_bool($matchIso)) |
|
| 44 | - throw new \Exception("Regex date error"); |
|
| 43 | + if (is_bool($matchDate) || is_bool($matchCanonical) || is_bool($matchSpaced) || is_bool($matchIso)) { |
|
| 44 | + throw new \Exception("Regex date error"); |
|
| 45 | + } |
|
| 45 | 46 | |
| 46 | 47 | return $matchDate || $matchCanonical || $matchSpaced || $matchIso; |
| 47 | 48 | } |
@@ -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 | { |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | public function __construct() |
| 22 | 22 | { |
| 23 | - parent::__construct([], 1);//1 = Array indices can be accessed as properties in read/write. |
|
| 23 | + parent::__construct([], 1); //1 = Array indices can be accessed as properties in read/write. |
|
| 24 | 24 | $this->__yaml__object__api = new API(); |
| 25 | 25 | } |
| 26 | 26 | |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | public function __call($funcName, $arguments) |
| 29 | 29 | { |
| 30 | 30 | $reflectAPI = new \ReflectionClass(get_class($this->__yaml__object__api)); |
| 31 | - $getName = function ($o) { return $o->name; }; |
|
| 31 | + $getName = function($o) { return $o->name; }; |
|
| 32 | 32 | $publicApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PUBLIC)); |
| 33 | 33 | $privateApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PRIVATE)); |
| 34 | 34 | if (!in_array($funcName, $publicApi) && !in_array($funcName, $privateApi)) { |
@@ -40,8 +40,12 @@ |
||
| 40 | 40 | { |
| 41 | 41 | $prop = get_object_vars($this); |
| 42 | 42 | unset($prop["__yaml__object__api"]); |
| 43 | - if (count($prop) > 0) return $prop; |
|
| 44 | - if (count($this) > 0) return iterator_to_array($this); |
|
| 43 | + if (count($prop) > 0) { |
|
| 44 | + return $prop; |
|
| 45 | + } |
|
| 46 | + if (count($this) > 0) { |
|
| 47 | + return iterator_to_array($this); |
|
| 48 | + } |
|
| 45 | 49 | return $this->__yaml__object__api->value; |
| 46 | 50 | } |
| 47 | 51 | } |
@@ -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); |
@@ -89,8 +89,8 @@ discard block |
||
| 89 | 89 | //modify type according to child |
| 90 | 90 | switch ($child->type) { |
| 91 | 91 | case Y\COMMENT: //fall through |
| 92 | - case Y\KEY: $this->value->type = Y\MAPPING;break; |
|
| 93 | - case Y\ITEM: $this->value->type = Y\SEQUENCE;break; |
|
| 92 | + case Y\KEY: $this->value->type = Y\MAPPING; break; |
|
| 93 | + case Y\ITEM: $this->value->type = Y\SEQUENCE; break; |
|
| 94 | 94 | // case Y\COMMENT: $this->value->type = Y\RAW;break; |
| 95 | 95 | } |
| 96 | 96 | } |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | */ |
| 116 | 116 | public function parse(string $nodeString):Node |
| 117 | 117 | { |
| 118 | - $nodeValue = preg_replace("/^\t+/m", " ", $nodeString);//permissive to tabs but replacement |
|
| 118 | + $nodeValue = preg_replace("/^\t+/m", " ", $nodeString); //permissive to tabs but replacement |
|
| 119 | 119 | $this->indent = strspn($nodeValue, ' '); |
| 120 | 120 | $nodeValue = ltrim($nodeValue); |
| 121 | 121 | if ($nodeValue === '') { |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | case '#': return [Y\COMMENT, ltrim($v)]; |
| 153 | 153 | case "-": return $this->onHyphen($nodeValue); |
| 154 | 154 | case '%': return [Y\DIRECTIVE, ltrim($v)]; |
| 155 | - case '?': return [Y\SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 155 | + case '?': return [Y\SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 156 | 156 | case ':': return [Y\SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
| 157 | 157 | case '>': return [Y\LITT_FOLDED, null]; |
| 158 | 158 | case '|': return [Y\LITT, null]; |
@@ -177,7 +177,7 @@ discard block |
||
| 177 | 177 | if (!is_bool($hasComment)) { |
| 178 | 178 | $tmpNode = new Node(trim(substr($keyValue, 0, $hasComment)), $this->line); |
| 179 | 179 | if ($tmpNode->type !== Y\PARTIAL) { |
| 180 | - $comment = new Node(trim(substr($keyValue, $hasComment+1)), $this->line); |
|
| 180 | + $comment = new Node(trim(substr($keyValue, $hasComment + 1)), $this->line); |
|
| 181 | 181 | //TODO: modify "identifier" to specify if fullline comment or not |
| 182 | 182 | $this->add($comment); |
| 183 | 183 | $n = $tmpNode; |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | $type = ['!' => Y\TAG, '&' => Y\REF_DEF, '*' => Y\REF_CALL][$nodeValue[0]]; |
| 247 | 247 | $pos = strpos($v, ' '); |
| 248 | 248 | $this->identifier = is_bool($pos) ? $v : strstr($v, ' ', true); |
| 249 | - $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos+1)), $this->line))->setParent($this); |
|
| 249 | + $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos + 1)), $this->line))->setParent($this); |
|
| 250 | 250 | return [$type, $n]; |
| 251 | 251 | } |
| 252 | 252 | |
@@ -260,8 +260,8 @@ discard block |
||
| 260 | 260 | { |
| 261 | 261 | $v = $this->value; |
| 262 | 262 | if (is_null($v)) return null; |
| 263 | - if ($this->type & (Y\REF_CALL | Y\SCALAR)) return self::getScalar($v); |
|
| 264 | - if ($this->type & (Y\COMPACT_MAPPING | Y\COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 263 | + if ($this->type & (Y\REF_CALL|Y\SCALAR)) return self::getScalar($v); |
|
| 264 | + if ($this->type & (Y\COMPACT_MAPPING|Y\COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 265 | 265 | switch ($this->type) { |
| 266 | 266 | case Y\JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
| 267 | 267 | case Y\QUOTED: return substr($v, 1, -1); |
@@ -317,7 +317,7 @@ discard block |
||
| 317 | 317 | { |
| 318 | 318 | $out = new Compact(); |
| 319 | 319 | if ($type === Y\COMPACT_SEQUENCE) { |
| 320 | - $f = function ($e) { return self::getScalar(trim($e));}; |
|
| 320 | + $f = function($e) { return self::getScalar(trim($e)); }; |
|
| 321 | 321 | //TODO : that's not robust enough, improve it |
| 322 | 322 | foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) { |
| 323 | 323 | $out[$key] = $value; |
@@ -55,7 +55,9 @@ discard block |
||
| 55 | 55 | */ |
| 56 | 56 | public function getParent(int $indent = null):Node |
| 57 | 57 | { |
| 58 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
| 58 | + if (!is_int($indent)) { |
|
| 59 | + return $this->parent ?? $this; |
|
| 60 | + } |
|
| 59 | 61 | $cursor = $this; |
| 60 | 62 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
| 61 | 63 | $cursor = $cursor->parent; |
@@ -146,8 +148,12 @@ discard block |
||
| 146 | 148 | $type = R::isProperlyQuoted($nodeValue) ? Y\QUOTED : Y\PARTIAL; |
| 147 | 149 | return [$type, $nodeValue]; |
| 148 | 150 | } |
| 149 | - if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
|
| 150 | - if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
|
| 151 | + if (in_array($first, ['{', '['])) { |
|
| 152 | + return $this->onObject($nodeValue); |
|
| 153 | + } |
|
| 154 | + if (in_array($first, ['!', '&', '*'])) { |
|
| 155 | + return $this->onNodeAction($nodeValue); |
|
| 156 | + } |
|
| 151 | 157 | switch ($first) { |
| 152 | 158 | case '#': return [Y\COMMENT, ltrim($v)]; |
| 153 | 159 | case "-": return $this->onHyphen($nodeValue); |
@@ -199,9 +205,15 @@ discard block |
||
| 199 | 205 | private function onObject($value):array |
| 200 | 206 | { |
| 201 | 207 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 202 | - if (json_last_error() === JSON_ERROR_NONE) return [Y\JSON, $value]; |
|
| 203 | - if (preg_match(R::MAPPING, $value)) return [Y\COMPACT_MAPPING, $value]; |
|
| 204 | - if (preg_match(R::SEQUENCE, $value)) return [Y\COMPACT_SEQUENCE, $value]; |
|
| 208 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
| 209 | + return [Y\JSON, $value]; |
|
| 210 | + } |
|
| 211 | + if (preg_match(R::MAPPING, $value)) { |
|
| 212 | + return [Y\COMPACT_MAPPING, $value]; |
|
| 213 | + } |
|
| 214 | + if (preg_match(R::SEQUENCE, $value)) { |
|
| 215 | + return [Y\COMPACT_SEQUENCE, $value]; |
|
| 216 | + } |
|
| 205 | 217 | return [Y\PARTIAL, $value]; |
| 206 | 218 | } |
| 207 | 219 | |
@@ -216,7 +228,9 @@ discard block |
||
| 216 | 228 | { |
| 217 | 229 | if (substr($nodeValue, 0, 3) === '---') { |
| 218 | 230 | $rest = trim(substr($nodeValue, 3)); |
| 219 | - if (empty($rest)) return [Y\DOC_START, null]; |
|
| 231 | + if (empty($rest)) { |
|
| 232 | + return [Y\DOC_START, null]; |
|
| 233 | + } |
|
| 220 | 234 | $n = new Node($rest, $this->line); |
| 221 | 235 | $n->indent = $this->indent + 4; |
| 222 | 236 | return [Y\DOC_START, $n->setParent($this)]; |
@@ -259,9 +273,15 @@ discard block |
||
| 259 | 273 | public function getPhpValue() |
| 260 | 274 | { |
| 261 | 275 | $v = $this->value; |
| 262 | - if (is_null($v)) return null; |
|
| 263 | - if ($this->type & (Y\REF_CALL | Y\SCALAR)) return self::getScalar($v); |
|
| 264 | - if ($this->type & (Y\COMPACT_MAPPING | Y\COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 276 | + if (is_null($v)) { |
|
| 277 | + return null; |
|
| 278 | + } |
|
| 279 | + if ($this->type & (Y\REF_CALL | Y\SCALAR)) { |
|
| 280 | + return self::getScalar($v); |
|
| 281 | + } |
|
| 282 | + if ($this->type & (Y\COMPACT_MAPPING | Y\COMPACT_SEQUENCE)) { |
|
| 283 | + return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 284 | + } |
|
| 265 | 285 | switch ($this->type) { |
| 266 | 286 | case Y\JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
| 267 | 287 | case Y\QUOTED: return substr($v, 1, -1); |
@@ -290,9 +310,15 @@ discard block |
||
| 290 | 310 | '-.inf' => -INF, |
| 291 | 311 | '.nan' => NAN |
| 292 | 312 | ]; |
| 293 | - if (isset($types[strtolower($v)])) return $types[strtolower($v)]; |
|
| 294 | - if (R::isDate($v)) return date_create($v); |
|
| 295 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 313 | + if (isset($types[strtolower($v)])) { |
|
| 314 | + return $types[strtolower($v)]; |
|
| 315 | + } |
|
| 316 | + if (R::isDate($v)) { |
|
| 317 | + return date_create($v); |
|
| 318 | + } |
|
| 319 | + if (R::isNumber($v)) { |
|
| 320 | + return self::getNumber($v); |
|
| 321 | + } |
|
| 296 | 322 | return strval($v); |
| 297 | 323 | } |
| 298 | 324 | |
@@ -305,8 +331,12 @@ discard block |
||
| 305 | 331 | */ |
| 306 | 332 | private static function getNumber(string $v) |
| 307 | 333 | { |
| 308 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
| 309 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
| 334 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
| 335 | + return intval(base_convert($v, 8, 10)); |
|
| 336 | + } |
|
| 337 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
| 338 | + return intval(base_convert($v, 16, 10)); |
|
| 339 | + } |
|
| 310 | 340 | // TODO: remove these if not needed |
| 311 | 341 | // if preg_match("/^([\d.]+e[-+]\d{1,2})$/", $v)://fall through |
| 312 | 342 | // if preg_match("/^([-+]?(?:\d+|\d*.\d+))$/", $v): |
@@ -1,5 +1,5 @@ |
||
| 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 | |
@@ -15,6 +15,6 @@ |
||
| 15 | 15 | |
| 16 | 16 | public function __debugInfo():array |
| 17 | 17 | { |
| 18 | - return ['type' => Y::getName($this->type) , "dllist" => $this->dllist]; |
|
| 18 | + return ['type' => Y::getName($this->type), "dllist" => $this->dllist]; |
|
| 19 | 19 | } |
| 20 | 20 | } |
| 21 | 21 | \ No newline at end of file |
@@ -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 NO_PARSING_EXCEPTIONS = 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 NO_PARSING_EXCEPTIONS = 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 | $deepest = $previous->getDeepestNode(); |
| 87 | 87 | if ($deepest->type & Y\PARTIAL) { |
| 88 | 88 | //TODO:verify this edge case |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | } |
| 102 | 102 | $emptyLines = []; |
| 103 | 103 | switch ($n->indent <=> $previous->indent) { |
| 104 | - case -1: $previous->getParent($n->indent)->add($n);break; |
|
| 105 | - case 0: $previous->getParent()->add($n);break; |
|
| 104 | + case -1: $previous->getParent($n->indent)->add($n); break; |
|
| 105 | + case 0: $previous->getParent()->add($n); break; |
|
| 106 | 106 | default: |
| 107 | 107 | if ($this->onDeepestType($n, $previous, $lineString)) continue 2; |
| 108 | 108 | $previous->add($n); |
@@ -114,11 +114,11 @@ 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 (\Error|\Exception|\ParseError $e) { |
| 120 | 120 | $file = basename($this->filePath); |
| 121 | - $message = basename($e->getFile())."@".$e->getLine().":".$e->getMessage()." in '$file' @".($lineNb+1)."\n"; |
|
| 121 | + $message = basename($e->getFile())."@".$e->getLine().":".$e->getMessage()." in '$file' @".($lineNb + 1)."\n"; |
|
| 122 | 122 | if ($e instanceof \ParseError && ($this->options & self::NO_PARSING_EXCEPTIONS)) { |
| 123 | 123 | trigger_error($message, E_USER_WARNING); |
| 124 | 124 | $this->errors[] = $message; |
@@ -158,7 +158,7 @@ discard block |
||
| 158 | 158 | // // if ($previous->type & Y\ITEM && $n->type & Y\KEY) { |
| 159 | 159 | // // $previous |
| 160 | 160 | // // } |
| 161 | - if ($deepest->type & (Y\BLANK | Y\SCALAR) ) {//|| ($previous->type & (Y\ITEM | Y\SET_KEY))) { |
|
| 161 | + if ($deepest->type & (Y\BLANK|Y\SCALAR)) {//|| ($previous->type & (Y\ITEM | Y\SET_KEY))) { |
|
| 162 | 162 | $previous = $deepest->getParent(); |
| 163 | 163 | // // if ($n->type === Y\SCALAR && ($deepest->getParent()->type & Y\LITTERALS)) { |
| 164 | 164 | // // // $deepest->type = Y\SCALAR; |
@@ -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; |
@@ -104,7 +108,9 @@ discard block |
||
| 104 | 108 | case -1: $previous->getParent($n->indent)->add($n);break; |
| 105 | 109 | case 0: $previous->getParent()->add($n);break; |
| 106 | 110 | default: |
| 107 | - if ($this->onDeepestType($n, $previous, $lineString)) continue 2; |
|
| 111 | + if ($this->onDeepestType($n, $previous, $lineString)) { |
|
| 112 | + continue 2; |
|
| 113 | + } |
|
| 108 | 114 | $previous->add($n); |
| 109 | 115 | } |
| 110 | 116 | $previous = $n; |
@@ -139,8 +145,12 @@ discard block |
||
| 139 | 145 | } |
| 140 | 146 | } |
| 141 | 147 | 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); |
|
| 148 | + if ($previous->type & Y\SCALAR) { |
|
| 149 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
| 150 | + } |
|
| 151 | + if ($deepest->type & Y\LITTERALS) { |
|
| 152 | + $emptyLines[] = $n->setParent($deepest); |
|
| 153 | + } |
|
| 144 | 154 | return true; |
| 145 | 155 | } |
| 146 | 156 | return false; |
@@ -25,14 +25,14 @@ discard block |
||
| 25 | 25 | private static function buildNodeList(NodeList $node, &$parent) |
| 26 | 26 | { |
| 27 | 27 | $type = property_exists($node, "type") ? $node->type : null; |
| 28 | - if ($type & (Y\RAW | Y\LITTERALS)) { |
|
| 28 | + if ($type&(Y\RAW|Y\LITTERALS)) { |
|
| 29 | 29 | return self::litteral($node, $type); |
| 30 | 30 | } |
| 31 | 31 | $p = $parent; |
| 32 | 32 | switch ($type) { |
| 33 | 33 | case Y\MAPPING: //fall through |
| 34 | - case Y\SET: $p = new \StdClass;break; |
|
| 35 | - case Y\SEQUENCE: $p = [];break; |
|
| 34 | + case Y\SET: $p = new \StdClass; break; |
|
| 35 | + case Y\SEQUENCE: $p = []; break; |
|
| 36 | 36 | // case Y\KEY: $p = $parent;break; |
| 37 | 37 | } |
| 38 | 38 | $out = null; |
@@ -53,10 +53,10 @@ discard block |
||
| 53 | 53 | { |
| 54 | 54 | list($line, $type, $value, $identifier) = [$node->line, $node->type, $node->value, $node->identifier]; |
| 55 | 55 | switch ($type) { |
| 56 | - case Y\COMMENT: self::$_root->addComment($line, $value);return; |
|
| 57 | - case Y\DIRECTIVE: return;//TODO |
|
| 58 | - case Y\ITEM: self::buildItem($value, $parent);return; |
|
| 59 | - case Y\KEY: self::buildKey($node, $parent);return; |
|
| 56 | + case Y\COMMENT: self::$_root->addComment($line, $value); return; |
|
| 57 | + case Y\DIRECTIVE: return; //TODO |
|
| 58 | + case Y\ITEM: self::buildItem($value, $parent); return; |
|
| 59 | + case Y\KEY: self::buildKey($node, $parent); return; |
|
| 60 | 60 | case Y\REF_DEF: //fall through |
| 61 | 61 | case Y\REF_CALL://TODO: self::build returns what ? |
| 62 | 62 | $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
@@ -71,8 +71,8 @@ discard block |
||
| 71 | 71 | case Y\SET_VALUE: |
| 72 | 72 | $prop = array_keys(get_object_vars($parent)); |
| 73 | 73 | $key = end($prop); |
| 74 | - if (property_exists($value, "type") && ($value->type & (Y\ITEM | Y\MAPPING))) { |
|
| 75 | - $p = $value->type === Y\ITEM ? [] : new \StdClass; |
|
| 74 | + if (property_exists($value, "type") && ($value->type & (Y\ITEM|Y\MAPPING))) { |
|
| 75 | + $p = $value->type === Y\ITEM ? [] : new \StdClass; |
|
| 76 | 76 | self::build($value, $p); |
| 77 | 77 | } else { |
| 78 | 78 | $p = self::build($value, $parent->{$key}); |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | return; |
| 82 | 82 | case Y\TAG: |
| 83 | 83 | if ($parent === self::$_root) { |
| 84 | - $parent->addTag($identifier);return; |
|
| 84 | + $parent->addTag($identifier); return; |
|
| 85 | 85 | } else {//TODO: have somewhere a list of common tags and their treatment |
| 86 | 86 | if (in_array($identifier, ['!binary', '!str'])) { |
| 87 | 87 | if ($value->value instanceof NodeList) $value->value->type = Y\RAW; |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | if (is_null($identifier)) { |
| 110 | 110 | throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $node->line)); |
| 111 | 111 | } else { |
| 112 | - if ($value instanceof Node && ($value->type & (Y\KEY | Y\ITEM))) { |
|
| 112 | + if ($value instanceof Node && ($value->type & (Y\KEY|Y\ITEM))) { |
|
| 113 | 113 | $parent->{$identifier} = $value->type & Y\KEY ? new \StdClass : []; |
| 114 | 114 | self::build($value, $parent->{$identifier}); |
| 115 | 115 | } elseif (is_object($value)) { |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | |
| 123 | 123 | private static function buildItem($value, &$parent):void |
| 124 | 124 | { |
| 125 | - if(!is_array($parent) && !($parent instanceof \ArrayIterator)) { |
|
| 125 | + if (!is_array($parent) && !($parent instanceof \ArrayIterator)) { |
|
| 126 | 126 | throw new \Exception("parent must be an Iterable not ".(is_object($parent) ? get_class($parent) : gettype($parent)), 1); |
| 127 | 127 | } |
| 128 | 128 | if ($value instanceof Node && $value->type === Y\KEY) { |
@@ -175,8 +175,8 @@ discard block |
||
| 175 | 175 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
| 176 | 176 | } else { |
| 177 | 177 | switch (true) { |
| 178 | - case $isSequence: $list->type = Y\SEQUENCE;break; |
|
| 179 | - case $isSet: $list->type = Y\SET;break; |
|
| 178 | + case $isSequence: $list->type = Y\SEQUENCE; break; |
|
| 179 | + case $isSet: $list->type = Y\SET; break; |
|
| 180 | 180 | case $isMapping://fall through |
| 181 | 181 | default: $list->type = Y\MAPPING; |
| 182 | 182 | } |
@@ -200,10 +200,10 @@ discard block |
||
| 200 | 200 | $children->rewind(); |
| 201 | 201 | $refIndent = $children->current()->indent; |
| 202 | 202 | $separator = $type === Y\RAW ? '' : "\n"; |
| 203 | - $action = function ($c) { return $c->value; }; |
|
| 204 | - if ($type & Y\LITT_FOLDED) { |
|
| 203 | + $action = function($c) { return $c->value; }; |
|
| 204 | + if ($type&Y\LITT_FOLDED) { |
|
| 205 | 205 | $separator = ' '; |
| 206 | - $action = function ($c) use ($refIndent) { |
|
| 206 | + $action = function($c) use ($refIndent) { |
|
| 207 | 207 | return $c->indent > $refIndent || ($c->type & Y\BLANK) ? "\n".$c->value : $c->value; |
| 208 | 208 | }; |
| 209 | 209 | } |
@@ -18,7 +18,9 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | private static function build(object $node, &$parent = null) |
| 20 | 20 | { |
| 21 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
| 21 | + if ($node instanceof NodeList) { |
|
| 22 | + return self::buildNodeList($node, $parent); |
|
| 23 | + } |
|
| 22 | 24 | return self::buildNode($node, $parent); |
| 23 | 25 | } |
| 24 | 26 | |
@@ -60,12 +62,15 @@ discard block |
||
| 60 | 62 | case Y\REF_DEF: //fall through |
| 61 | 63 | case Y\REF_CALL://TODO: self::build returns what ? |
| 62 | 64 | $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
| 63 | - if ($type === Y\REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 65 | + if ($type === Y\REF_DEF) { |
|
| 66 | + self::$_root->addReference($identifier, $tmp); |
|
| 67 | + } |
|
| 64 | 68 | return self::$_root->getReference($identifier); |
| 65 | 69 | case Y\SET_KEY: |
| 66 | 70 | $key = json_encode(self::build($value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 67 | - if (empty($key)) |
|
| 68 | - throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
|
| 71 | + if (empty($key)) { |
|
| 72 | + throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
|
| 73 | + } |
|
| 69 | 74 | $parent->{$key} = null; |
| 70 | 75 | return; |
| 71 | 76 | case Y\SET_VALUE: |
@@ -84,8 +89,11 @@ discard block |
||
| 84 | 89 | $parent->addTag($identifier);return; |
| 85 | 90 | } else {//TODO: have somewhere a list of common tags and their treatment |
| 86 | 91 | if (in_array($identifier, ['!binary', '!str'])) { |
| 87 | - if ($value->value instanceof NodeList) $value->value->type = Y\RAW; |
|
| 88 | - else $value->type = Y\RAW; |
|
| 92 | + if ($value->value instanceof NodeList) { |
|
| 93 | + $value->value->type = Y\RAW; |
|
| 94 | + } else { |
|
| 95 | + $value->type = Y\RAW; |
|
| 96 | + } |
|
| 89 | 97 | } |
| 90 | 98 | $val = is_null($value) ? null : self::build($value, $node); |
| 91 | 99 | return new Tag($identifier, $val); |
@@ -153,10 +161,14 @@ discard block |
||
| 153 | 161 | } |
| 154 | 162 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 155 | 163 | foreach ($_root->value as $child) { |
| 156 | - if ($child->type & Y\DOC_START) $totalDocStart++; |
|
| 164 | + if ($child->type & Y\DOC_START) { |
|
| 165 | + $totalDocStart++; |
|
| 166 | + } |
|
| 157 | 167 | //if 0 or 1 DOC_START = we are still in first document |
| 158 | 168 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 159 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 169 | + if (!isset($documents[$currentDoc])) { |
|
| 170 | + $documents[$currentDoc] = new NodeList(); |
|
| 171 | + } |
|
| 160 | 172 | $documents[$currentDoc]->push($child); |
| 161 | 173 | } |
| 162 | 174 | // $_debug >= 2 && var_dump($documents);//var_dump($documents);die("documents"); |