@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | foreach ($array as $key => $item) { |
| 144 | 144 | $lineStart = current($refKeys) === $key ? "- " : "- $key: "; |
| 145 | 145 | if (is_scalar($item)) { |
| 146 | - self::add($lineStart.self::dump($item,0), $indent); |
|
| 146 | + self::add($lineStart.self::dump($item, 0), $indent); |
|
| 147 | 147 | } else { |
| 148 | 148 | self::add(rtrim($lineStart), $indent); |
| 149 | 149 | self::dump($item, $indent + self::INDENT); |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | if (is_array($subject) || $subject instanceof \ArrayIterator) { |
| 214 | 214 | $max = count($subject); |
| 215 | 215 | $objectAsArray = is_array($subject) ? $subject : $subject->getArrayCopy(); |
| 216 | - if(array_keys($objectAsArray) !== range(0, $max-1)) { |
|
| 216 | + if (array_keys($objectAsArray) !== range(0, $max - 1)) { |
|
| 217 | 217 | $pairs = $objectAsArray; |
| 218 | 218 | } else { |
| 219 | 219 | $valuesList = []; |
@@ -41,7 +41,9 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | public static function toString($dataType, int $options = null):string |
| 43 | 43 | { |
| 44 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml"); |
|
| 44 | + if (is_null($dataType)) { |
|
| 45 | + throw new \Exception(self::class.": No content to convert to Yaml"); |
|
| 46 | + } |
|
| 45 | 47 | self::$options = is_int($options) ? $options : self::OPTIONS; |
| 46 | 48 | self::$result = new DLL; |
| 47 | 49 | if ($dataType instanceof YamlObject) { |
@@ -83,8 +85,12 @@ discard block |
||
| 83 | 85 | private static function dump($dataType, int $indent) |
| 84 | 86 | { |
| 85 | 87 | if (is_scalar($dataType)) { |
| 86 | - if ($dataType === \INF) return '.inf'; |
|
| 87 | - if ($dataType === -\INF) return '-.inf'; |
|
| 88 | + if ($dataType === \INF) { |
|
| 89 | + return '.inf'; |
|
| 90 | + } |
|
| 91 | + if ($dataType === -\INF) { |
|
| 92 | + return '-.inf'; |
|
| 93 | + } |
|
| 88 | 94 | switch (gettype($dataType)) { |
| 89 | 95 | case 'boolean': return $dataType ? 'true' : 'false'; |
| 90 | 96 | case 'float': //fall through |
@@ -106,7 +112,9 @@ discard block |
||
| 106 | 112 | */ |
| 107 | 113 | private static function dumpYamlObject(YamlObject $obj) |
| 108 | 114 | { |
| 109 | - if ($obj->hasDocStart() && self::$result instanceof DLL) self::$result->push("---"); |
|
| 115 | + if ($obj->hasDocStart() && self::$result instanceof DLL) { |
|
| 116 | + self::$result->push("---"); |
|
| 117 | + } |
|
| 110 | 118 | // self::dump($obj, 0); |
| 111 | 119 | if (count($obj) > 0) { |
| 112 | 120 | self::dumpArray($obj->getArrayCopy(), 0); |
@@ -181,9 +189,13 @@ discard block |
||
| 181 | 189 | self::add(self::dump($obj->value, $indent + self::INDENT), $indent + self::INDENT); |
| 182 | 190 | } |
| 183 | 191 | } |
| 184 | - if ($obj instanceof Compact) return self::dumpCompact($obj, $indent); |
|
| 192 | + if ($obj instanceof Compact) { |
|
| 193 | + return self::dumpCompact($obj, $indent); |
|
| 194 | + } |
|
| 185 | 195 | //TODO: consider dumping datetime as date strings according to a format provided by user or default |
| 186 | - if ($obj instanceof \DateTime) return $obj->format(self::DATE_FORMAT); |
|
| 196 | + if ($obj instanceof \DateTime) { |
|
| 197 | + return $obj->format(self::DATE_FORMAT); |
|
| 198 | + } |
|
| 187 | 199 | $propList = get_object_vars($obj); |
| 188 | 200 | foreach ($propList as $property => $value) { |
| 189 | 201 | if (is_scalar($value) || $value instanceof Compact || $value instanceof \DateTime) { |
@@ -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 | |
@@ -15,12 +15,12 @@ |
||
| 15 | 15 | private const NO_NAME = '%s Error: a tag MUST have a name'; |
| 16 | 16 | private const WRONG_VALUE = "Error : cannot transform tag '%s' for type '%s'"; |
| 17 | 17 | private const LEGACY_TAGS_HANDLERS = ['!!str' => 'strHandler', |
| 18 | - '!!binary' => 'binaryHandler', |
|
| 19 | - '!set' => 'setHandler', |
|
| 20 | - '!!omap' => 'omapHandler', |
|
| 21 | - '!php/object' => 'symfonyPHPobjectHandler', |
|
| 22 | - '!inline' => 'inlineHandler', |
|
| 23 | - ]; |
|
| 18 | + '!!binary' => 'binaryHandler', |
|
| 19 | + '!set' => 'setHandler', |
|
| 20 | + '!!omap' => 'omapHandler', |
|
| 21 | + '!php/object' => 'symfonyPHPobjectHandler', |
|
| 22 | + '!inline' => 'inlineHandler', |
|
| 23 | + ]; |
|
| 24 | 24 | |
| 25 | 25 | public static $registeredHandlers = []; |
| 26 | 26 | |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | { |
| 34 | 34 | $reflectAPI = new \ReflectionClass(self::class); |
| 35 | 35 | $methodsList = []; |
| 36 | - $list = $reflectAPI->getMethods(RM::IS_FINAL | RM::IS_STATIC & RM::IS_PRIVATE); |
|
| 36 | + $list = $reflectAPI->getMethods(RM::IS_FINAL|RM::IS_STATIC & RM::IS_PRIVATE); |
|
| 37 | 37 | foreach ($list as $method) { |
| 38 | 38 | $methodsList[$method->name] = $method->getClosure(); |
| 39 | 39 | } |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | foreach ($node as $key => $child) { |
| 104 | 104 | $list[] = self::strHandler($child)->raw; |
| 105 | 105 | } |
| 106 | - return new NodeScalar(implode('',$list), 0); |
|
| 106 | + return new NodeScalar(implode('', $list), 0); |
|
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | 109 | |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | public static function transform(string $identifier, $value) |
| 171 | 171 | { |
| 172 | 172 | if (self::isKnown($identifier)) { |
| 173 | - if (!($value instanceof Node) && !($value instanceof NodeList) ) { |
|
| 173 | + if (!($value instanceof Node) && !($value instanceof NodeList)) { |
|
| 174 | 174 | throw new \Exception(sprintf(self::WRONG_VALUE, $identifier, gettype($value))); |
| 175 | 175 | } |
| 176 | 176 | return self::$registeredHandlers[$identifier]($value); |
@@ -10,16 +10,16 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class NodeComment extends Node |
| 12 | 12 | { |
| 13 | - public function specialProcess(Node &$previous, array &$emptyLines):bool |
|
| 14 | - { |
|
| 13 | + public function specialProcess(Node &$previous, array &$emptyLines):bool |
|
| 14 | + { |
|
| 15 | 15 | $previous->getRoot()->add($this); |
| 16 | 16 | return true; |
| 17 | - } |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - public function build(&$parent = null) |
|
| 20 | - { |
|
| 19 | + public function build(&$parent = null) |
|
| 20 | + { |
|
| 21 | 21 | $root = $this->getRoot(); |
| 22 | 22 | $yamlObject = $root->getYamlObject(); |
| 23 | 23 | $yamlObject->addComment($this->line, $this->raw); |
| 24 | - } |
|
| 24 | + } |
|
| 25 | 25 | } |
| 26 | 26 | \ No newline at end of file |
@@ -13,7 +13,7 @@ |
||
| 13 | 13 | public function __construct(string $nodeString, int $line) |
| 14 | 14 | { |
| 15 | 15 | parent::__construct($nodeString, $line); |
| 16 | - preg_match_all(Regex::SEQUENCE_VALUES, trim(substr(trim($nodeString), 1,-1)), $matches); |
|
| 16 | + preg_match_all(Regex::SEQUENCE_VALUES, trim(substr(trim($nodeString), 1, -1)), $matches); |
|
| 17 | 17 | foreach ($matches['item'] as $key => $item) { |
| 18 | 18 | $i = new NodeItem('', $line); |
| 19 | 19 | $i->indent = null; |
@@ -12,6 +12,6 @@ |
||
| 12 | 12 | { |
| 13 | 13 | public function build(&$parent = null) |
| 14 | 14 | { |
| 15 | - return substr(trim($this->raw), 1,-1); |
|
| 15 | + return substr(trim($this->raw), 1, -1); |
|
| 16 | 16 | } |
| 17 | 17 | } |
| 18 | 18 | \ No newline at end of file |
@@ -29,7 +29,7 @@ |
||
| 29 | 29 | public function build(&$parent = null) |
| 30 | 30 | { |
| 31 | 31 | $built = is_null($this->value) ? null : $this->value->build(); |
| 32 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
| 32 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
| 33 | 33 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 34 | 34 | if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($this->value, true)); |
| 35 | 35 | $parent->{trim($key, '\'" ')} = null; |
@@ -31,7 +31,9 @@ |
||
| 31 | 31 | $built = is_null($this->value) ? null : $this->value->build(); |
| 32 | 32 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
| 33 | 33 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 34 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($this->value, true)); |
|
| 34 | + if (empty($key)) { |
|
| 35 | + throw new \Exception("Cant serialize complex key: ".var_export($this->value, true)); |
|
| 36 | + } |
|
| 35 | 37 | $parent->{trim($key, '\'" ')} = null; |
| 36 | 38 | } |
| 37 | 39 | |
@@ -43,7 +43,7 @@ |
||
| 43 | 43 | |
| 44 | 44 | public function getTargetOnLessIndent(Node &$node):Node |
| 45 | 45 | { |
| 46 | - if ($node instanceof NodeScalar || $node instanceof NodeBlank ) { |
|
| 46 | + if ($node instanceof NodeScalar || $node instanceof NodeBlank) { |
|
| 47 | 47 | return $this->getParent(); |
| 48 | 48 | } else { |
| 49 | 49 | return $this->getParent($node->indent); |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | return false; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - /** |
|
| 133 | + /** |
|
| 134 | 134 | * Find parent target when current Node indentation is lesser than previous node indentation |
| 135 | 135 | * |
| 136 | 136 | * @param Node $previous The previous |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | return $this->getParent(); |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | - /** |
|
| 169 | + /** |
|
| 170 | 170 | * Find parent target when current Node indentation is superior than previous node indentation |
| 171 | 171 | * |
| 172 | 172 | * @param Node $previous The previous |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | */ |
| 66 | 66 | public function getParent(int $indent = null):Node |
| 67 | 67 | { |
| 68 | - if (!is_int($indent)){ |
|
| 68 | + if (!is_int($indent)) { |
|
| 69 | 69 | if ($this->_parent instanceof Node) { |
| 70 | 70 | return $this->_parent; |
| 71 | 71 | } else { |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | if ($this->tag) $props['tag'] = "($this->tag)"; |
| 216 | 216 | if ($this->value) $props['value'] = $this->value; |
| 217 | 217 | // $props['value'] = $this->value; |
| 218 | - $props['raw'] = $this->raw; |
|
| 218 | + $props['raw'] = $this->raw; |
|
| 219 | 219 | if (!$this->_parent) $props['parent'] = 'NO PARENT!!!'; |
| 220 | 220 | return $props; |
| 221 | 221 | } |
@@ -210,13 +210,23 @@ |
||
| 210 | 210 | { |
| 211 | 211 | $props = []; |
| 212 | 212 | $props['line->indent'] = "$this->line -> $this->indent"; |
| 213 | - if ($this->identifier) $props['identifier'] = "($this->identifier)"; |
|
| 214 | - if ($this->anchor) $props['anchor'] = "($this->anchor)"; |
|
| 215 | - if ($this->tag) $props['tag'] = "($this->tag)"; |
|
| 216 | - if ($this->value) $props['value'] = $this->value; |
|
| 213 | + if ($this->identifier) { |
|
| 214 | + $props['identifier'] = "($this->identifier)"; |
|
| 215 | + } |
|
| 216 | + if ($this->anchor) { |
|
| 217 | + $props['anchor'] = "($this->anchor)"; |
|
| 218 | + } |
|
| 219 | + if ($this->tag) { |
|
| 220 | + $props['tag'] = "($this->tag)"; |
|
| 221 | + } |
|
| 222 | + if ($this->value) { |
|
| 223 | + $props['value'] = $this->value; |
|
| 224 | + } |
|
| 217 | 225 | // $props['value'] = $this->value; |
| 218 | 226 | $props['raw'] = $this->raw; |
| 219 | - if (!$this->_parent) $props['parent'] = 'NO PARENT!!!'; |
|
| 227 | + if (!$this->_parent) { |
|
| 228 | + $props['parent'] = 'NO PARENT!!!'; |
|
| 229 | + } |
|
| 220 | 230 | return $props; |
| 221 | 231 | } |
| 222 | 232 | } |