<commit_before><?php

namespace hipanel\behaviors;

use hipanel\models\IndexPageUiOptions;
use Yii;
use yii\base\Behavior;
use yii\base\InvalidConfigException;
use yii\web\Controller;

class UiOptionsBehavior extends Behavior
{
    /**
     * @var mixed
     */
    public $modelClass;

    /**
     * @var array
     */
    public $allowedRoutes = [];

    /**
     * @var IndexPageUiOptions
     */
    private $_model;

    public function init()
    {
        parent::init();

        foreach ($this->allowedRoutes as &$allowedRoute) {
            $allowedRoute = ltrim(Yii::getAlias($allowedRoute), '/');
        }
    }

    public function events()
    {
        return [Controller::EVENT_BEFORE_ACTION => 'ensureOptions'];
    }

    public function ensureOptions($event)
    {
        if ($this->isRouteAllowed($this->getRoute())) {
            $options = [];
            $params = Yii::$app->request->get();
            if ($params) {
                $model = $this->getModel();
                foreach ($params as $key => $value) {
                    if (in_array($key, array_keys($model->toArray()))) {
                        $options[$key] = $value;
                    }
                }
                $model->attributes = $options;
                if ($model->validate()) {
                    $this->getUiOptionsStorage()->set($this->getRoute(), $model->toArray());
                } else {
                    $errors = $model->getErrors();
                }
            }
        }
    }

    protected function getModel()
    {
        if ($this->_model === null) {
            $this->_model = $this->findModel();
        }

        return $this->_model;
    }

    protected function findModel()
    {
        if (isset($this->modelClass['class']) && class_exists($this->modelClass['class'])) {
            return Yii::createObject($this->modelClass);
        } else {
            throw new InvalidConfigException('UiOptionsBehavior::$modelClass must contain `class` item');
        }
    }

    protected function isRouteAllowed($route)
    {
        return in_array($route, $this->allowedRoutes, true);
    }

    protected function getUiOptionsStorage()
    {
        return Yii::$app->get('uiOptionsStorage');
    }

    protected function getRoute()
    {
        return Yii::$app->request->pathInfo;
    }
}<commit_msg>Improved UiOptionsBehavior<commit_after><?php

namespace hipanel\behaviors;

use hipanel\models\IndexPageUiOptions;
use Yii;
use yii\base\Behavior;
use yii\helpers\Inflector;
use yii\web\Controller;

class UiOptionsBehavior extends Behavior
{
    /**
     * @var mixed
     */
    public $modelClass;

    /**
     * @var IndexPageUiOptions
     */
    private $_model;

    public function events()
    {
        return [Controller::EVENT_BEFORE_ACTION => 'ensureUiOptions'];
    }

    public function ensureUiOptions($event)
    {
        if ($this->isRouteAllowed()) {
            $options = [];
            $params = Yii::$app->request->get();
            $model = $this->getModel();
            $model->attributes = $this->getUiOptionsStorage()->get($this->getRoute());
            $model->availableRepresentations = $this->findRepresentations();
            if ($params) {
                foreach ($params as $key => $value) {
                    if (in_array($key, array_keys($model->toArray()))) {
                        $options[$key] = $value;
                    }
                }
                $model->attributes = $options;
                if ($model->validate()) {
                    $this->getUiOptionsStorage()->set($this->getRoute(), $model->toArray());
                } else {
                    $errors = json_encode($model->getErrors());
                    Yii::warning('UiOptionsBehavior - IndexPageUiModel validation errors: ' . $errors);
                }
            }
        }
    }

    protected function getModel()
    {
        if ($this->_model === null) {
            $this->_model = $this->findModel();
        }

        return $this->_model;
    }

    protected function findModel()
    {
        return $this->owner->indexPageUiOptionsModel;
    }

    protected function isRouteAllowed()
    {
        return $this->owner->action->id === 'index';
    }

    protected function getUiOptionsStorage()
    {
        return Yii::$app->get('uiOptionsStorage');
    }

    protected function getRoute()
    {
        return Yii::$app->request->pathInfo;
    }

    protected function findRepresentations()
    {
        $out = [];
        $module = $this->owner->module->id;
        $owner = Inflector::id2camel($this->owner->id);
        $gridClass =  sprintf('\hipanel\modules\%s\grid\%sGridView', $module, $owner);
        if (class_exists($gridClass)) {
            $out = array_keys($gridClass::getRepresentations());
        }

        return $out;
    }
}
{'fraction_non_alphanumeric': 0.1450350226617223, 'fraction_numerical': 0.00020601565718994644, 'mean_word_length': 1.908927501497903, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, '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': '6120435', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1329, 'n_words': 240}