Issues (4)

src/models/BaseLanguageSetting.php (2 issues)

1
<?php
2
namespace andmemasin\language\models;
3
4
use andmemasin\language\interfaces\WithLanguageInterface;
5
use andmemasin\myabstract\MyActiveRecord;
6
use andmemasin\myabstract\interfaces\SettingInterface;
7
use andmemasin\myabstract\TypeInterface;
8
use yii\base\InvalidConfigException;
9
10
/**
11
 * Class BaseLanguageSetting
12
 * @property integer $language_id
13
 * @property integer $is_default
14
 * @property string $value
15
 *
16
 * @property Language $language
17
 * @property TypeInterface $type
18
 *
19
 * @package andmemasin\language\models
20
 */
21
class BaseLanguageSetting extends MyActiveRecord implements  SettingInterface {
22
23
    /** @var string $typeColumn the column containing setting type id */
24
    protected static $typeColumn  = "key";
25
26
    /** @var string  */
27
    public $keyColumn = "key";
28
29
    /** @var string $typeClass Class of setting */
30
    protected  static $typeClass;
31
32
    /** @var WithLanguageInterface */
33
    public $parent;
34
35
36
    /** {@inheritdoc} */
37
    public function init()
38
    {
39
        parent::init();
40
        if($this->insert()){
41
            if(!$this->parent){
42
                throw new InvalidConfigException('Parent Model must be defined while initiating languages-settings');
43
            }
44
            if(!$this->parent instanceof WithLanguageInterface){
0 ignored issues
show
$this->parent is always a sub-type of andmemasin\language\inte...s\WithLanguageInterface.
Loading history...
45
                throw new InvalidConfigException('Parent Model must implement ModelWithLanguages');
46
            }
47
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function rules()
55
    {
56
        return array_merge([
57
            [['is_default', 'language_id'], 'integer'],
58
            [['language_id', 'value'], 'required'],
59
            [['value'], 'string'],
60
            [['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => Language::class, 'targetAttribute' => ['language_id' => 'language_id']],
61
        ],  parent::rules());
62
    }
63
64
    /**
65
     * @param MyActiveRecord $parent
66
     * @param Language $language
67
     * @param string $key
68
     * @return bool|static|array
69
     */
70
    public static function getSetting($parent, $language, $key){
71
72
        /** @var TypeInterface $typeClass */
73
        $typeClass = static::$typeClass;
74
        /** @var TypeInterface $type */
75
        $type = $typeClass::getByKey($key);
76
77
        if(!empty($type)){
78
            /** @var BaseLanguageSetting $model */
79
            $query = static::find();
80
            if(!empty($parent)){
81
                $query->andWhere([$parent->primaryKeySingle() =>$parent->primaryKey]);
82
            }
83
            $model =$query->andWhere(['language_id'=>$language->primaryKey])
84
                ->andWhere([$type->primaryKeySingle()=>$type->{$type->primaryKeySingle()}])
85
                ->one();
86
87
            if(empty($model)){
88
                /** @var BaseLanguageSetting $model */
89
                $model = \Yii::createObject(['class'=>static::class]);
90
                $model->language_id = $language->primaryKey;
91
                if(!empty($parent)){
92
                    $model->{$parent->primaryKeySingle()} = $parent->primaryKey;
93
                }
94
                $model->{$type->primaryKeySingle()}=$type->{$type->primaryKeySingle()};
95
96
            }
97
            return $model;
98
        }
99
100
        throw new InvalidConfigException('Undefined type key: ' . $key);
101
102
    }
103
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function behaviors()
109
    {
110
        return array_merge([
111
            'actionlog' => [
112
                'class' => 'andmemasin\actionlog\behaviors\ActionLogBehavior',
113
            ],
114
        ], parent::behaviors());
115
    }
116
117
    /**
118
     * @return \yii\db\ActiveQuery
119
     */
120
    public function getLanguage()
121
    {
122
        return $this->hasOne(Language::class, ['language_id' => 'language_id']);
123
    }
124
125
126
    /**
127
     * @return TypeInterface
128
     */
129
    public function getType()
130
    {
131
        /** @var TypeInterface $typeClass */
132
        $typeClass = static::$typeClass;
133
        return $typeClass::getByKey(static::$typeColumn);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $typeClass::getByKey(static::typeColumn) returns the type yii\base\Model which is incompatible with the documented return type andmemasin\myabstract\TypeInterface.
Loading history...
134
    }
135
136
    /**
137
     * @param $key string
138
     * @return static
139
     */
140
    public function findOneByKey($key){
141
        /** @var static $model */
142
        $model = static::find()
143
            ->andWhere([static::$typeColumn => $key])
144
            ->one();
145
        return $model;
146
    }
147
148
}