@@ -21,7 +21,7 @@ |
||
| 21 | 21 | // } else { |
| 22 | 22 | // $this->specialProcess($previous, $emptyLines); |
| 23 | 23 | // } |
| 24 | - if ($previous instanceof NodeScalar) $emptyLines[] = $this->setParent($previous->getParent()); |
|
| 24 | + if ($previous instanceof NodeScalar) $emptyLines[] = $this->setParent($previous->getParent()); |
|
| 25 | 25 | if ($deepest instanceof NodeLiterals) $emptyLines[] = $this->setParent($deepest); |
| 26 | 26 | return true; |
| 27 | 27 | } |
@@ -21,8 +21,12 @@ |
||
| 21 | 21 | // } else { |
| 22 | 22 | // $this->specialProcess($previous, $emptyLines); |
| 23 | 23 | // } |
| 24 | - if ($previous instanceof NodeScalar) $emptyLines[] = $this->setParent($previous->getParent()); |
|
| 25 | - if ($deepest instanceof NodeLiterals) $emptyLines[] = $this->setParent($deepest); |
|
| 24 | + if ($previous instanceof NodeScalar) { |
|
| 25 | + $emptyLines[] = $this->setParent($previous->getParent()); |
|
| 26 | + } |
|
| 27 | + if ($deepest instanceof NodeLiterals) { |
|
| 28 | + $emptyLines[] = $this->setParent($deepest); |
|
| 29 | + } |
|
| 26 | 30 | return true; |
| 27 | 31 | } |
| 28 | 32 | |
@@ -28,7 +28,9 @@ |
||
| 28 | 28 | public static function buildReference($node, $parent) |
| 29 | 29 | { |
| 30 | 30 | $tmp = is_null($node->value) ? null : $node->value->build($parent); |
| 31 | - if ($node instanceof NodeRefDef) Builder::$_root->addReference($node->identifier, $tmp); |
|
| 31 | + if ($node instanceof NodeRefDef) { |
|
| 32 | + Builder::$_root->addReference($node->identifier, $tmp); |
|
| 33 | + } |
|
| 32 | 34 | return Builder::$_root->getReference($node->identifier); |
| 33 | 35 | } |
| 34 | 36 | } |
| 35 | 37 | \ No newline at end of file |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | { |
| 40 | 40 | $this->raw = $nodeString; |
| 41 | 41 | $this->line = (int) $line; |
| 42 | - //permissive to tabs but replacement |
|
| 42 | + //permissive to tabs but replacement |
|
| 43 | 43 | $nodeValue = preg_replace("/^\t+/m", " ", $nodeString); |
| 44 | 44 | $this->indent = strspn($nodeValue, ' '); |
| 45 | 45 | // $this->value = $nodeString; |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | return $previous->getParent(); |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | - /** |
|
| 176 | + /** |
|
| 177 | 177 | * Modify parent target when current Node indentation is superior to previous node indentation |
| 178 | 178 | * |
| 179 | 179 | * @param Node $previous The previous |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | return false; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - /** |
|
| 198 | + /** |
|
| 199 | 199 | * According to the current Node type and deepest value |
| 200 | 200 | * this indicates if self::parse skips (or not) the parent and previous assignment |
| 201 | 201 | * |
@@ -68,7 +68,9 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function getParent(int $indent = null, $type = 0):Node |
| 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 |
| 74 | 76 | && $cursor->indent !== $indent |
@@ -212,11 +214,15 @@ discard block |
||
| 212 | 214 | |
| 213 | 215 | public function getValue(&$parent = null) |
| 214 | 216 | { |
| 215 | - if (is_null($this->value)) return null; |
|
| 216 | - elseif (is_string($this->value)) return $this->getScalar($this->value); |
|
| 217 | - elseif ($this->value instanceof Node) return $this->value->getValue($parent); |
|
| 218 | - elseif ($this->value instanceof NodeList) return Builder::buildNodeList($this->value, $parent); |
|
| 219 | - else { |
|
| 217 | + if (is_null($this->value)) { |
|
| 218 | + return null; |
|
| 219 | + } elseif (is_string($this->value)) { |
|
| 220 | + return $this->getScalar($this->value); |
|
| 221 | + } elseif ($this->value instanceof Node) { |
|
| 222 | + return $this->value->getValue($parent); |
|
| 223 | + } elseif ($this->value instanceof NodeList) { |
|
| 224 | + return Builder::buildNodeList($this->value, $parent); |
|
| 225 | + } else { |
|
| 220 | 226 | throw new \ParseError("Error trying to getValue of ".gettype($this->value)); |
| 221 | 227 | } |
| 222 | 228 | } |
@@ -231,8 +237,12 @@ discard block |
||
| 231 | 237 | */ |
| 232 | 238 | public static function getScalar(string $v) |
| 233 | 239 | { |
| 234 | - if (Regex::isDate($v)) return self::$dateAsString ? $v : date_create($v); |
|
| 235 | - if (Regex::isNumber($v)) return self::getNumber($v); |
|
| 240 | + if (Regex::isDate($v)) { |
|
| 241 | + return self::$dateAsString ? $v : date_create($v); |
|
| 242 | + } |
|
| 243 | + if (Regex::isNumber($v)) { |
|
| 244 | + return self::getNumber($v); |
|
| 245 | + } |
|
| 236 | 246 | $types = ['yes' => true, |
| 237 | 247 | 'no' => false, |
| 238 | 248 | 'true' => true, |
@@ -255,8 +265,12 @@ discard block |
||
| 255 | 265 | */ |
| 256 | 266 | private static function getNumber(string $v) |
| 257 | 267 | { |
| 258 | - if (preg_match(Regex::OCTAL_NUM, $v)) return intval(base_convert($v, 8, 10)); |
|
| 259 | - if (preg_match(Regex::HEX_NUM, $v)) return intval(base_convert($v, 16, 10)); |
|
| 268 | + if (preg_match(Regex::OCTAL_NUM, $v)) { |
|
| 269 | + return intval(base_convert($v, 8, 10)); |
|
| 270 | + } |
|
| 271 | + if (preg_match(Regex::HEX_NUM, $v)) { |
|
| 272 | + return intval(base_convert($v, 16, 10)); |
|
| 273 | + } |
|
| 260 | 274 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
| 261 | 275 | } |
| 262 | 276 | |
@@ -288,7 +302,9 @@ discard block |
||
| 288 | 302 | 'value' => $this->value, |
| 289 | 303 | 'raw' => $this->raw, |
| 290 | 304 | ]; |
| 291 | - if ($this->identifier) $props['identifier'] = "($this->identifier)"; |
|
| 305 | + if ($this->identifier) { |
|
| 306 | + $props['identifier'] = "($this->identifier)"; |
|
| 307 | + } |
|
| 292 | 308 | return $props; |
| 293 | 309 | } |
| 294 | 310 | } |
@@ -46,9 +46,9 @@ |
||
| 46 | 46 | public function __call($funcName, $arguments) |
| 47 | 47 | { |
| 48 | 48 | $reflectAPI = new \ReflectionClass(get_class($this->__yaml__object__api)); |
| 49 | - $getName = function ($o) { return $o->name; }; |
|
| 49 | + $getName = function($o) { return $o->name; }; |
|
| 50 | 50 | $publicApi = array_map($getName, $reflectAPI->getMethods(\ReflectionMethod::IS_PUBLIC)); |
| 51 | - if (!in_array($funcName, $publicApi) ) { |
|
| 51 | + if (!in_array($funcName, $publicApi)) { |
|
| 52 | 52 | throw new \BadMethodCallException(sprintf(self::UNDEFINED_METHOD, $funcName, implode(",", $publicApi))); |
| 53 | 53 | } |
| 54 | 54 | return call_user_func_array([$this->__yaml__object__api, $funcName], $arguments); |
@@ -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 | } |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | foreach ($tmp as $child) { |
| 41 | 41 | if (!($child instanceof NodeComment) |
| 42 | 42 | && !($child instanceof NodeDirective) |
| 43 | - && !($child instanceof NodeDocstart && is_null($child->value)) ) return true; |
|
| 43 | + && !($child instanceof NodeDocstart && is_null($child->value))) return true; |
|
| 44 | 44 | } |
| 45 | 45 | return false; |
| 46 | 46 | } |
@@ -28,7 +28,9 @@ discard block |
||
| 28 | 28 | $tmp = clone $this; |
| 29 | 29 | $tmp->rewind(); |
| 30 | 30 | foreach ($tmp as $child) { |
| 31 | - if (is_a($child, $nodeType)) return true; |
|
| 31 | + if (is_a($child, $nodeType)) { |
|
| 32 | + return true; |
|
| 33 | + } |
|
| 32 | 34 | } |
| 33 | 35 | return false; |
| 34 | 36 | } |
@@ -40,7 +42,9 @@ discard block |
||
| 40 | 42 | foreach ($tmp as $child) { |
| 41 | 43 | if (!($child instanceof NodeComment) |
| 42 | 44 | && !($child instanceof NodeDirective) |
| 43 | - && !($child instanceof NodeDocstart && is_null($child->value)) ) return true; |
|
| 45 | + && !($child instanceof NodeDocstart && is_null($child->value)) ) { |
|
| 46 | + return true; |
|
| 47 | + } |
|
| 44 | 48 | } |
| 45 | 49 | return false; |
| 46 | 50 | } |
@@ -15,10 +15,10 @@ discard block |
||
| 15 | 15 | //public |
| 16 | 16 | /* @var null|string */ |
| 17 | 17 | public static $error; |
| 18 | - public const IGNORE_DIRECTIVES = 1;//DONT include_directive |
|
| 19 | - public const IGNORE_COMMENTS = 2;//DONT include_comments |
|
| 20 | - public const NO_PARSING_EXCEPTIONS = 4;//DONT throw Exception on parsing errors |
|
| 21 | - public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
|
| 18 | + public const IGNORE_DIRECTIVES = 1; //DONT include_directive |
|
| 19 | + public const IGNORE_COMMENTS = 2; //DONT include_comments |
|
| 20 | + public const NO_PARSING_EXCEPTIONS = 4; //DONT throw Exception on parsing errors |
|
| 21 | + public const NO_OBJECT_FOR_DATE = 8; //DONT import date strings as dateTime Object |
|
| 22 | 22 | |
| 23 | 23 | //privates |
| 24 | 24 | /* @var null|false|array */ |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | /* @var null|string */ |
| 27 | 27 | private $filePath; |
| 28 | 28 | /* @var integer */ |
| 29 | - private $debug = 0;///TODO: determine levels |
|
| 29 | + private $debug = 0; ///TODO: determine levels |
|
| 30 | 30 | /* @var integer */ |
| 31 | 31 | private $options = 0; |
| 32 | 32 | |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
| 93 | 93 | //TODO : be more permissive on $strContent values |
| 94 | 94 | if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
| 95 | - return function () use($source) { |
|
| 95 | + return function() use($source) { |
|
| 96 | 96 | foreach ($source as $key => $value) { |
| 97 | 97 | yield ++$key => $value; |
| 98 | 98 | } |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | break; |
| 130 | 130 | default: $target = $node->getTargetOnMoreIndent($previous); |
| 131 | 131 | } |
| 132 | - if ($node->skipOnContext($target)) continue;//var_dump(get_class($target)); |
|
| 132 | + if ($node->skipOnContext($target)) continue; //var_dump(get_class($target)); |
|
| 133 | 133 | $target->add($node); |
| 134 | 134 | $previous = $node; |
| 135 | 135 | } |
@@ -91,7 +91,9 @@ discard block |
||
| 91 | 91 | { |
| 92 | 92 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
| 93 | 93 | //TODO : be more permissive on $strContent values |
| 94 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 94 | + if (!is_array($source) || !count($source)) { |
|
| 95 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 96 | + } |
|
| 95 | 97 | return function () use($source) { |
| 96 | 98 | foreach ($source as $key => $value) { |
| 97 | 99 | yield ++$key => $value; |
@@ -119,7 +121,9 @@ discard block |
||
| 119 | 121 | foreach ($sourceIterator as $lineNb => $lineString) { |
| 120 | 122 | $currentLine = $lineNb; |
| 121 | 123 | $node = NodeFactory::get($lineString, $lineNb); |
| 122 | - if ($node->needsSpecialProcess($previous, $emptyLines)) continue; |
|
| 124 | + if ($node->needsSpecialProcess($previous, $emptyLines)) { |
|
| 125 | + continue; |
|
| 126 | + } |
|
| 123 | 127 | $this->attachBlankLines($emptyLines, $previous); |
| 124 | 128 | $emptyLines = []; |
| 125 | 129 | switch ($node->indent <=> $previous->indent) { |
@@ -129,11 +133,16 @@ discard block |
||
| 129 | 133 | break; |
| 130 | 134 | default: $target = $node->getTargetOnMoreIndent($previous); |
| 131 | 135 | } |
| 132 | - if ($node->skipOnContext($target)) continue;//var_dump(get_class($target)); |
|
| 136 | + if ($node->skipOnContext($target)) { |
|
| 137 | + continue; |
|
| 138 | + } |
|
| 139 | + //var_dump(get_class($target)); |
|
| 133 | 140 | $target->add($node); |
| 134 | 141 | $previous = $node; |
| 135 | 142 | } |
| 136 | - if ($this->debug === 2) print_r($root); |
|
| 143 | + if ($this->debug === 2) { |
|
| 144 | + print_r($root); |
|
| 145 | + } |
|
| 137 | 146 | return Builder::buildContent($root, $this->debug); |
| 138 | 147 | } catch (\Error|\Exception|\ParseError $e) { |
| 139 | 148 | $file = $this->filePath ? realpath($this->filePath) : '#YAML STRING#'; |
@@ -1,11 +1,11 @@ |
||
| 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; |
| 7 | 7 | |
| 8 | -$fileName = __DIR__ . '/../dummy.yml'; |
|
| 8 | +$fileName = __DIR__.'/../dummy.yml'; |
|
| 9 | 9 | |
| 10 | 10 | $yamlObject = Yaml::parseFile($fileName, $options = null, $debug = null); |
| 11 | 11 | |