1717class SetConfig extends Base {
1818 public function __construct (
1919 SystemConfig $ systemConfig ,
20+ private CastHelper $ castHelper ,
2021 ) {
2122 parent ::__construct ($ systemConfig );
2223 }
@@ -57,7 +58,7 @@ protected function configure() {
5758 protected function execute (InputInterface $ input , OutputInterface $ output ): int {
5859 $ configNames = $ input ->getArgument ('name ' );
5960 $ configName = $ configNames [0 ];
60- $ configValue = $ this ->castValue ($ input ->getOption ('value ' ), $ input ->getOption ('type ' ));
61+ $ configValue = $ this ->castHelper -> castValue ($ input ->getOption ('value ' ), $ input ->getOption ('type ' ));
6162 $ updateOnly = $ input ->getOption ('update-only ' );
6263
6364 if (count ($ configNames ) > 1 ) {
@@ -80,80 +81,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8081 return 0 ;
8182 }
8283
83- /**
84- * @param string $value
85- * @param string $type
86- * @return mixed
87- * @throws \InvalidArgumentException
88- */
89- protected function castValue ($ value , $ type ) {
90- switch ($ type ) {
91- case 'integer ' :
92- case 'int ' :
93- if (!is_numeric ($ value )) {
94- throw new \InvalidArgumentException ('Non-numeric value specified ' );
95- }
96- return [
97- 'value ' => (int )$ value ,
98- 'readable-value ' => 'integer ' . (int )$ value ,
99- ];
100-
101- case 'double ' :
102- case 'float ' :
103- if (!is_numeric ($ value )) {
104- throw new \InvalidArgumentException ('Non-numeric value specified ' );
105- }
106- return [
107- 'value ' => (float )$ value ,
108- 'readable-value ' => 'double ' . (float )$ value ,
109- ];
110-
111- case 'boolean ' :
112- case 'bool ' :
113- $ value = strtolower ($ value );
114- switch ($ value ) {
115- case 'true ' :
116- return [
117- 'value ' => true ,
118- 'readable-value ' => 'boolean ' . $ value ,
119- ];
120-
121- case 'false ' :
122- return [
123- 'value ' => false ,
124- 'readable-value ' => 'boolean ' . $ value ,
125- ];
126-
127- default :
128- throw new \InvalidArgumentException ('Unable to parse value as boolean ' );
129- }
130-
131- // no break
132- case 'null ' :
133- return [
134- 'value ' => null ,
135- 'readable-value ' => 'null ' ,
136- ];
137-
138- case 'string ' :
139- $ value = (string )$ value ;
140- return [
141- 'value ' => $ value ,
142- 'readable-value ' => ($ value === '' ) ? 'empty string ' : 'string ' . $ value ,
143- ];
144-
145- case 'json ' :
146- $ value = json_decode ($ value , true );
147- return [
148- 'value ' => $ value ,
149- 'readable-value ' => 'json ' . json_encode ($ value ),
150- ];
151-
152- default :
153- throw new \InvalidArgumentException ('Invalid type ' );
154- }
155- }
156-
15784 /**
15885 * @param array $configNames
15986 * @param mixed $existingValues
0 commit comments