Test Failed
Pull Request — master (#1)
by Carlos
04:03
created

Field   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 95
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeTypecast() 0 5 1
A tableName() 0 3 1
A rules() 0 14 1
A buildValidators() 0 9 2
A attributeLabels() 0 8 1
A getRules() 0 4 1
A getDataType() 0 3 1
1
<?php
2
3
namespace roaresearch\yii2\formgenerator\models;
4
5
use yii\base\Model;
6
use yii\db\ActiveQuery;
7
8
/**
9
 * Model class for table `{{%formgenerator_field}}`
10
 *
11
 * @property integer $id
12
 * @property integer $data_type_id
13
 * @property string $name
14
 * @property string $label
15
 *
16
 * @property DataType $dataType
17
 * @property Rules[] $rules
18
 */
19
class Field extends \roaresearch\yii2\rmdb\models\Entity
20
{
21
    /**
22
     * @var string full class name of the model used in the relation
23
     * `getDataType()`.
24
     */
25
    protected $dataTypeClass = DataType::class;
26
27
    /**
28
     * @var string full class name of the model used in the relation
29
     * `getRules()`.
30
     */
31
    protected $ruleClass = FieldRule::class;
32
33
    /**
34
     * @inheritdoc
35
     */
36 28
    public static function tableName()
37
    {
38 28
        return '{{%formgenerator_field}}';
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 23
    protected function attributeTypecast(): array
45
    {
46 23
        return parent::attributeTypecast() + [
47 23
            'id' => 'integer',
48
            'data_type_id' => 'integer',
49
        ];
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 2
    public function rules()
56
    {
57
        return [
58 2
            [['data_type_id', 'name', 'label'], 'required'],
59
            [['data_type_id'], 'integer'],
60
            [
61
                ['data_type_id'],
62
                'exist',
63
                'skipOnError' => true,
64
                'targetClass' => DataType::class,
65
                'targetAttribute' => ['data_type_id' => 'id'],
66
            ],
67
            [['name', 'label', 'service'], 'string', 'min' => 4],
68
            [['name'], 'unique'],
69
        ];
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 3
    public function attributeLabels()
76
    {
77 3
        return array_merge([
78 3
            'id' => 'ID',
79
            'data_type_id' => 'Data Type ID',
80
            'name' => 'Field name',
81
            'label' => 'Field label',
82 3
        ], parent::attributeLabels());
83
    }
84
85
    /**
86
     * @return ActiveQuery
87
     */
88 6
    public function getDataType(): ActiveQuery
89
    {
90 6
        return $this->hasOne($this->dataTypeClass, ['id' => 'data_type_id']);
91
    }
92
93
    /**
94
     * @return ActiveQuery
95
     */
96 2
    public function getRules(): ActiveQuery
97
    {
98 2
        return $this->hasMany($this->ruleClass, ['field_id' => 'id'])
99 2
            ->inverseOf('field');
100
    }
101
102
    /**
103
     * @return \yii\validators\Validator[]
104
     */
105 2
    public function buildValidators(Model $model, $attributes)
106
    {
107 2
        $validators = [];
108
109 2
        foreach ($this->rules as $rule) {
110 2
            $validators[] = $rule->buildValidator($model, $attributes);
111
        }
112
113 2
        return $validators;
114
    }
115
}
116