@@ -18,11 +18,13 @@ class ScriptHandler
18
18
const OPTION_KEY_FORMATTER = 'format ' ;
19
19
const DEFAULT_OPTION_FORMATTER = 'compact ' ;
20
20
protected static $ mandatoryOptions = [
21
- self ::OPTION_KEY_INPUT ,
22
- self ::OPTION_KEY_OUTPUT
21
+ self ::OPTION_KEY_INPUT => ' array ' ,
22
+ self ::OPTION_KEY_OUTPUT => ' string '
23
23
];
24
24
25
25
/**
26
+ * @api
27
+ *
26
28
* @param Event $event
27
29
*
28
30
* @throws \InvalidArgumentException
@@ -34,15 +36,15 @@ public static function generateCSS(Event $event)
34
36
35
37
$ processor = new Processor ($ event ->getIO ());
36
38
37
- foreach ($ extra [static ::CONFIG_MAIN_KEY ] as $ config ) {
38
- foreach ($ config [static ::OPTION_KEY_INPUT ] as $ inputSource ) {
39
+ foreach ($ extra [static ::CONFIG_MAIN_KEY ] as $ options ) {
40
+ foreach ($ options [static ::OPTION_KEY_INPUT ] as $ inputSource ) {
39
41
$ processor ->attachFiles (
40
42
static ::resolvePath ($ inputSource , getcwd ()),
41
- static ::resolvePath ($ config [static ::OPTION_KEY_OUTPUT ], getcwd ())
43
+ static ::resolvePath ($ options [static ::OPTION_KEY_OUTPUT ], getcwd ())
42
44
);
43
45
}
44
46
45
- $ formatter = isset ( $ config [ static ::OPTION_KEY_FORMATTER ] ) ? $ config [static ::OPTION_KEY_FORMATTER ] : static ::DEFAULT_OPTION_FORMATTER ;
47
+ $ formatter = array_key_exists ( static ::OPTION_KEY_FORMATTER , $ options ) ? $ options [static ::OPTION_KEY_FORMATTER ] : static ::DEFAULT_OPTION_FORMATTER ;
46
48
$ processor ->processFiles ($ formatter );
47
49
}
48
50
$ processor ->saveOutput ();
@@ -62,51 +64,43 @@ protected static function resolvePath($path, $prefix)
62
64
/**
63
65
* @param array $config
64
66
*
65
- * @return bool
66
67
* @throws \InvalidArgumentException
67
68
*/
68
69
protected static function validateConfiguration (array $ config )
69
70
{
70
- if (empty ( $ config [ static ::CONFIG_MAIN_KEY ] )) {
71
+ if (! array_key_exists ( static ::CONFIG_MAIN_KEY , $ config )) {
71
72
throw new \InvalidArgumentException ('compiler should needs to be configured through the extra.css-compiler setting ' );
72
73
}
73
74
74
75
if (!is_array ($ config [static ::CONFIG_MAIN_KEY ])) {
75
- throw new \InvalidArgumentException ('the extra.css-compiler setting must be an array of objects ' );
76
+ throw new \InvalidArgumentException ('the extra. ' . static :: CONFIG_MAIN_KEY . ' setting must be an array of objects ' );
76
77
}
77
78
78
- foreach ($ config [static ::CONFIG_MAIN_KEY ] as $ options ) {
79
+ foreach ($ config [static ::CONFIG_MAIN_KEY ] as $ index => $ options ) {
79
80
if (!is_array ($ options )) {
80
- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[]. " . static :: OPTION_KEY_INPUT . ' array' );
81
+ throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[ $ index ] should be an array" );
81
82
}
82
83
83
- static ::validateMandatoryOptions ($ options );
84
+ static ::validateMandatoryOptions ($ options, $ index );
84
85
}
85
86
}
86
87
87
88
/**
88
89
* @param array $options
90
+ * @param int $index
89
91
*
90
92
* @throws \InvalidArgumentException
91
93
*/
92
- protected static function validateMandatoryOptions (array $ options )
94
+ protected static function validateMandatoryOptions (array $ options, $ index )
93
95
{
94
- foreach (static ::$ mandatoryOptions as $ optionIndex ) {
95
- if (!isset ( $ options [ $ optionIndex] )) {
96
- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[]. {$ optionIndex } is required! " );
96
+ foreach (static ::$ mandatoryOptions as $ optionIndex => $ type ) {
97
+ if (!array_key_exists ( $ optionIndex, $ options )) {
98
+ throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[ $ index ]. {$ optionIndex } is required! " );
97
99
}
98
100
99
- switch ($ optionIndex ) {
100
- case static ::OPTION_KEY_INPUT :
101
- if (!is_array ($ options [$ optionIndex ])) {
102
- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[]. {$ optionIndex } should be array! " );
103
- }
104
- break ;
105
- case static ::OPTION_KEY_OUTPUT :
106
- if (!is_string ($ options [$ optionIndex ])) {
107
- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[]. {$ optionIndex } should string! " );
108
- }
109
- break ;
101
+ $ callable = "is_ {$ type }" ;
102
+ if (!$ callable ($ options [$ optionIndex ])) {
103
+ throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[ $ index]. {$ optionIndex } should be {$ type }! " );
110
104
}
111
105
}
112
106
}
0 commit comments