Completed
Push — master ( 1bc3c7...02ce33 )
by Tõnis
02:52
created

BaseSurvey   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 50
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLanguages() 0 5 1
A getLanguage() 0 3 1
A getQuestions() 0 3 1
A surveyLanguages() 0 3 1
A rules() 0 6 1
1
<?php
2
3
namespace dameter\abstracts\models;
4
5
use dameter\abstracts\DActiveRecord;
6
use yii\helpers\ArrayHelper;
7
8
/**
9
 * Class BaseSurvey
10
 *
11
 * @property int $survey_id
12
 * @property string $name Survey name. Primarily meant for back-end usage.
13
 * @property int $language_id base language id
14
 *
15
 * @property BaseQuestion[] $questions
16
 * @property Language[] $languages
17
 * @property Language $language
18
 *
19
 * @package dameter\abstracts\models
20
 * @author Tõnis Ormisson <[email protected]>
21
 */
22
abstract class BaseSurvey extends DActiveRecord
23
{
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function rules()
29
    {
30
        return [
31
            [['name'], 'string', 'max' => 254],
32
            [['language_id'], 'integer'],
33
            [['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => Language::class, 'targetAttribute' => ['language_id' => 'language_id']],
34
        ];
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getLanguage()
41
    {
42
        return $this->hasMany(Language::class);
43
    }
44
45
    /**
46
     * @return \yii\db\ActiveQuery
47
     * @throws \yii\base\NotSupportedException
48
     */
49
    public function getLanguages()
50
    {
51
        $relations = $this->surveyLanguages();
52
        $ids = ArrayHelper::getColumn($relations, SurveyLanguage::primaryKeySingle());
53
        return Language::find()->andWhere(['in', Language::primaryKeySingle(), $ids]);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getQuestions()
60
    {
61
        return $this->hasMany(BaseQuestion::class);
62
    }
63
64
65
    /**
66
     * @return SurveyLanguage[]
67
     * @throws \yii\base\NotSupportedException
68
     */
69
    private function surveyLanguages()
70
    {
71
        return SurveyLanguage::getChildren($this);
72
    }
73
74
}