20
20
use function array_fill_keys ;
21
21
use function array_keys ;
22
22
use function array_map ;
23
+ use function array_merge ;
23
24
use function implode ;
25
+ use function is_array ;
24
26
use function is_string ;
25
27
use function preg_match ;
26
28
use function sprintf ;
31
33
*/
32
34
final class InputOptionsSerializer
33
35
{
34
- private const string ESCAPE_TOKEN_PATTERN = '/[\s\W]/ ' ;
35
-
36
36
private function __construct ()
37
37
{
38
38
}
@@ -52,86 +52,43 @@ public static function serialize(
52
52
array_fill_keys ($ excludedOptionNames , '' ),
53
53
);
54
54
55
- return array_map (
56
- static fn (string $ name ) => self ::serializeOption (
57
- $ commandDefinition ->getOption ($ name ),
58
- $ name ,
59
- $ filteredOptions [$ name ],
60
- ),
61
- array_keys ($ filteredOptions ),
62
- );
55
+ $ serializedOptions = [];
56
+
57
+ foreach (array_keys ($ filteredOptions ) as $ optionName ) {
58
+ $ serializedOption = self ::serializeOption (
59
+ $ commandDefinition ->getOption ($ optionName ),
60
+ $ optionName ,
61
+ $ filteredOptions [$ optionName ],
62
+ );
63
+
64
+ $ serializedOptions [] = is_array ($ serializedOption ) ? $ serializedOption : [$ serializedOption ];
65
+ }
66
+
67
+ return array_merge (...$ serializedOptions );
63
68
}
64
69
65
70
/**
66
71
* @param string|bool|int|float|null|array<string|bool|int|float|null> $value
72
+ *
73
+ * @return string|list<string>
67
74
*/
68
75
private static function serializeOption (
69
76
InputOption $ option ,
70
77
string $ name ,
71
78
array |bool |float |int |string |null $ value ,
72
- ): string {
73
- if ($ option ->isNegatable ()) {
74
- return sprintf (
75
- '--%s%s ' ,
76
- $ value ? '' : 'no- ' ,
77
- $ name ,
78
- );
79
- }
80
-
81
- if (!$ option ->acceptValue ()) {
82
- return sprintf (
83
- '--%s ' ,
84
- $ name ,
85
- );
86
- }
87
-
88
- if ($ option ->isArray ()) {
89
- /** @var array<string|bool|int|float|null> $value */
90
- return implode (
91
- '' ,
92
- array_map (
93
- static fn ($ item ) => self ::serializeOptionWithValue ($ name , $ item ),
94
- $ value ,
95
- ),
96
- );
97
- }
98
-
99
- /** @var string|bool|int|float|null $value */
100
- return self ::serializeOptionWithValue ($ name , $ value );
79
+ ): string |array {
80
+ return match (true ) {
81
+ $ option ->isNegatable () => sprintf ('--%s%s ' , $ value ? '' : 'no- ' , $ name ),
82
+ !$ option ->acceptValue () => sprintf ('--%s ' , $ name ),
83
+ $ option ->isArray () => array_map (fn ($ item ) => self ::serializeOptionWithValue ($ name , $ item ), $ value ),
84
+ default => self ::serializeOptionWithValue ($ name , $ value ),
85
+ };
101
86
}
102
87
103
88
private static function serializeOptionWithValue (
104
89
string $ name ,
105
90
bool |float |int |string |null $ value
106
91
): string {
107
- return sprintf (
108
- '--%s=%s ' ,
109
- $ name ,
110
- self ::quoteOptionValue ($ value ),
111
- );
112
- }
113
-
114
- /**
115
- * Ensure that an option value is quoted correctly before it is passed to a
116
- * child process.
117
- */
118
- private static function quoteOptionValue (bool |float |int |string |null $ value ): bool |float |int |string |null
119
- {
120
- if (self ::isValueRequiresQuoting ($ value )) {
121
- return sprintf (
122
- '"%s" ' ,
123
- str_replace ('" ' , '\" ' , (string ) $ value ),
124
- );
125
- }
126
-
127
- return $ value ;
128
- }
129
-
130
- /**
131
- * Validate whether a command option requires quoting.
132
- */
133
- private static function isValueRequiresQuoting (mixed $ value ): bool
134
- {
135
- return is_string ($ value ) && 0 < preg_match (self ::ESCAPE_TOKEN_PATTERN , $ value );
92
+ return sprintf ('--%s=%s ' , $ name , $ value );
136
93
}
137
94
}
0 commit comments