@@ -18,11 +18,13 @@ class ScriptHandler
1818 const OPTION_KEY_FORMATTER = 'format ' ;
1919 const DEFAULT_OPTION_FORMATTER = 'compact ' ;
2020 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 '
2323 ];
2424
2525 /**
26+ * @api
27+ *
2628 * @param Event $event
2729 *
2830 * @throws \InvalidArgumentException
@@ -34,20 +36,26 @@ public static function generateCSS(Event $event)
3436
3537 $ processor = new Processor ($ event ->getIO ());
3638
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 ) {
3941 $ processor ->attachFiles (
4042 static ::resolvePath ($ inputSource , getcwd ()),
41- static ::resolvePath ($ config [static ::OPTION_KEY_OUTPUT ], getcwd ())
43+ static ::resolvePath ($ options [static ::OPTION_KEY_OUTPUT ], getcwd ())
4244 );
4345 }
4446
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 ;
4648 $ processor ->processFiles ($ formatter );
4749 }
4850 $ processor ->saveOutput ();
4951 }
5052
53+ /**
54+ * @param string $path
55+ * @param string $prefix
56+ *
57+ * @return string
58+ */
5159 protected static function resolvePath ($ path , $ prefix )
5260 {
5361 return '/ ' === substr ($ path , 0 , 1 ) ? $ path : "{$ prefix }/ {$ path }" ;
@@ -56,92 +64,44 @@ protected static function resolvePath($path, $prefix)
5664 /**
5765 * @param array $config
5866 *
59- * @return bool
6067 * @throws \InvalidArgumentException
6168 */
6269 protected static function validateConfiguration (array $ config )
6370 {
64- if (empty ( $ config [ static ::CONFIG_MAIN_KEY ] )) {
71+ if (! array_key_exists ( static ::CONFIG_MAIN_KEY , $ config )) {
6572 throw new \InvalidArgumentException ('compiler should needs to be configured through the extra.css-compiler setting ' );
6673 }
6774
6875 if (!is_array ($ config [static ::CONFIG_MAIN_KEY ])) {
69- 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 ' );
7077 }
7178
72- return static ::validateOptions ($ config [static ::CONFIG_MAIN_KEY ]);
73- }
74-
75- /**
76- * @param array $config
77- *
78- * @return bool
79- * @throws \InvalidArgumentException
80- */
81- protected static function validateOptions (array $ config )
82- {
83- foreach ($ config as $ option ) {
84- if (!is_array ($ option )) {
85- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[]. " . static ::OPTION_KEY_INPUT . ' array ' );
79+ foreach ($ config [static ::CONFIG_MAIN_KEY ] as $ index => $ options ) {
80+ if (!is_array ($ options )) {
81+ throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[ $ index] should be an array " );
8682 }
8783
88- static ::validateMandatoryOptions ($ option );
84+ static ::validateMandatoryOptions ($ options , $ index );
8985 }
90-
91- return true ;
9286 }
9387
9488 /**
95- * @param array $config
89+ * @param array $options
90+ * @param int $index
9691 *
97- * @return bool
9892 * @throws \InvalidArgumentException
9993 */
100- protected static function validateMandatoryOptions (array $ config )
94+ protected static function validateMandatoryOptions (array $ options , $ index )
10195 {
102- foreach (static ::$ mandatoryOptions as $ option ) {
103- if (empty ( $ config [ $ option ] )) {
104- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[]. {$ option } 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! " );
10599 }
106100
107- switch ($ option ) {
108- case static ::OPTION_KEY_INPUT :
109- static ::validateIsArray ($ config [$ option ]);
110- break ;
111- case static ::OPTION_KEY_OUTPUT :
112- static ::validateIsString ($ config [$ option ]);
113- break ;
101+ $ callable = "is_ {$ type }" ;
102+ if (!$ callable ($ options [$ optionIndex ])) {
103+ throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . "[ $ index]. {$ optionIndex } should be {$ type }! " );
114104 }
115105 }
116-
117- return true ;
118- }
119-
120- /**
121- * @param array $option
122- *
123- * @return bool
124- */
125- protected static function validateIsArray ($ option )
126- {
127- if (!is_array ($ option )) {
128- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . '[] ' . static ::OPTION_KEY_INPUT . ' should be array! ' );
129- }
130-
131- return true ;
132- }
133-
134- /**
135- * @param string $option
136- *
137- * @return bool
138- */
139- protected static function validateIsString ($ option )
140- {
141- if (!is_string ($ option )) {
142- throw new \InvalidArgumentException ('extra. ' . static ::CONFIG_MAIN_KEY . '[] ' . static ::OPTION_KEY_OUTPUT . ' should string! ' );
143- }
144-
145- return true ;
146106 }
147107}
0 commit comments