<commit_before><?php
/**
 * Created by BrainMaestro
 * Date: 1/2/2017
 * Time: 7:37 PM
 */

namespace Lawstands\Hermes;

use Lawstands\Hermes\Exception\HermesException;
use Lawstands\Hermes\Formatter\JsonFormatter;

class Config
{
    /**
     * @var array
     */
    private $config;

    /**
     * @var array
     */
    private static $requiredKeys = [
        'channels',
    ];

    /**
     * format: extension => program
     * @var array
     */
    private static $channelTypes = [
        'rb' => 'ruby',
        'py' => 'python',
    ];

    /**
     * Config constructor.
     *
     * @param array $config
     * @throws HermesException
     */
    public function __construct(array $config)
    {
        if (! self::hasRequiredKeys($config)) {
            throw new HermesException('Hermes config is missing one of the required config keys');
        }

        foreach ($config['channels'] as $channel) {
            $this->normalizeChannelConfig($channel);
        }

        $this->config = $config;
    }

    /**
     * Get channels by their aliases.
     *
     * @param string|array|null $aliases
     * @param $data
     * @param bool $async
     * @return array
     */
    public function getChannels($aliases = null, $data, $async = true)
    {
        if (is_null($aliases)) {
            // get all aliases
            $aliases = array_keys($this->config['channels']);
        }

        if (is_string($aliases)) {
            $aliases = [$aliases];
        }

        // create an array of valid channels.
        // the array filter removes the null elements which are
        // the invalid channels.
        return array_filter(array_map(function ($alias) use ($data, $async) {
            if (! isset($this->config['channels'][$alias])) {
                return null;
            }

            return new Channel($this->config['channels'][$alias], $data, $async);
        }, $aliases));
    }

    /**
     * Get the default formatter.
     *
     * @return mixed
     */
    private function getDefaultFormatter()
    {
        return isset($this->config['default_formatter']) ? $this->config['default_formatter'] : JsonFormatter::class;
    }

    /**
     * Get a channel's type.
     *
     * @param $path
     * @return string
     */<commit_msg>Simplify get channels function<commit_after><?php
/**
 * Created by BrainMaestro
 * Date: 1/2/2017
 * Time: 7:37 PM
 */

namespace Lawstands\Hermes;

use Lawstands\Hermes\Exception\HermesException;
use Lawstands\Hermes\Formatters\JsonFormatter;

class Config
{
    /**
     * @var array
     */
    private $config;

    /**
     * @var array
     */
    private static $requiredKeys = [
        'channels',
    ];

    /**
     * format: extension => program
     * @var array
     */
    private static $channelTypes = [
        'rb' => 'ruby',
        'py' => 'python',
    ];

    /**
     * Config constructor.
     *
     * @param array $config
     * @throws HermesException
     */
    public function __construct(array $config)
    {
        if (! self::hasRequiredKeys($config)) {
            throw new HermesException('Hermes config is missing one of the required config keys');
        }

        foreach ($config['channels'] as $channel) {
            $this->normalizeChannelConfig($channel);
        }

        $this->config = $config;
    }

    /**
     * Get channels by their aliases.
     *
     * @param string|array|null $aliases
     * @param $data
     * @param bool $async
     * @return array
     */
    public function getChannels($aliases = null, $data, $async = true)
    {
        if (is_null($aliases)) {
            // get all aliases
            $aliases = array_keys($this->config['channels']);
        }

        if (is_string($aliases)) {
            $aliases = [$aliases];
        }

        $channels = [];
        foreach ($aliases as $alias) {
            // add channel only if its alias exists in the config
            if (isset($this->config['channels'][$alias])) {
                $channels[] = new Channel($this->config['channels'][$alias], $data, $async);
            }
        }

        return $channels;
    }

    /**
     * Get the default formatter.
     *
     * @return mixed
     */
    private function getDefaultFormatter()
    {
        return isset($this->config['default_formatter']) ? $this->config['default_formatter'] : JsonFormatter::class;
    }

    /**
     * Get a channel's type.
     *
     * @param $path
     * @return string
     */
{'fraction_non_alphanumeric': 0.13799243267304695, 'fraction_numerical': 0.0040062319163142665, 'mean_word_length': 1.906856403622251, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '4842825', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1293, 'n_words': 311}