<commit_before><?php

namespace Zelenin\yii\behaviors;

use dosamigos\transliterator\TransliteratorHelper;
use Yii;
use yii\base\InvalidConfigException;
use yii\behaviors\SluggableBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\validators\UniqueValidator;

class Slug extends SluggableBehavior
{
    /** @var string */
    public $slugAttribute = 'slug';
    /** @var string|array */
    public $attribute = 'name';

    /** @var bool */
    public $ensureUnique = true;
    /** @var bool */
    public $translit = true;
    /** @var string */
    public $replacement = '-';
    /** @var bool */
    public $lowercase = true;
    /** @var bool */
    public $immutable = true;
    /**
     * @var string
     * @link http://userguide.icu-project.org/transforms/general
     */
    public $transliterateOptions = '';
    /** @var bool */
    private $slugIsEmpty = false;

    /**
     * @inheritdoc
     */
    public function attach($owner)
    {
        $this->attribute = (array)$this->attribute;
        $primaryKey = $owner->primaryKey();
        $primaryKey = is_array($primaryKey)
            ? array_shift($primaryKey)
            : $primaryKey;
        if (in_array($primaryKey, $this->attribute) && $owner->getIsNewRecord()) {
            $this->attributes[ActiveRecord::EVENT_AFTER_INSERT] = $this->slugAttribute;
        }

        parent::attach($owner);
    }

    /**
     * @inheritdoc
     */
    protected function getValue($event)
    {
        /* @var $owner ActiveRecord */
        $owner = $this->owner;

        if (!empty($owner->{$this->slugAttribute}) && !$this->slugIsEmpty && $this->immutable) {
            $slug = $owner->{$this->slugAttribute};
        } else {
            if ($owner->getIsNewRecord()) {
                $this->slugIsEmpty = true;
            }
            if ($this->attribute !== null) {
                $attributes = $this->attribute;

                $slugParts = [];
                foreach ($attributes as $attribute) {
                    $slugParts[] = ArrayHelper::getValue($this->owner, $attribute);
                }
                $slug = $this->slug(implode($this->replacement, $slugParts), $this->replacement, $this->lowercase);

                if (!$owner->getIsNewRecord() && $this->slugIsEmpty) {
                    $owner->{$this->slugAttribute} = $slug;
                    $owner->save(false, [$this->slugAttribute]);
                }
            } else {
                $slug = parent::getValue($event);
            }
        }

        if ($this->ensureUnique) {
            $baseSlug = $slug;
            $iteration = 0;
            while (!$this->validateSlug($slug)) {
                $iteration++;
                $slug = $this->generateUniqueSlug($baseSlug, $iteration);
            }
        }

        return $slug;
    }

    /**
     * @param string $string
     * @param string $replacement
     * @param bool $lowercase
     * @return string
     */
    private function slug($string, $replacement = '-', $lowercase = true)
    {
        if ($this->translit) {
            $string = $this->transliterate($string);
            $string = preg_replace('/[^a-zA-Z0-9=\s—–-]+/u', '', $string);
        }

        $string = preg_replace('/[=\s—–-]+/u', $replacement, $string);
        $string = trim($string, $replacement);
        return $lowercase ? mb_strtolower($string) : $string;
    }

    /**
     * @param string $string
     * @return string
     */
    private function transliterate($string)
    {
        if (extension_loaded('intl') === true) {
            $options = rtrim(trim($this->transliterateOptions), ';');
            if ($options) {
                $options = $options . ';';
            }
            $options .= 'Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC;';
            return transliterator_transliterate($options, $string);
        } else {
            return TransliteratorHelper::process($string);
        }
    }

    /**
     * @param string $slug
     * @return bool
     * @throws InvalidConfigException
     */
    private function validateSlug($slug)
    {
        $validator = Yii::createObject(array_merge(['class' => UniqueValidator::className()], $this->uniqueValidator));

        /** @var ActiveRecord $model */
        $model = clone $this->owner;
        $model->clearErrors();
        $model->{$this->slugAttribute} = $slug;<commit_msg>fix id to transliterate<commit_after><?php

namespace Zelenin\yii\behaviors;

use dosamigos\transliterator\TransliteratorHelper;
use Yii;
use yii\base\InvalidConfigException;
use yii\behaviors\SluggableBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\validators\UniqueValidator;

class Slug extends SluggableBehavior
{
    /** @var string */
    public $slugAttribute = 'slug';
    /** @var string|array */
    public $attribute = 'name';

    /** @var bool */
    public $ensureUnique = true;
    /** @var bool */
    public $translit = true;
    /** @var string */
    public $replacement = '-';
    /** @var bool */
    public $lowercase = true;
    /** @var bool */
    public $immutable = true;
    /**
     * @var string
     * @link http://userguide.icu-project.org/transforms/general
     */
    public $transliterateOptions = '';
    /** @var bool */
    private $slugIsEmpty = false;

    /**
     * @inheritdoc
     */
    public function attach($owner)
    {
        $this->attribute = (array)$this->attribute;
        $primaryKey = $owner->primaryKey();
        $primaryKey = is_array($primaryKey)
            ? array_shift($primaryKey)
            : $primaryKey;
        if (in_array($primaryKey, $this->attribute) && $owner->getIsNewRecord()) {
            $this->attributes[ActiveRecord::EVENT_AFTER_INSERT] = $this->slugAttribute;
        }

        parent::attach($owner);
    }

    /**
     * @inheritdoc
     */
    protected function getValue($event)
    {
        /* @var $owner ActiveRecord */
        $owner = $this->owner;

        if (!empty($owner->{$this->slugAttribute}) && !$this->slugIsEmpty && $this->immutable) {
            $slug = $owner->{$this->slugAttribute};
        } else {
            if ($owner->getIsNewRecord()) {
                $this->slugIsEmpty = true;
            }
            if ($this->attribute !== null) {
                $attributes = $this->attribute;

                $slugParts = [];
                foreach ($attributes as $attribute) {
                    $slugParts[] = ArrayHelper::getValue($this->owner, $attribute);
                }
                $slug = $this->slug(implode($this->replacement, $slugParts), $this->replacement, $this->lowercase);

                if (!$owner->getIsNewRecord() && $this->slugIsEmpty) {
                    $owner->{$this->slugAttribute} = $slug;
                    $owner->save(false, [$this->slugAttribute]);
                }
            } else {
                $slug = parent::getValue($event);
            }
        }

        if ($this->ensureUnique) {
            $baseSlug = $slug;
            $iteration = 0;
            while (!$this->validateSlug($slug)) {
                $iteration++;
                $slug = $this->generateUniqueSlug($baseSlug, $iteration);
            }
        }

        return $slug;
    }

    /**
     * @param string $string
     * @param string $replacement
     * @param bool $lowercase
     * @return string
     */
    private function slug($string, $replacement = '-', $lowercase = true)
    {
        if ($this->translit) {
            $string = $this->transliterate($string);
            $string = preg_replace('/[^a-zA-Z0-9=\s—–-]+/u', '', $string);
        }

        $string = preg_replace('/[=\s—–-]+/u', $replacement, $string);
        $string = trim($string, $replacement);
        return $lowercase ? mb_strtolower($string) : $string;
    }

    /**
     * @param string $string
     * @return string
     */
    private function transliterate($string)
    {
        if (extension_loaded('intl') === true) {
            $options = rtrim(trim($this->transliterateOptions), ';');
            if ($options) {
                $options = $options . ';';
            } else {
            	$options = 'Any-Latin; [:Nonspacing Mark:] Remove;';
            }
	    return transliterator_transliterate($options, $string);
        } else {
            return TransliteratorHelper::process($string);
        }
    }

    /**
     * @param string $slug
     * @return bool
     * @throws InvalidConfigException
     */
    private function validateSlug($slug)
    {
        $validator = Yii::createObject(array_merge(['class' => UniqueValidator::className()], $this->uniqueValidator));

        /** @var ActiveRecord $model */
        $model = clone $this->owner;
        $model->clearErrors();
        $model->{$this->slugAttribute} = $slug;
{'fraction_non_alphanumeric': 0.15877879922823743, 'fraction_numerical': 0.0006809669731018045, 'mean_word_length': 2.0013623978201633, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 85, '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': '6447737', 'n_tokens_mistral': 2584, 'n_tokens_neox': 2573, 'n_words': 472}