From d0c9e5bdd14f19ce3a5444c99dea423153fe3c71 Mon Sep 17 00:00:00 2001 From: Eugene Matvejev Date: Fri, 1 Jul 2016 09:46:02 +0100 Subject: [PATCH 1/2] RC10 - separate validation --- src/ScriptHandler.php | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/ScriptHandler.php b/src/ScriptHandler.php index d18fd87..813f02a 100644 --- a/src/ScriptHandler.php +++ b/src/ScriptHandler.php @@ -63,33 +63,68 @@ protected static function validateConfiguration(array $config) throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects'); } - foreach ($config[static::CONFIG_MAIN_KEY] as $index => $el) { - if (!is_array($el)) { + return static::validateOptions($config); + } + + /** + * @param array $config + * + * @return bool + * @throws \InvalidArgumentException + */ + protected static function validateOptions(array $config) + { + foreach ($config[static::CONFIG_MAIN_KEY] as $index => $option) { + if (!is_array($option)) { throw new \InvalidArgumentException("the extra.css-compiler[{$index}]." . static::OPTION_KEY_INPUT . ' array'); } - static::validateOptions($el); + static::validateMandatoryOptions($option); } return true; } + /** * @param array $config * * @return bool * @throws \InvalidArgumentException */ - protected static function validateOptions(array $config) + protected static function validateMandatoryOptions(array $config) { foreach (static::$mandatoryOptions as $option) { if (empty($config[$option])) { throw new \InvalidArgumentException("The extra.css-compiler[].{$option} required!"); } } + static::validateInputOption($config); + static::validateOutputOption($config); + + return true; + } + /** + * @param array $config + * + * @return bool + */ + protected static function validateInputOption(array $config) + { if (!is_array($config[static::OPTION_KEY_INPUT])) { throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_INPUT . ' should be array!'); } + + return true; + } + + /** + * @param array $config + * + * @return bool + */ + protected static function validateOutputOption(array $config) + { if (!is_string($config[static::OPTION_KEY_OUTPUT])) { throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_OUTPUT . ' should string!'); } From 5b6093ff76764e10953d100a626ea865e39faebf Mon Sep 17 00:00:00 2001 From: Eugene Matvejev Date: Fri, 1 Jul 2016 11:24:04 +0100 Subject: [PATCH 2/2] fix build --- src/ScriptHandler.php | 37 +++++++++++++++++------------ tests/phpunit/ScriptHandlerTest.php | 9 +++---- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/ScriptHandler.php b/src/ScriptHandler.php index 813f02a..fd4d575 100644 --- a/src/ScriptHandler.php +++ b/src/ScriptHandler.php @@ -63,7 +63,7 @@ protected static function validateConfiguration(array $config) throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects'); } - return static::validateOptions($config); + return static::validateOptions($config[static::CONFIG_MAIN_KEY]); } /** @@ -74,9 +74,9 @@ protected static function validateConfiguration(array $config) */ protected static function validateOptions(array $config) { - foreach ($config[static::CONFIG_MAIN_KEY] as $index => $option) { + foreach ($config as $option) { if (!is_array($option)) { - throw new \InvalidArgumentException("the extra.css-compiler[{$index}]." . static::OPTION_KEY_INPUT . ' array'); + throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[]." . static::OPTION_KEY_INPUT . ' array'); } static::validateMandatoryOptions($option); @@ -85,7 +85,6 @@ protected static function validateOptions(array $config) return true; } - /** * @param array $config * @@ -96,37 +95,45 @@ protected static function validateMandatoryOptions(array $config) { foreach (static::$mandatoryOptions as $option) { if (empty($config[$option])) { - throw new \InvalidArgumentException("The extra.css-compiler[].{$option} required!"); + throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$option} is required!"); + } + + switch ($option) { + case static::OPTION_KEY_INPUT: + static::validateIsArray($config[$option]); + break; + case static::OPTION_KEY_OUTPUT: + static::validateIsString($config[$option]); + break; } } - static::validateInputOption($config); - static::validateOutputOption($config); return true; } + /** - * @param array $config + * @param array $option * * @return bool */ - protected static function validateInputOption(array $config) + protected static function validateIsArray($option) { - if (!is_array($config[static::OPTION_KEY_INPUT])) { - throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_INPUT . ' should be array!'); + if (!is_array($option)) { + throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_INPUT . ' should be array!'); } return true; } /** - * @param array $config + * @param string $option * * @return bool */ - protected static function validateOutputOption(array $config) + protected static function validateIsString($option) { - if (!is_string($config[static::OPTION_KEY_OUTPUT])) { - throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_OUTPUT . ' should string!'); + if (!is_string($option)) { + throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_OUTPUT . ' should string!'); } return true; diff --git a/tests/phpunit/ScriptHandlerTest.php b/tests/phpunit/ScriptHandlerTest.php index 42a73f3..6f6038b 100644 --- a/tests/phpunit/ScriptHandlerTest.php +++ b/tests/phpunit/ScriptHandlerTest.php @@ -32,6 +32,7 @@ function validateConfigurationExpectedExceptionOnEmpty() { $this->invokeMethod(new ScriptHandler(), 'validateConfiguration', [[ScriptHandler::CONFIG_MAIN_KEY]]); } + /** * @see ScriptHandler::validateConfiguration * @test @@ -134,10 +135,10 @@ function validateOptionsExpectedExceptionOnOutputNotString() */ function validateOptionsOnValid() { - $result = $this->invokeMethod(new ScriptHandler(), 'validateOptions', [[ - ScriptHandler::OPTION_KEY_INPUT => ['string'], - ScriptHandler::OPTION_KEY_OUTPUT => 'string' - ]]); + $options = [ + [ScriptHandler::OPTION_KEY_INPUT => ['string'], ScriptHandler::OPTION_KEY_OUTPUT => 'string'] + ]; + $result = $this->invokeMethod(new ScriptHandler(), 'validateOptions', [$options]); $this->assertTrue($result); }