Completed
Pull Request — master (#1)
by Angel
07:01
created

Section::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 20
ccs 2
cts 2
cp 1
crap 1
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace roaresearch\yii2\formgenerator\models;
4
5
use roaresearch\yii2\formgenerator\behaviors\Positionable;
6
use yii\db\ActiveQuery;
7
8
/**
9
 * Model class for table `{{%formgenerator_form_section}}`
10
 *
11
 * @property integer $id
12
 * @property integer $form_id
13
 * @property integer $position
14
 * @property string $name
15
 * @property string $label
16
 *
17
 * @property Form $form
18
 * @property SectionField[] $sectionFields
19
 * @property Field[] $fieds
20
 */
21
class Section extends \roaresearch\yii2\rmdb\models\Entity
22
{
23
    /**
24
     * @var string full class name of the model used in the relation
25
     * `getForm()`.
26
     */
27
    protected $formClass = Form::class;
28
29
    /**
30
     * @var string full class name of the model used in the relation
31
     * `getSectionFields()`.
32
     */
33
    protected $sectionFieldClass = SectionField::class;
34
35
    /**
36
     * @var string full class name of the model used in the relation
37
     * `getFields()`.
38
     */
39
    protected $fieldClass = Field::class;
40
41
    /**
42
     * @inheritdoc
43
     */
44 20
    public static function tableName()
45
    {
46 20
        return '{{%formgenerator_form_section}}';
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 15
    protected function attributeTypecast(): array
53
    {
54 15
        return parent::attributeTypecast() + [
55 15
            'id' => 'integer',
56
            'form_id' => 'integer',
57
        ];
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 3
    public function transactions()
64
    {
65
        return [
66 3
            self::SCENARIO_DEFAULT => self::OP_ALL,
67
        ];
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 15
    public function behaviors()
74
    {
75 15
        return parent::behaviors() + [
76 15
            'position' => [
77
                'class' => Positionable::class,
78
                'parentAttribute' => 'form_id',
79
            ]
80
        ];
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 2
    public function rules()
87
    {
88
        return [
89 2
            [['form_id', 'name', 'label'], 'required'],
90
            [['form_id', 'position'], 'integer'],
91
            [
92
                ['form_id'],
93
                'exist',
94
                'skipOnError' => true,
95
                'targetClass' => Form::class,
96
                'targetAttribute' => ['form_id' => 'id'],
97
            ],
98
            [['name', 'label'], 'trim'],
99
            [['name', 'label'], 'string', 'min' => 6],
100
            [
101
                ['name'],
102
                'unique',
103
                'targetAttribute' => ['form_id', 'name'],
104
                'message' => 'Section name "{value}" already in use '
105
                    . 'for this form.',
106
            ],
107
        ];
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113 3
    public function attributeLabels()
114
    {
115 3
        return array_merge([
116 3
            'id' => 'ID',
117
            'form_id' => 'Form ID',
118
            'name' => 'Section name',
119
            'label' => 'Section label',
120 3
        ], parent::attributeLabels());
121
    }
122
123
    /**
124
     * @return ActiveQuery
125
     */
126 15
    public function getForm(): ActiveQuery
127
    {
128 15
        return $this->hasOne($this->formClass, ['id' => 'form_id']);
129
    }
130
131
    /**
132
     * @return ActiveQuery
133
     */
134
    public function getSectionFields(): ActiveQuery
135
    {
136
        return $this->hasMany($this->sectionFieldClass, ['section_id' => 'id'])
137
            ->inverseOf('section');
138
    }
139
140
    /**
141
     * @return ActiveQuery sibling stages for the same workflow
142
     */
143
    public function getFields(): ActiveQuery
144
    {
145
        return $this->hasMany($this->fieldClass, ['id' => 'field_id'])
146
            ->via('sectionFields');
147
    }
148
}
149