<commit_before><?php
namespace Extlib;

/**
 * Generator class
 *
 * @category    Extlib
 * @author      <NAME> <<EMAIL>> 
 * @copyright   Copyright (c) <NAME> (mart)
 */
final class Generator 
{    
    /* Configuration */
    const RAND_MIN = 1;
    const RAND_MAX = 1000000;
    const CUT_LEN = 4;
    
    /* Algorithm types */
    const ALGO_MD5 = 'md5';
    const ALGO_SHA256 = 'sha256';
    const ALGO_SHA1   = 'shad1';

    /**
     * Array allowed types of algorithm
     * 
     * @var array 
     */
    static protected $allowedAlgorithm = array(
        self::ALGO_MD5,
        self::ALGO_SHA256,
        self::ALGO_SHA1
    );
    
    /**
     * Doctrine v1.2 generate method
     * 
     * 
     * @param string $tableName
     * @param string $field
     * @param string $length
     * @return string
     * @throws Extlib\Exception 
     */
    static public function doctrine($tableName, $field, $length = 16)
    {
        do {
            $generate = self::generate($length);
        } while (function($tableName, $field, $generate) {
            return \Doctrine_Query::create()->from($tableName)->where("$field = ?", $generate)->fetchOne();
        });
        
        return $generate;
    }
    
    /**
     * Doctrine v2.x generate method
     * 
     * @param \Doctrine\ORM\EntityManager $entityManager
     * @param string $entityName
     * @param string $field
     * @param int $length
     * @return string
     */
    static public function generateDoctrine2(\Doctrine\ORM\EntityManager $entityManager, $entityName, $field, $length = 16)
    {        
        do {
            $generate = self::generate($length);
        } while (function(\Doctrine\ORM\EntityManager $entityManager, $entityName, $field, $generate) {
            return $entityManager->getRepository($entityName)->findOneBy(array($field => $generate));
        });
        
        return $generate;
    }
    
    /**
     * Phalcon ORM generate method
     * 
     * @param string $modelName
     * @param string $field
     * @param int $length
     * @return string
     */
    static public function generatePhalcon($modelName, $field, $length = 16)
    {
        function query($modelName, $field, $generate)
        {
            $return = \Phalcon\Mvc\Model::query()
                                        ->setModelName($modelName)
                                        ->where("$field = :value:")
                                        ->bind(array('value' => $generate))
                                        ->execute();
            
            return (boolean) $return->count();
        }
        
        
        
        do {
            $generate = self::generate($length);
        } while (query($modelName, $field, $generate));
        
        return $generate;
    }

    /**
     * Generate random string
     * 
     * @param int $length
     * @param string $algorithm
     * @return string
     * @throws \Extlib\Exception 
     */
    static public function generate($length = 16, $algorithm = 'sha256')
    {
        if (!in_array($algorithm, self::$allowedAlgorithm)) {
            throw new Exception("Hash algorithm $algorithm doesn't exists!");    
        }

        $salt = hash($algorithm, time());

        return substr(hash($algorithm, (mt_rand(self::RAND_MIN, self::RAND_MAX) % $length) . $salt . mt_rand(self::RAND_MIN, self::RAND_MAX)), self::CUT_LEN, $length);
    }
}
<commit_msg>Add phalcon orm generate method<commit_after><?php
namespace Extlib;

/**
 * Generator class
 *
 * @category    Extlib
 * @author      <NAME> <<EMAIL>> 
 * @copyright   Copyright (c) <NAME> (mart)
 */
final class Generator 
{    
    /* Configuration */
    const RAND_MIN = 1;
    const RAND_MAX = 1000000;
    const CUT_LEN = 4;
    
    /* Algorithm types */
    const ALGO_MD5 = 'md5';
    const ALGO_SHA256 = 'sha256';
    const ALGO_SHA1   = 'shad1';

    /**
     * Array allowed types of algorithm
     * 
     * @var array 
     */
    static protected $allowedAlgorithm = array(
        self::ALGO_MD5,
        self::ALGO_SHA256,
        self::ALGO_SHA1
    );
    
    /**
     * Doctrine v1.2 generate method
     * 
     * 
     * @param string $tableName
     * @param string $field
     * @param string $length
     * @return string
     * @throws Extlib\Exception 
     */
    static public function doctrine($tableName, $field, $length = 16)
    {
        do {
            $generate = self::generate($length);
        } while (function($tableName, $field, $generate) {
            return \Doctrine_Query::create()->from($tableName)->where("$field = ?", $generate)->fetchOne();
        });
        
        return $generate;
    }
    
    /**
     * Doctrine v2.x generate method
     * 
     * @param \Doctrine\ORM\EntityManager $entityManager
     * @param string $entityName
     * @param string $field
     * @param int $length
     * @return string
     */
    static public function generateDoctrine2(\Doctrine\ORM\EntityManager $entityManager, $entityName, $field, $length = 16)
    {        
        do {
            $generate = self::generate($length);
        } while (function(\Doctrine\ORM\EntityManager $entityManager, $entityName, $field, $generate) {
            return $entityManager->getRepository($entityName)->findOneBy(array($field => $generate));
        });
        
        return $generate;
    }
    
    /**
     * Phalcon ORM generate method
     * 
     * @param string $modelName
     * @param string $field
     * @param int $length
     * @return string
     */
    static public function generatePhalcon($modelName, $field, $length = 16)
    {
        function query($modelName, $field, $generate)
        {
            $return = \Phalcon\Mvc\Model::query()
                                        ->setModelName($modelName)
                                        ->where("$field = :value:")
                                        ->bind(array('value' => $generate))
                                        ->execute();
            
            return (boolean) $return->count();
        }
        
        do {
            $generate = self::generate($length);
        } while (query($modelName, $field, $generate));
        
        return $generate;
    }

    /**
     * Generate random string
     * 
     * @param int $length
     * @param string $algorithm
     * @return string
     * @throws \Extlib\Exception 
     */
    static public function generate($length = 16, $algorithm = 'sha256')
    {
        if (!in_array($algorithm, self::$allowedAlgorithm)) {
            throw new Exception("Hash algorithm $algorithm doesn't exists!");    
        }

        $salt = hash($algorithm, time());

        return substr(hash($algorithm, (mt_rand(self::RAND_MIN, self::RAND_MAX) % $length) . $salt . mt_rand(self::RAND_MIN, self::RAND_MAX)), self::CUT_LEN, $length);
    }
}

{'fraction_non_alphanumeric': 0.12637207993245145, 'fraction_numerical': 0.010976639459611595, 'mean_word_length': 1.6808751414560543, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1526694', 'n_tokens_mistral': 2096, 'n_tokens_neox': 1952, 'n_words': 442}