<commit_before><?php

/**
 *  _   __ __ _____ _____ ___  ____  _____
 * | | / // // ___//_  _//   ||  __||_   _|
 * | |/ // /(__  )  / / / /| || |     | |
 * |___//_//____/  /_/ /_/ |_||_|     |_|
 * @link https://vistart.me/
 * @copyright Copyright (c) 2016 - 2017 vistart
 * @license https://vistart.me/license/
 */

namespace rhosocial\base\models\traits;

use Yii;
use yii\base\ModelEvent;

/**
 * This trait allow its owner to enable the entity to be blamed by user.
 * @property-read boolean $isConfirmed
 * @property integer $confirmation
 * @property-read array $confirmationRules
 * @property string $confirmCode the confirm code used for confirming the content.
 * You can disable this attribute and create a new model for storing confirm code as
 * its low-frequency usage.
 * @version 1.0
 * @author vistart <<EMAIL>>
 */
trait ConfirmationTrait
{

    /**
     * @var int Unconfirmed.
     */
    public static $confirmFalse = 0;

    /**
     * @var int Confirmed.
     */
    public static $confirmTrue = 1;

    /**
     * @var string|false attribute name of confirmation, or false if disable confirmation features.
     */
    public $confirmationAttribute = false;

    /**
     * @var string This attribute specify the name of confirm_code attribute, if
     * this attribute is assigned to false, this feature will be ignored.
     * if $confirmationAttribute is empty or false, this attribute will be skipped.
     */
    public $confirmCodeAttribute = 'confirm_code';

    /**
     * @var integer The expiration in seconds. If $confirmCodeAttribute is
     * specified, this attribute must be specified.
     */
    public $confirmCodeExpiration = 3600;

    /**
     * @var string This attribute specify the name of confirm_time attribute. if
     * this attribute is assigned to false, this feature will be ignored.
     * if $confirmationAttribute is empty or false, this attribute will be skipped.
     */
    public $confirmTimeAttribute = 'confirmed_at';

    /**
     * @var string initialization confirm time.
     */
    public $initConfirmTime = '1970-01-01 00:00:00';
    public static $eventConfirmationChanged = "confirmationChanged";
    public static $eventConfirmationCanceled = "confirmationCanceled";
    public static $eventConfirmationSuceeded = "confirmationSucceeded";

    /**
     * Apply confirmation.
     * @return boolean
     * @throws \yii\base\NotSupportedException
     */
    public function applyConfirmation()
    {
        if (!$this->confirmCodeAttribute || empty($this->confirmCodeAttribute)) {
            throw new \yii\base\NotSupportedException('This method is not implemented.');
        }
        $this->setConfirmCode($this->generateConfirmationCode());
        return $this->save();
    }

    /**
     * Set confirm code.
     * @param string $code
     */
    public function setConfirmCode($code)
    {
        if (!$this->confirmCodeAttribute || empty($this->confirmCodeAttribute)) {
            return;
        }
        $confirmCodeAttribute = $this->confirmCodeAttribute;
        $this->$confirmCodeAttribute = $code;
        if (!$this->confirmTimeAttribute) {
            return;
        }
        $confirmTimeAttribute = $this->confirmTimeAttribute;
        if (!empty($code)) {
            $this->$confirmTimeAttribute = date('Y-m-d H:i:s');
            return;
        }
        $this->$confirmTimeAttribute = $this->initConfirmTime;
    }

    /**
     * Get confirm code.
     * @return string
     */
    public function getConfirmCode()
    {
        $confirmCodeAttribute = $this->confirmCodeAttribute;
        return (is_string($confirmCodeAttribute) && !empty($confirmCodeAttribute)) ? $this->$confirmCodeAttribute : null;
    }

    /**
     * Confirm the current content.
     * @param string $code
     * @return boolean
     */
    public function confirm($code)
    {
        if (!$this->confirmationAttribute || !$this->validateConfirmationCode($code)) {
            return false;
        }
        $this->confirmation = static::$confirmTrue;
        return $this->save();
    }

    /**
     * Generate confirmation code.
     * @return string code
     */
    public function generateConfirmationCode()
    {
        return substr(sha1(Yii::$app->security->generateRandomString()), 0, 17);
    }

    /**
     * Validate the confirmation code.
     * @param string $code
     * @return boolean Whether the confirmation code is valid.
     */
    public function validateConfirmationCode($code)
    {
        $ccAttribute = $this->confirmCodeAttribute;
        if (!$ccAttribute || empty($ccAttribute)) {
            return true;
        }<commit_msg>allow empty confirmation code.<commit_after><?php

/**
 *  _   __ __ _____ _____ ___  ____  _____
 * | | / // // ___//_  _//   ||  __||_   _|
 * | |/ // /(__  )  / / / /| || |     | |
 * |___//_//____/  /_/ /_/ |_||_|     |_|
 * @link https://vistart.me/
 * @copyright Copyright (c) 2016 - 2017 vistart
 * @license https://vistart.me/license/
 */

namespace rhosocial\base\models\traits;

use Yii;
use yii\base\ModelEvent;

/**
 * This trait allow its owner to enable the entity to be blamed by user.
 * @property-read boolean $isConfirmed
 * @property integer $confirmation
 * @property-read array $confirmationRules
 * @property string $confirmCode the confirm code used for confirming the content.
 * You can disable this attribute and create a new model for storing confirm code as
 * its low-frequency usage.
 * @version 1.0
 * @author vistart <<EMAIL>>
 */
trait ConfirmationTrait
{

    /**
     * @var int Unconfirmed.
     */
    public static $confirmFalse = 0;

    /**
     * @var int Confirmed.
     */
    public static $confirmTrue = 1;

    /**
     * @var string|false attribute name of confirmation, or false if disable confirmation features.
     */
    public $confirmationAttribute = false;

    /**
     * @var string This attribute specify the name of confirm_code attribute, if
     * this attribute is assigned to false, this feature will be ignored.
     * if $confirmationAttribute is empty or false, this attribute will be skipped.
     */
    public $confirmCodeAttribute = 'confirm_code';

    /**
     * @var integer The expiration in seconds. If $confirmCodeAttribute is
     * specified, this attribute must be specified.
     */
    public $confirmCodeExpiration = 3600;

    /**
     * @var string This attribute specify the name of confirm_time attribute. if
     * this attribute is assigned to false, this feature will be ignored.
     * if $confirmationAttribute is empty or false, this attribute will be skipped.
     */
    public $confirmTimeAttribute = 'confirmed_at';

    /**
     * @var string initialization confirm time.
     */
    public $initConfirmTime = '1970-01-01 00:00:00';
    public static $eventConfirmationChanged = "confirmationChanged";
    public static $eventConfirmationCanceled = "confirmationCanceled";
    public static $eventConfirmationSuceeded = "confirmationSucceeded";

    /**
     * Apply confirmation.
     * @return boolean
     * @throws \yii\base\NotSupportedException
     */
    public function applyConfirmation()
    {
        if (!$this->confirmCodeAttribute || empty($this->confirmCodeAttribute)) {
            throw new \yii\base\NotSupportedException('This method is not implemented.');
        }
        $this->setConfirmCode($this->generateConfirmationCode());
        return $this->save();
    }

    /**
     * Set confirm code.
     * @param string $code
     */
    public function setConfirmCode($code)
    {
        if (!$this->confirmCodeAttribute || empty($this->confirmCodeAttribute)) {
            return;
        }
        $confirmCodeAttribute = $this->confirmCodeAttribute;
        $this->$confirmCodeAttribute = $code;
        if (!$this->confirmTimeAttribute) {
            return;
        }
        $confirmTimeAttribute = $this->confirmTimeAttribute;
        if (!empty($code)) {
            $this->$confirmTimeAttribute = date('Y-m-d H:i:s');
            return;
        }
        $this->$confirmTimeAttribute = $this->initConfirmTime;
    }

    /**
     * Get confirm code.
     * @return string
     */
    public function getConfirmCode()
    {
        $confirmCodeAttribute = $this->confirmCodeAttribute;
        return (is_string($confirmCodeAttribute) && !empty($confirmCodeAttribute)) ? $this->$confirmCodeAttribute : null;
    }

    /**
     * Confirm the current content.
     * @param string $code
     * @return boolean
     */
    public function confirm($code = '')
    {
        if (!$this->confirmationAttribute || !$this->validateConfirmationCode($code)) {
            return false;
        }
        $this->confirmation = static::$confirmTrue;
        return $this->save();
    }

    /**
     * Generate confirmation code.
     * @return string code
     */
    public function generateConfirmationCode()
    {
        return substr(sha1(Yii::$app->security->generateRandomString()), 0, 17);
    }

    /**
     * Validate the confirmation code.
     * @param string $code
     * @return boolean Whether the confirmation code is valid.
     */
    public function validateConfirmationCode($code)
    {
        $ccAttribute = $this->confirmCodeAttribute;
        if (!$ccAttribute || empty($ccAttribute)) {
            return true;
        }
{'fraction_non_alphanumeric': 0.11855670103092783, 'fraction_numerical': 0.007302405498281787, 'mean_word_length': 2.5103656238220884, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 53, 'https://': 4, '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': '7031701', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2523, 'n_words': 687}