Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions src/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,23 @@ 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)) {
throw new \InvalidArgumentException("the extra.css-compiler[{$index}]." . static::OPTION_KEY_INPUT . ' array');
return static::validateOptions($config[static::CONFIG_MAIN_KEY]);
}

/**
* @param array $config
*
* @return bool
* @throws \InvalidArgumentException
*/
protected static function validateOptions(array $config)
{
foreach ($config as $option) {
if (!is_array($option)) {
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[]." . static::OPTION_KEY_INPUT . ' array');
}

static::validateOptions($el);
static::validateMandatoryOptions($option);
}

return true;
Expand All @@ -80,18 +91,49 @@ protected static function validateConfiguration(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!");
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;
}
}
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 $option
*
* @return bool
*/
protected static function validateIsArray($option)
{
if (!is_array($option)) {
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_INPUT . ' should be array!');
}
if (!is_string($config[static::OPTION_KEY_OUTPUT])) {
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_OUTPUT . ' should string!');

return true;
}

/**
* @param string $option
*
* @return bool
*/
protected static function validateIsString($option)
{
if (!is_string($option)) {
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_OUTPUT . ' should string!');
}

return true;
Expand Down
9 changes: 5 additions & 4 deletions tests/phpunit/ScriptHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function validateConfigurationExpectedExceptionOnEmpty()
{
$this->invokeMethod(new ScriptHandler(), 'validateConfiguration', [[ScriptHandler::CONFIG_MAIN_KEY]]);
}

/**
* @see ScriptHandler::validateConfiguration
* @test
Expand Down Expand Up @@ -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);
}
Expand Down