<commit_before><?php
/**
 * @author <NAME> <<EMAIL>>
 */

namespace lagman\eav;

use yii\base\Behavior;
use yii\base\InvalidConfigException;
use yii\db\ActiveRecord;

/**
 * Class EavBehavior
 * @package lagman\eav
 *
 * @mixin ActiveRecord
 * @property DynamicModel $eav;
 * @property ActiveRecord $owner
 */
class EavBehavior extends Behavior
{
    public function events() {
        return [
            ActiveRecord::EVENT_AFTER_INSERT   => 'afterSave',
            ActiveRecord::EVENT_AFTER_UPDATE    => 'afterSave',
            ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
        ];
                
    }
    
    /** @var array */
    public $valueClass;

    protected $dynamicModel;

    public function init()
    {
        assert(isset($this->valueClass));
    }

    /**
     * @return DynamicModel
     */
    public function getEav()
    {
        if (!$this->dynamicModel instanceof DynamicModel) {
            $this->dynamicModel = DynamicModel::create([
                'entityModel' => $this->owner,
                'valueClass' => $this->valueClass,
            ]);
        }
        return $this->dynamicModel;
    }
    
    public function beforeValidate() {
        return $this->eav->validate();
    }
    
    public function afterSave() {
        $this->eav->save(false);
    }
    
}<commit_msg>added: parametrized fieldPrefix to EavBehavior<commit_after><?php
/**
 * @author <NAME> <<EMAIL>>
 */

namespace lagman\eav;

use yii\base\Behavior;
use yii\base\InvalidConfigException;
use yii\db\ActiveRecord;

/**
 * Class EavBehavior
 * @package lagman\eav
 *
 * @mixin ActiveRecord
 * @property DynamicModel $eav;
 * @property ActiveRecord $owner
 */
class EavBehavior extends Behavior
{
    public $fieldPrefix = 'eav';

    public function events() {
        return [
            ActiveRecord::EVENT_AFTER_INSERT   => 'afterSave',
            ActiveRecord::EVENT_AFTER_UPDATE    => 'afterSave',
            ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
        ];
                
    }
    
    /** @var array */
    public $valueClass;

    protected $dynamicModel;

    public function init()
    {
        assert(isset($this->valueClass));
    }

    /**
     * @return DynamicModel
     */
    public function getEav()
    {
        if (!$this->dynamicModel instanceof DynamicModel) {
            $this->dynamicModel = DynamicModel::create([
                'entityModel' => $this->owner,
                'valueClass' => $this->valueClass,
                'fieldPrefix' => $this->fieldPrefix,
            ]);
        }
        return $this->dynamicModel;
    }
    
    public function beforeValidate() {
        return $this->eav->validate();
    }
    
    public function afterSave() {
        $this->eav->save(false);
    }
    
}
{'fraction_non_alphanumeric': 0.1296561604584527, 'fraction_numerical': 0.0, 'mean_word_length': 2.159502262443439, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, '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': '6158519', 'n_tokens_mistral': 822, 'n_tokens_neox': 804, 'n_words': 157}