<commit_before><?php

namespace makallio85\YamlRoute;

/**
 * Class Validator
 *
 * @package makallio85\YamlRoute
 */
class Validator
{
    /**
     * Run validation
     *
     * @param $data
     *
     * @return bool
     * @throws \makallio85\YamlRoute\YamlRouteException
     */
    public static function run($data)
    {
        if (count($data['route']) === 0) {
            throw new YamlRouteException('Invalid routing data in file ' . $data['file'] . '!');
        }
        foreach ($data['route'] as $name => $route) {
            self::checkRoute($name, $route, true);
        }

        return true;
    }

    /**
     * Check route
     *
     * @param $name
     * @param $route
     *
     * @throws \makallio85\YamlRoute\YamlRouteException
     */
    private static function checkRoute($name, $route, $root)
    {
        if (!isset($route['path'])) {
            throw new YamlRouteException("Route path missing for route $name!");
        }
        if (isset($route['config'])) {
            if (isset($route['config']['routes'])) {

                foreach ($route['config']['routes'] as $name => $route) {
                    self::checkRoute($name, $route, false);
                }
            }
        }
        if (!$root && !isset($route['config'])) {
            throw new YamlRouteException("Route $name is missing config key!");

        }
        if (!$root && isset($route['config'])) {
            $requiredKeys = ['controller'];
            foreach ($requiredKeys as $key) {
                if (!array_key_exists($key, $route['config'])) {
                    throw new YamlRouteException("Key $key is missing from route config!");
                }
            }
        }
    }
}<commit_msg>Imporeved error handling and validation<commit_after><?php

namespace makallio85\YamlRoute;

/**
 * Class Validator
 *
 * @package makallio85\YamlRoute
 */
class Validator
{
    /**
     * Run validation
     *
     * @param $data
     *
     * @return bool
     * @throws \makallio85\YamlRoute\YamlRouteException
     */
    public static function run($data)
    {
        if (count($data['route']) === 0) {
            throw new YamlRouteException('Invalid routing data in file \'' . $data['file'] . '\'!');
        }
        foreach ($data['route'] as $name => $route) {
            self::checkRoute($name, $route, true);
        }

        return true;
    }

    /**
     * Check route
     *
     * @param $name
     * @param $route
     *
     * @throws \makallio85\YamlRoute\YamlRouteException
     */
    private static function checkRoute($name, $route, $root)
    {
        if (!isset($route['path'])) {
            throw new YamlRouteException("Route path missing for route '$name''!");
        }
        if (isset($route['config'])) {
            if (isset($route['config']['action']) && !isset($route['config']['controller'])) {
                throw new YamlRouteException('Action \'' . $route['config']['action'] . "' is present, but controller is missing from route '$name'' config!");
            }
            if (isset($route['config']['routes'])) {
                foreach ($route['config']['routes'] as $name => $route) {
                    self::checkRoute($name, $route, false);
                }
            }
        }
        if (!$root && !isset($route['config'])) {
            throw new YamlRouteException("Route '$name'' is missing config key!");

        }
        if (!$root && isset($route['config'])) {
            $requiredKeys = ['controller'];
            foreach ($requiredKeys as $key) {
                if (!array_key_exists($key, $route['config'])) {
                    throw new YamlRouteException("Key '$key' is missing from route '$name'' config!");
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.15938575589091872, 'fraction_numerical': 0.004765687053216839, 'mean_word_length': 1.919629057187017, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '2332517', 'n_tokens_mistral': 1071, 'n_tokens_neox': 1041, 'n_words': 242}