Skip to content

Commit 0b097f1

Browse files
author
Volodymyr Myrza
committed
fix types
1 parent e3d6b31 commit 0b097f1

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

Commands/UpdateYaml.php

+38-8
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
2525
{
2626
$file = $input->getArgument('file');
2727
$content = Yaml::parse(file_get_contents($file));
28-
$type = $input->getOption('value-type');
2928
$value = $input->getArgument('value');
30-
if (strtolower($type) !== 'mixed') {
31-
if (function_exists($type)) {
32-
$value = $type($value);
33-
} else {
34-
settype($value, $type);
35-
}
36-
}
29+
30+
$cast = $this->getTypeCast($input->getOption('value-type'));
31+
32+
$value = $cast($value);
33+
3734
self::set($content, $input->getArgument('node-path'), $value);
3835
echo Yaml::dump($content, $input->getOption('inline'), $input->getOption('indent'));
3936
}
@@ -58,4 +55,37 @@ public static function set(&$array, $key, $value)
5855

5956
return $array;
6057
}
58+
59+
/**
60+
* @param string $type
61+
* @return callable
62+
*/
63+
private function getTypeCast($type)
64+
{
65+
switch ($type) {
66+
case 'json':
67+
return function ($x) {
68+
return json_decode($x, true);
69+
};
70+
break;
71+
case 'yaml':
72+
case 'yml':
73+
return function ($x) {
74+
return Yaml::parse($x);
75+
};
76+
break;
77+
case 'mixed':
78+
return function ($x) {
79+
return $x;
80+
};
81+
break;
82+
default:
83+
return function ($x) use ($type) {
84+
settype($x, $type);
85+
86+
return $x;
87+
};
88+
break;
89+
}
90+
}
6191
}

yaml.phar

652 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)