@@ -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); |
@@ -182,9 +190,13 @@ discard block |
||
| 182 | 190 | self::add(self::dump($obj->value, $indent + self::INDENT), $indent + self::INDENT); |
| 183 | 191 | } |
| 184 | 192 | } |
| 185 | - if ($obj instanceof Compact) return self::dumpCompact($obj, $indent); |
|
| 193 | + if ($obj instanceof Compact) { |
|
| 194 | + return self::dumpCompact($obj, $indent); |
|
| 195 | + } |
|
| 186 | 196 | //TODO: consider dumping datetime as date strings according to a format provided by user or default |
| 187 | - if ($obj instanceof \DateTime) return $obj->format(self::DATE_FORMAT); |
|
| 197 | + if ($obj instanceof \DateTime) { |
|
| 198 | + return $obj->format(self::DATE_FORMAT); |
|
| 199 | + } |
|
| 188 | 200 | $propList = get_object_vars($obj); |
| 189 | 201 | foreach ($propList as $property => $value) { |
| 190 | 202 | 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 | |
@@ -106,7 +106,7 @@ |
||
| 106 | 106 | |
| 107 | 107 | function isOneOf(object $obj, array $comparison) |
| 108 | 108 | { |
| 109 | - if(!is_object($obj) || !is_array($comparison)) { |
|
| 109 | + if (!is_object($obj) || !is_array($comparison)) { |
|
| 110 | 110 | throw new Exception(__FUNCTION__." only object and array are allowed for comparison", 1); |
| 111 | 111 | } |
| 112 | 112 | foreach ($comparison as $className) { |
@@ -111,7 +111,9 @@ |
||
| 111 | 111 | } |
| 112 | 112 | foreach ($comparison as $className) { |
| 113 | 113 | $fqn = __NAMESPACE__."\\$className"; |
| 114 | - if ($obj instanceof $fqn) return true; |
|
| 114 | + if ($obj instanceof $fqn) { |
|
| 115 | + return true; |
|
| 116 | + } |
|
| 115 | 117 | } |
| 116 | 118 | return false; |
| 117 | 119 | } |
| 118 | 120 | \ No newline at end of file |
@@ -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 | //private |
| 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 | /* @var array */ |
@@ -173,8 +173,8 @@ discard block |
||
| 173 | 173 | { |
| 174 | 174 | $deepest = $previous->getDeepestNode(); |
| 175 | 175 | if ($deepest instanceof NodePartial) { |
| 176 | - return $deepest->specialProcess($current, $this->_blankBuffer); |
|
| 177 | - } elseif(!($current instanceof NodePartial)) { |
|
| 176 | + return $deepest->specialProcess($current, $this->_blankBuffer); |
|
| 177 | + } elseif (!($current instanceof NodePartial)) { |
|
| 178 | 178 | return $current->specialProcess($previous, $this->_blankBuffer); |
| 179 | 179 | } |
| 180 | 180 | return false; |
@@ -93,7 +93,9 @@ discard block |
||
| 93 | 93 | { |
| 94 | 94 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
| 95 | 95 | //TODO : be more permissive on $strContent values |
| 96 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 96 | + if (!is_array($source) || !count($source)) { |
|
| 97 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 98 | + } |
|
| 97 | 99 | foreach ($source as $key => $value) { |
| 98 | 100 | yield ++$key => $value; |
| 99 | 101 | } |
@@ -116,7 +118,9 @@ discard block |
||
| 116 | 118 | try { |
| 117 | 119 | foreach ($generator as $lineNb => $lineString) { |
| 118 | 120 | $node = NodeFactory::get($lineString, $lineNb); |
| 119 | - if ($this->needsSpecialProcess($node, $previous)) continue; |
|
| 121 | + if ($this->needsSpecialProcess($node, $previous)) { |
|
| 122 | + continue; |
|
| 123 | + } |
|
| 120 | 124 | $this->attachBlankLines($previous); |
| 121 | 125 | switch ($node->indent <=> $previous->indent) { |
| 122 | 126 | case -1: $target = $previous->getTargetOnLessIndent($node); |
@@ -79,12 +79,12 @@ |
||
| 79 | 79 | */ |
| 80 | 80 | public function checkTypeCoherence($estimatedType):bool |
| 81 | 81 | { |
| 82 | - // if ($this->type === self::MAPPING) { |
|
| 83 | - // if ($estimatedType === self::SEQUENCE) { |
|
| 84 | - // throw new \ParseError("Error : no coherence in types", 1); |
|
| 85 | - // } |
|
| 86 | - // } |
|
| 87 | - return true; |
|
| 82 | + // if ($this->type === self::MAPPING) { |
|
| 83 | + // if ($estimatedType === self::SEQUENCE) { |
|
| 84 | + // throw new \ParseError("Error : no coherence in types", 1); |
|
| 85 | + // } |
|
| 86 | + // } |
|
| 87 | + return true; |
|
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | public function build(&$parent = null) |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | foreach ($tmp as $child) { |
| 48 | 48 | if (!($child instanceof NodeComment) |
| 49 | 49 | && !($child instanceof NodeDirective) |
| 50 | - && !($child instanceof NodeDocstart && is_null($child->value)) ) return true; |
|
| 50 | + && !($child instanceof NodeDocstart && is_null($child->value))) return true; |
|
| 51 | 51 | } |
| 52 | 52 | return false; |
| 53 | 53 | } |
@@ -55,12 +55,12 @@ discard block |
||
| 55 | 55 | public function push($node) |
| 56 | 56 | { |
| 57 | 57 | $type = null; |
| 58 | - if ($node instanceof NodeItem ) $type = self::SEQUENCE; |
|
| 58 | + if ($node instanceof NodeItem) $type = self::SEQUENCE; |
|
| 59 | 59 | elseif ($node instanceof NodeKey) $type = self::MAPPING; |
| 60 | 60 | elseif ($node instanceof NodeSetKey |
| 61 | 61 | || $node instanceof NodeSetValue) { |
| 62 | 62 | $type = self::SET; |
| 63 | - } elseif ($node instanceof NodeScalar ){ |
|
| 63 | + } elseif ($node instanceof NodeScalar) { |
|
| 64 | 64 | $type = self::MULTILINE; |
| 65 | 65 | } |
| 66 | 66 | if (!is_null($type) && $this->checkTypeCoherence($type)) { |
@@ -143,10 +143,10 @@ discard block |
||
| 143 | 143 | if ($child instanceof NodeComment) { |
| 144 | 144 | $child->build(); |
| 145 | 145 | } else { |
| 146 | - if($child->value instanceof NodeComment) { |
|
| 146 | + if ($child->value instanceof NodeComment) { |
|
| 147 | 147 | $child->value->build(); |
| 148 | 148 | $child->value = null; |
| 149 | - } elseif($child->value instanceof NodeList) { |
|
| 149 | + } elseif ($child->value instanceof NodeList) { |
|
| 150 | 150 | $child->value = $child->value->filterComment(); |
| 151 | 151 | } |
| 152 | 152 | $out->push($child); |
@@ -35,7 +35,9 @@ discard block |
||
| 35 | 35 | $tmp->rewind(); |
| 36 | 36 | $fqn = __NAMESPACE__."\\$nodeType"; |
| 37 | 37 | foreach ($tmp as $child) { |
| 38 | - if ($child instanceof $fqn) return true; |
|
| 38 | + if ($child instanceof $fqn) { |
|
| 39 | + return true; |
|
| 40 | + } |
|
| 39 | 41 | } |
| 40 | 42 | return false; |
| 41 | 43 | } |
@@ -47,7 +49,9 @@ discard block |
||
| 47 | 49 | foreach ($tmp as $child) { |
| 48 | 50 | if (!($child instanceof NodeComment) |
| 49 | 51 | && !($child instanceof NodeDirective) |
| 50 | - && !($child instanceof NodeDocstart && is_null($child->value)) ) return true; |
|
| 52 | + && !($child instanceof NodeDocstart && is_null($child->value)) ) { |
|
| 53 | + return true; |
|
| 54 | + } |
|
| 51 | 55 | } |
| 52 | 56 | return false; |
| 53 | 57 | } |
@@ -55,9 +59,11 @@ discard block |
||
| 55 | 59 | public function push($node) |
| 56 | 60 | { |
| 57 | 61 | $type = null; |
| 58 | - if ($node instanceof NodeItem ) $type = self::SEQUENCE; |
|
| 59 | - elseif ($node instanceof NodeKey) $type = self::MAPPING; |
|
| 60 | - elseif ($node instanceof NodeSetKey |
|
| 62 | + if ($node instanceof NodeItem ) { |
|
| 63 | + $type = self::SEQUENCE; |
|
| 64 | + } elseif ($node instanceof NodeKey) { |
|
| 65 | + $type = self::MAPPING; |
|
| 66 | + } elseif ($node instanceof NodeSetKey |
|
| 61 | 67 | || $node instanceof NodeSetValue) { |
| 62 | 68 | $type = self::SET; |
| 63 | 69 | } elseif ($node instanceof NodeScalar ){ |
@@ -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, object $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); |
@@ -23,7 +23,9 @@ |
||
| 23 | 23 | |
| 24 | 24 | public function add(Node $child):Node |
| 25 | 25 | { |
| 26 | - if (is_null($this->value)) $this->value = new NodeList(); |
|
| 26 | + if (is_null($this->value)) { |
|
| 27 | + $this->value = new NodeList(); |
|
| 28 | + } |
|
| 27 | 29 | $candidate = $child; |
| 28 | 30 | if (!isOneOf($child, ['NodeScalar', 'NodeBlank', 'NodeComment', 'NodeQuoted'])) { |
| 29 | 31 | $candidate = new NodeScalar($child->raw, $child->line); |
@@ -28,7 +28,7 @@ |
||
| 28 | 28 | if ($value instanceof NodeKey && $child instanceof NodeKey) { |
| 29 | 29 | if ($value->indent === $child->indent) { |
| 30 | 30 | return parent::add($child); |
| 31 | - } elseif ($value->isAwaitingChild($child)){ |
|
| 31 | + } elseif ($value->isAwaitingChild($child)) { |
|
| 32 | 32 | return $value->add($child); |
| 33 | 33 | } else { |
| 34 | 34 | throw new \ParseError('key ('.$value->identifier.')@'.$value->line.' has already a value', 1); |
@@ -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 |