<commit_before><?php

namespace Gdbots\Pbjc;

class SchemaStore
{
    /**
     * Regular expression pattern for matching a valid SchemaId string.
     *
     * Schema Id Format:
     *  pbj:vendor:package:category:message:version
     *
     * Formats:
     *  VENDOR:   [a-z0-9-]+
     *  PACKAGE:  [a-z0-9\.-]+
     *  CATEGORY: ([a-z0-9-]+)? (clarifies the intent of the message, e.g. command, request, event, response, etc.)
     *  MESSAGE:  [a-z0-9-]+
     *  VERSION:  [0-9]+-[0-9]+-[0-9])
     *
     * Examples of fully qualified schema ids:
     *  pbj:acme:videos:event:video-uploaded:1-0-0
     *  pbj:acme:users:command:register-user:1-1-0
     *  pbj:acme:api.videos:request:get-video:1-0-0
     *
     * @constant string
     */
    const VALID_PATTERN = '/^pbj:([a-z0-9-]+):([a-z0-9\.-]+):([a-z0-9-]+)?:([a-z0-9-]+):(([0-9]+)-([0-9]+)-([0-9]+))$/';

    /** @var array */
    protected static $dirs = [];

    /** @var array */
    protected static $schemas = [];

    /**
     * Adds a directory where schemas exist.
     *
     * @param string $dir
     * @param bool   $isDependent
     */
    public static function addDir($dir, $isDependent = false)
    {
        self::$dirs[$dir] = $isDependent;
    }

    /**
     * Checks whether the directory is marked as a dependent.
     *
     * @param string $dir
     *
     * @return bool
     */
    public static function isDirDependent($dir)
    {
        if (isset(self::$dirs[$dir])) {
            return self::$dirs[$dir];
        }

        return false;
    }

    /**
     * Adds an array of schema directories.
     *
     * @param array $dirs
     */
    public static function addDirs(array $dirs)
    {
        foreach ($dirs as $dir => $isDependent) {
            self::addDir($dir, $isDependent);
        }
    }

    /**
     * Returns an array of directories where schemas should exist.
     *
     * @return array
     */
    public static function getDirs()
    {
        return self::$dirs;
    }

    /**
     * Adds a schema. An exception will be thorwn when attempting to load
     * the same id multi times.
     *
     * @param string       $id
     * @param array|Schema $schema
     * @param bool         $ignoreDuplication
     *
     * @throw \RuntimeException on duplicate schema id's
     */
    public static function addSchema($id, $schema, $ignoreDuplication = false)
    {
        if (isset(self::$schemas[$id]) && !$ignoreDuplication) {
            throw new \RuntimeException(sprintf('Schema with id "%s" is already exists.', $id));
        }

        if (!self::validateSchemaId($id) && !$ignoreDuplication) {
            throw new \RuntimeException(sprintf('Schema with id "%s" is invalid.', $id));
        }

        self::$schemas[$id] = $schema;
    }

    /**
     * Returns an array of schemas.
     *
     * @return array
     */
    public static function getSchemas()
    {
        return self::$schemas;
    }

    /**
     * Returns an array of sorted schemas.
     *
     * @return array
     */
    public static function getSortedSchemas()
    {
        $schemas = self::$schemas;
        ksort($schemas);
        return $schemas;
    }

    /**
     * Returns a schema by its id. This is NOT the \Gdbots\Pbjc\Schema object.
     * It contains more info (from the xml) about how to build this schema
     * into multiple languages. the pbj-php is specifically for php
     * (assuming it's already been compiled).
     *
     * @param string $id
     *
     * @return array|\Gdbots\Pbjc\Schema|null
     */
    public static function getSchemaById($id)
    {
        if (isset(self::$schemas[$id])) {
            return self::$schemas[$id];
        }

        throw new \RuntimeException(sprintf('Schema with id "%s" is invalid.', $id));
    }<commit_msg>Auto sort schemas on add<commit_after><?php

namespace Gdbots\Pbjc;

class SchemaStore
{
    /**
     * Regular expression pattern for matching a valid SchemaId string.
     *
     * Schema Id Format:
     *  pbj:vendor:package:category:message:version
     *
     * Formats:
     *  VENDOR:   [a-z0-9-]+
     *  PACKAGE:  [a-z0-9\.-]+
     *  CATEGORY: ([a-z0-9-]+)? (clarifies the intent of the message, e.g. command, request, event, response, etc.)
     *  MESSAGE:  [a-z0-9-]+
     *  VERSION:  [0-9]+-[0-9]+-[0-9])
     *
     * Examples of fully qualified schema ids:
     *  pbj:acme:videos:event:video-uploaded:1-0-0
     *  pbj:acme:users:command:register-user:1-1-0
     *  pbj:acme:api.videos:request:get-video:1-0-0
     *
     * @constant string
     */
    const VALID_PATTERN = '/^pbj:([a-z0-9-]+):([a-z0-9\.-]+):([a-z0-9-]+)?:([a-z0-9-]+):(([0-9]+)-([0-9]+)-([0-9]+))$/';

    /** @var array */
    protected static $dirs = [];

    /** @var array */
    protected static $schemas = [];

    /**
     * Adds a directory where schemas exist.
     *
     * @param string $dir
     * @param bool   $isDependent
     */
    public static function addDir($dir, $isDependent = false)
    {
        self::$dirs[$dir] = $isDependent;
    }

    /**
     * Checks whether the directory is marked as a dependent.
     *
     * @param string $dir
     *
     * @return bool
     */
    public static function isDirDependent($dir)
    {
        if (isset(self::$dirs[$dir])) {
            return self::$dirs[$dir];
        }

        return false;
    }

    /**
     * Adds an array of schema directories.
     *
     * @param array $dirs
     */
    public static function addDirs(array $dirs)
    {
        foreach ($dirs as $dir => $isDependent) {
            self::addDir($dir, $isDependent);
        }
    }

    /**
     * Returns an array of directories where schemas should exist.
     *
     * @return array
     */
    public static function getDirs()
    {
        return self::$dirs;
    }

    /**
     * Adds a schema. An exception will be thorwn when attempting to load
     * the same id multi times.
     *
     * @param string       $id
     * @param array|Schema $schema
     * @param bool         $ignoreDuplication
     *
     * @throw \RuntimeException on duplicate schema id's
     */
    public static function addSchema($id, $schema, $ignoreDuplication = false)
    {
        if (isset(self::$schemas[$id]) && !$ignoreDuplication) {
            throw new \RuntimeException(sprintf('Schema with id "%s" is already exists.', $id));
        }

        if (!self::validateSchemaId($id) && !$ignoreDuplication) {
            throw new \RuntimeException(sprintf('Schema with id "%s" is invalid.', $id));
        }

        self::$schemas[$id] = $schema;
        ksort(self::$schemas);
    }

    /**
     * Returns an array of schemas.
     *
     * @return array
     */
    public static function getSchemas()
    {
        return self::$schemas;
    }

    /**
     * Returns a schema by its id. This is NOT the \Gdbots\Pbjc\Schema object.
     * It contains more info (from the xml) about how to build this schema
     * into multiple languages. the pbj-php is specifically for php
     * (assuming it's already been compiled).
     *
     * @param string $id
     *
     * @return array|\Gdbots\Pbjc\Schema|null
     */
    public static function getSchemaById($id)
    {
        if (isset(self::$schemas[$id])) {
            return self::$schemas[$id];
        }

        throw new \RuntimeException(sprintf('Schema with id "%s" is invalid.', $id));
    }
{'fraction_non_alphanumeric': 0.14811795080853377, 'fraction_numerical': 0.01005571409158853, 'mean_word_length': 2.0275606746194983, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5406308', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2374, 'n_words': 596}