Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr Myrza committed Aug 16, 2017
1 parent e3d6b31 commit 0b097f1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions Commands/UpdateYaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand All @@ -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;
}
}
}
Binary file modified yaml.phar
Binary file not shown.

0 comments on commit 0b097f1

Please sign in to comment.