<commit_before><?php

namespace EM\CssCompiler\Handler;

use Composer\Script\Event;
use EM\CssCompiler\Processor\Processor;

class ScriptHandler
{
    public static function compileCSS(Event $event)
    {
        $extras = $event->getComposer()->getPackage()->getExtra();

        if (!isset($extras['css-compiler'])) {
            throw new \InvalidArgumentException('The parameter handler needs to be configured through the extra.css-compiler setting.');
        }

        $configs = $extras['css-compiler'];

        if (!is_array($configs)) {
            throw new \InvalidArgumentException('The extra.css-compiler setting must be an array of a configuration objects.');
        }

        if (array_keys($configs) !== range(0, count($configs) - 1)) {
            $configs = [$configs];
        }

        $processor = new Processor($event->getIO());
        $prefix = getcwd();
        foreach ($configs as $config) {
            if (!is_array($config)) {
                throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.');
            }

            foreach ($config['input'] as $item => $value) {
                $processor->attachFiles("{$prefix}/{$value}", "{$prefix}/{$config['output']}");
            }

            $processor->processFiles(isset($config['format']) ? $config['format'] : 'compact');
        }

        $processor->saveOutput();
    }
}<commit_msg>improve composer handler and its validation<commit_after><?php

namespace EM\CssCompiler\Handler;

use Composer\Script\Event;
use EM\CssCompiler\Processor\Processor;

class ScriptHandler
{
    const CONFIG_MAIN_KEY          = 'css-compiler';
    const CONFIG_INPUT_KEY         = 'input';
    const CONFIG_OUTPUT_KEY        = 'output';
    const CONFIG_FORMATTER_KEY     = 'format';
    const CONFIG_DEFAULT_FORMATTER = 'compact';

    public static function compileCSS(Event $event)
    {
        $extra = $event->getComposer()->getPackage()->getExtra();

        static::validateConfiguration($extra);

        $processor = new Processor($event->getIO());
        $currentDirectory = getcwd();
        foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
            foreach ($config[static::CONFIG_INPUT_KEY] as $item => $value) {
                $processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::CONFIG_OUTPUT_KEY]}");
            }

            $formatter = isset($config[static::CONFIG_FORMATTER_KEY])
                ? $config[static::CONFIG_FORMATTER_KEY]
                : static::CONFIG_DEFAULT_FORMATTER;

            $processor->processFiles($formatter);
        }

        $processor->saveOutput();
    }

    protected static function validateConfiguration(array $config)
    {
        if (!isset($config[static::CONFIG_MAIN_KEY])) {
            throw new \InvalidArgumentException('the parameter handler needs to be configured through the extra.css-compiler setting.');
        }

        if (!is_array($config[static::CONFIG_MAIN_KEY])) {
            throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of a configuration objects.');
        }

        foreach ($config[static::CONFIG_MAIN_KEY] as $el) {
            if (!is_array($el)) {
                throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.');
            }

            if (!isset($el[static::CONFIG_INPUT_KEY])) {
                throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_INPUT_KEY . ' is required!');
            }
            if (!isset($el[static::CONFIG_OUTPUT_KEY])) {
                throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_OUTPUT_KEY . ' is required!');
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.1349934469200524, 'fraction_numerical': 0.0005242463958060288, 'mean_word_length': 2.6905222437137333, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '62830', 'n_tokens_mistral': 1011, 'n_tokens_neox': 976, 'n_words': 213}