<commit_before><?php

/*
* This file is part of pssht.
*
* (c) <NAME> <<EMAIL>>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Clicky\Pssht;

class Algorithms
{
    const CLASS_WHITELIST = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890';
    const ALGO_WHITELIST  = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890@.';

    protected $_algos;

    private function __construct()
    {
        $this->_algos = array(
            'MAC'           => array(),
            'Compression'   => array(),
            'PublicKey'     => array(),
            'KEX'           => array(),
            'Encryption'    => array(),
            'Services'      => array(),
        );

        foreach (array_keys($this->_algos) as $type) {
            $it = new \DirectoryIterator(__DIR__ . DIRECTORY_SEPARATOR . $type);
            foreach ($it as $entry) {
                $name = $entry->getBasename('.php');
                if (!is_string($name) || strspn($name, self::CLASS_WHITELIST) !== strlen($name))
                    continue;
                if (!$entry->isFile())
                    continue;
                $class = "\\Clicky\\Pssht\\$type\\$name";
                if (!class_exists($class))
                    continue;

                $algo = $class::getName();
                if (!is_string($algo) || strspn($algo, self::ALGO_WHITELIST) !== strlen($algo))
                    continue;

                $this->_algos[$type][$algo] = "\\Clicky\\Pssht\\$type\\$name";
            }
        }
    }

    public function __clone()
    {
        throw new \RuntimeException();
    }

    static public function factory()
    {
        static $instance = NULL;
        if ($instance === NULL) {
            $instance = new self();
        }
        return $instance;
    }

    public function register($type, $class)
    {
        if (!is_string($class) || !class_exists($class))
            throw new \InvalidArgumentException();
        if (!is_string($type) || !isset($this->_algos[$type]))
            throw new \InvalidArgumentException();
        $name = $class::getName();
        if (!is_string($name) || strspn($name, self::ALGO_WHITELIST) !== strlen($name))
            throw new \InvalidArgumentException();

        $this->_algos[$type][$name] = $class;
        return $this;
    }

    public function unregister($type, $name)
    {
        if (!is_string($type) || !isset($this->_algos[$type]))
            throw new \InvalidArgumentException();
        if (!is_string($name))
            throw new \InvalidArgumentException();
        unset($this->_algos[$type][$name]);
        return $this;
    }

    public function restore($type, $name)
    {
        if (!is_string($type) || !isset($this->_algos[$type]))
            throw new \InvalidArgumentException();
        if (!is_string($name) || strspn($name, self::CLASS_WHITELIST) !== strlen($name))
            throw new \InvalidArgumentException();

        if (class_exists("\\Clicky\\Pssht\\$type\\$name")) {
            $this->_algos[$type][$name] = "\\Clicky\\Pssht\\$type\\$name";
        }<commit_msg>Refactor Algorithms class a bit.<commit_after><?php

/*
* This file is part of pssht.
*
* (c) <NAME> <<EMAIL>>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Clicky\Pssht;

class Algorithms
{
    protected $_algos;

    private function __construct()
    {
        $this->_algos = array(
            'MAC'           => array(),
            'Compression'   => array(),
            'PublicKey'     => array(),
            'KEX'           => array(),
            'Encryption'    => array(),
            'Services'      => array(),
        );

        foreach (array_keys($this->_algos) as $type) {
            $it = new \DirectoryIterator(__DIR__ . DIRECTORY_SEPARATOR . $type);
            foreach ($it as $entry) {
                if (!$entry->isFile())
                    continue;
                $name   = $entry->getBasename('.php');
                $class  = $this->_getClass($type, $name);
                if ($class === NULL)
                    continue;
                $algo = $this->_getAlgo($class);
                if ($algo === NULL)
                    continue;
                $this->_algos[$type][$algo] = $class;
            }
        }
    }

    public function __clone()
    {
        throw new \RuntimeException();
    }

    static public function factory()
    {
        static $instance = NULL;
        if ($instance === NULL) {
            $instance = new self();
        }
        return $instance;
    }

    protected function _getClass($type, $name)
    {
        $w = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890';
        if (!is_string($name) || strspn($name, $w) !== strlen($name))
            return NULL;
        $class = "\\Clicky\\Pssht\\$type\\$name";
        if (!class_exists($class))
            return NULL;
        $reflector = new ReflectionClass($class);
        return ($reflector->isAbstract() ? NULL : $class);
    }

    protected function _getAlgo($class)
    {
        $w =    'abcdefghijklmnopqrstuvwxyz' .
                'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
                '1234567890-@.';
        $name = $class::getName();
        if (!is_string($name) || strspn($name, $w) !== strlen($name))
            return NULL;
        return $name;
    }

    public function register($type, $class)
    {
        if (is_object($class))
            $class = get_class($class);
        if (!is_string($class) || !class_exists($class))
            throw new \InvalidArgumentException();
        if (!is_string($type) || !isset($this->_algos[$type]))
            throw new \InvalidArgumentException();
        $name = $this->_getAlgo($class);
        if ($name === NULL)
            throw new \InvalidArgumentException();

        $this->_algos[$type][$name] = $class;
        return $this;
    }

    public function unregister($type, $name)
    {
        if (!is_string($type) || !isset($this->_algos[$type]))
            throw new \InvalidArgumentException();
        if (!is_string($name))
            throw new \InvalidArgumentException();
        unset($this->_algos[$type][$name]);
        return $this;
    }

    public function restore($type, $name)
    {
        if (!is_string($type) || !isset($this->_algos[$type]))
            throw new \InvalidArgumentException();
        $class = $this->_getClass($type, $name);
        if ($class !== NULL)
            $this->_algos[$type][$name] = $class;
{'fraction_non_alphanumeric': 0.15316531653165316, 'fraction_numerical': 0.006000600060006, 'mean_word_length': 1.95, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 48, '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': '3555536', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1917, 'n_words': 410}