<commit_before><?php

namespace ItBlaster\TranslationBundle\Behavior;
use ItBlaster\TranslationBundle\Traits\TranslationTrait;

/**
 * Базовый класс
 *
 * Class TranslationBehavior
 * @package ItBlaster\TranslationBundle\Behavior
 */
abstract class TranslationBehavior extends \Behavior
{
    use TranslationTrait;

    protected $parameters = array(
        'primary_string'  => '',
    );

    /**
     * Метож сортировки элементов языковых версий
     *
     * @param $script
     */
    protected function addSortI18ns(&$script)
    {
        $locales = $this->getLocales();
        $langs = "array(";
        foreach ($locales as $locale) {
            $langs.='"'.$locale.'",';
        }
        $langs.=')';

        $script .= '
/**
 * Сортирует элементы массива по порядку следования языков
 *
 * @param $elements
 * @return array
 */
protected function sortI18ns($elements) {
    if (count($elements)) {
        $result = new \PropelObjectCollection();
        $langs = array_flip('.$langs.');
        foreach($elements as $element) {
            $result[$langs[$element->getLocale()]] = $element;
        }
        $result->ksort();
        return $result;
    } else {
        return $elements;
    }
}
    ';
    }

    /**
     * Переопределение метода _toString
     *
     * @return string
     * @throws \Exception
     */
    protected function getToStringMethod()
    {
        $primary_string = $this->getParameter('primary_string');
        $i18n_languages = $this->getLocales();
        $primary_string_column =  count($i18n_languages) ? $primary_string : $this->getColumnForParameter('primary_string');
        $get_primary_string = 'get'.(count($i18n_languages) ? $this->CamelCase($primary_string) : $primary_string_column->getPhpName());

        if(!$primary_string_column) {
            $this->exceptionError('Not found column "'.$primary_string.'" in table '.$this->getTable()->getName());
        }

        $toString = '

    /**
     * Отдаём PrimaryString
     *
     * @return string
     */
    public function __toString() {';

        //есть языковые версии
        if (count($i18n_languages)) {
            $languages = 'array(';
            foreach ($i18n_languages as $lang) {
                $languages.='"'.$lang.'",';
            }
            $languages.=')';

            $toString .= '
        $to_string = $this->isNew() ? "Новая запись" : "";
        $languages = '.$languages.';
        foreach ($languages as $language) {
            $str = $this->setLocale($language)->'.$get_primary_string.'();
            if ($str) {
		        $to_string = $str;
                break;
            }
        }
        if (mb_strlen($to_string, "UTF-8")>26) {
            $to_string = mb_substr($to_string, 0, 26, "utf-8")."...";
        }
        return $to_string;';
        } else { //нет языковых версий
            $toString .= '
        return $this->'.$get_primary_string.'() ? $this->'.$get_primary_string.'() : "Новая запись";';
        }
        $toString .= '
    }
    ';
        return $toString;
    }

    /**
     * Перелпределение метода getI18ns
     *
     * @return string
     */
    protected function getI18nsMethod()
    {
        $class_name = $this->CamelCase($this->getTable()->getName());
        $get_i18ns_method = '
    /**
     * Gets an array of FaqQuestionGroupI18n objects which contain a foreign key that references this object.
     *<commit_msg>add getSlug to __toString method<commit_after><?php

namespace ItBlaster\TranslationBundle\Behavior;
use ItBlaster\TranslationBundle\Traits\TranslationTrait;

/**
 * Базовый класс
 *
 * Class TranslationBehavior
 * @package ItBlaster\TranslationBundle\Behavior
 */
abstract class TranslationBehavior extends \Behavior
{
    use TranslationTrait;

    protected $parameters = array(
        'primary_string'  => '',
    );

    /**
     * Метож сортировки элементов языковых версий
     *
     * @param $script
     */
    protected function addSortI18ns(&$script)
    {
        $locales = $this->getLocales();
        $langs = "array(";
        foreach ($locales as $locale) {
            $langs.='"'.$locale.'",';
        }
        $langs.=')';

        $script .= '
/**
 * Сортирует элементы массива по порядку следования языков
 *
 * @param $elementsf
 * @return array
 */
protected function sortI18ns($elements) {
    if (count($elements)) {
        $result = new \PropelObjectCollection();
        $langs = array_flip('.$langs.');
        foreach($elements as $element) {
            $result[$langs[$element->getLocale()]] = $element;
        }
        $result->ksort();
        return $result;
    } else {
        return $elements;
    }
}
    ';
    }

    /**
     * Переопределение метода _toString
     *
     * @return string
     * @throws \Exception
     */
    protected function getToStringMethod()
    {
        $primary_string = $this->getParameter('primary_string');
        $i18n_languages = $this->getLocales();
        $primary_string_column =  count($i18n_languages) ? $primary_string : $this->getColumnForParameter('primary_string');
        $get_primary_string = 'get'.(count($i18n_languages) ? $this->CamelCase($primary_string) : $primary_string_column->getPhpName());

        if(!$primary_string_column) {
            $this->exceptionError('Not found column "'.$primary_string.'" in table '.$this->getTable()->getName());
        }

        $toString = '

    /**
     * Отдаём PrimaryString
     *
     * @return string
     */
    public function __toString() {';

        //есть языковые версии
        if (count($i18n_languages)) {
            $languages = 'array(';
            foreach ($i18n_languages as $lang) {
                $languages.='"'.$lang.'",';
            }
            $languages.=')';

            $toString .= '
        $to_string = $this->isNew() ? "Новая запись" : "";
        $languages = '.$languages.';
        foreach ($languages as $language) {
            $str = $this->setLocale($language)->'.$get_primary_string.'();
            if ($str) {
		        $to_string = $str;
                break;
            }
        }
        if (mb_strlen($to_string, "UTF-8")>26) {
            $to_string = mb_substr($to_string, 0, 26, "utf-8")."...";
        }
        ';
            if ($this->getTable()->hasColumn('slug')) {
                $toString .= '$to_string = $to_string ? $to_string : $this->getSlug();
        ';
            }
        $toString .= 'return $to_string;';
        } else { //нет языковых версий
            $toString .= '
        return $this->'.$get_primary_string.'() ? $this->'.$get_primary_string.'() : "Новая запись";';
        }
        $toString .= '
    }
    ';
        return $toString;
    }

    /**
     * Перелпределение метода getI18ns
     *
     * @return string
     */
    protected function getI18nsMethod()
    {
        $class_name = $this->CamelCase($this->getTable()->getName());
        $get_i18ns_method = '
    /**
     * Gets an array of FaqQuestionGroupI18n objects which contain a foreign key that references this object.
     *
{'fraction_non_alphanumeric': 0.15136898850900837, 'fraction_numerical': 0.008228117463469995, 'mean_word_length': 2.123615418697386, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4869550', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2228, 'n_words': 398}