diff --git a/Commands/UpdateYaml.php b/Commands/UpdateYaml.php index f9cef02..194cb43 100644 --- a/Commands/UpdateYaml.php +++ b/Commands/UpdateYaml.php @@ -25,15 +25,12 @@ protected function execute(InputInterface $input, OutputInterface $output) { $file = $input->getArgument('file'); $content = Yaml::parse(file_get_contents($file)); - $type = $input->getOption('value-type'); $value = $input->getArgument('value'); - if (strtolower($type) !== 'mixed') { - if (function_exists($type)) { - $value = $type($value); - } else { - settype($value, $type); - } - } + + $cast = $this->getTypeCast($input->getOption('value-type')); + + $value = $cast($value); + self::set($content, $input->getArgument('node-path'), $value); echo Yaml::dump($content, $input->getOption('inline'), $input->getOption('indent')); } @@ -58,4 +55,37 @@ public static function set(&$array, $key, $value) return $array; } + + /** + * @param string $type + * @return callable + */ + private function getTypeCast($type) + { + switch ($type) { + case 'json': + return function ($x) { + return json_decode($x, true); + }; + break; + case 'yaml': + case 'yml': + return function ($x) { + return Yaml::parse($x); + }; + break; + case 'mixed': + return function ($x) { + return $x; + }; + break; + default: + return function ($x) use ($type) { + settype($x, $type); + + return $x; + }; + break; + } + } } diff --git a/yaml.phar b/yaml.phar index ca8a2ba..35939b9 100644 Binary files a/yaml.phar and b/yaml.phar differ