Test Failed
Pull Request — master (#1)
by Angel
04:13
created

SectionField::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 27
ccs 0
cts 0
cp 0
crap 2
rs 9.6
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;
7
use yii\db\ActiveQuery;
8
9
/**
10
 * Model class for table `{{%formgenerator_form_section_field}}`
11
 *
12
 * @property int $section_id
13
 * @property int $field_id
14
 * @property int $position
15
 * @property string $label
16
 * @property int $created_by
17
 * @property string $created_at
18
 * @property int $updated_by
19
 * @property string $updated_at
20
 *
21
 * @property Section $section
22
 * @property Field $field
23
 * @property SolicitudeValue $solicitudeValues
24
 * @property array $solicitudeValuesData
25
 * @property array $solicitudeValuesDataDetail
26
 */
27
class SectionField extends \roaresearch\yii2\rmdb\models\Entity
28
{
29
    /**
30
     * @var string full class name of the model used in the relation
31
     * `getSection()`.
32
     */
33
    protected $sectionClass = Section::class;
34
35
    /**
36
     * @var string full class name of the model used in the relation
37
     * `getField()`.
38
     */
39
    protected $fieldClass = Field::class;
40
41
    /**
42
     * @var string full class name of the model used in the relation
43
     * `getSolicitudeValues()`.
44
     */
45
    protected $solicitudeValueClass = SolicitudeValue::class;
46 10
47
    /**
48 10
     * @inheritdoc
49
     */
50
    public static function tableName()
51
    {
52
        return '{{%formgenerator_form_section_field}}';
53
    }
54 5
55
    /**
56 5
     * @inheritdoc
57 5
     */
58
    protected function attributeTypecast(): array
59
    {
60
        return parent::attributeTypecast() + [
61
            'section_id' => 'integer',
62
            'field_id' => 'integer',
63
        ];
64
    }
65 2
66
    /**
67
     * @inheritdoc
68 2
     */
69
    public function rules()
70
    {
71
        return [
72
            [['section_id', 'field_id'], 'required'],
73
            [['section_id', 'field_id', 'position'], 'integer'],
74
            [
75
                ['section_id'],
76
                'exist',
77
                'skipOnError' => true,
78
                'targetClass' => Section::class,
79
                'targetAttribute' => ['section_id' => 'id'],
80
            ],
81
            [
82
                ['field_id'],
83
                'exist',
84
                'skipOnError' => true,
85
                'targetClass' => Field::class,
86
                'targetAttribute' => ['field_id' => 'id'],
87
                'message' => 'The field does not exists.',
88
            ],
89
            [['label'], 'trim'],
90
            [['label'], 'string'],
91
            [
92
                ['field_id'],
93
                'unique',
94
                'targetAttribute' => ['section_id', 'field_id'],
95
                'message' => 'Field already associated to the section.',
96
            ],
97
        ];
98
    }
99 3
100
    /**
101
     * @inheritdoc
102 3
     */
103
    public function transactions()
104
    {
105
        return [
106
            self::SCENARIO_DEFAULT => self::OP_ALL,
107
        ];
108
    }
109 5
110
    /**
111 5
     * @inheritdoc
112 5
     */
113
    public function behaviors()
114
    {
115
        return parent::behaviors() + [
116
            'position' => [
117
                'class' => Positionable::class,
118
                'parentAttribute' => 'section_id',
119
            ]
120
        ];
121
    }
122 2
123
    /**
124 2
     * @inheritdoc
125 2
     */
126
    public function attributeLabels()
127
    {
128 2
        return array_merge([
129
            'section_id' => 'Section ID',
130
            'field_id' => 'Field ID',
131
            'label' => 'label',
132
        ], parent::attributeLabels());
133
    }
134 5
135
    /**
136 5
     * @return ActiveQuery
137
     */
138
    public function getSection(): ActiveQuery
139
    {
140
        return $this->hasOne($this->sectionClass, ['id' => 'section_id']);
141
    }
142 4
143
    /**
144 4
     * @return ActiveQuery
145
     */
146
    public function getField(): ActiveQuery
147
    {
148
        return $this->hasOne($this->fieldClass, ['id' => 'field_id']);
149
    }
150 1
151
    /**
152 1
     * @return ActiveQuery
153 1
     */
154
    public function getSolicitudeValues(): ActiveQuery
155 1
    {
156
        return $this->hasMany($this->solicitudeValueClass, [
157
            'field_id' => 'field_id',
158
            'section_id' => 'section_id',
159
        ])->inverseOf('sectionField');
160
    }
161 1
162
    /**
163 1
     * @return ActiveQuery
164 1
     */
165 1
    public function getSolicitudeValuesDetail(): ActiveQuery
166
    {
167 1
        return Yii::configure(
168 1
            $this->getSolicitudeValues(),
169
            ['multiple' => false]
170
        )
171 1
            ->select([
172 1
                'count' => 'count(value)',
173 1
                'countDistinct' => 'count(distinct value)',
174
            ])
175
            ->groupBy(['section_id', 'field_id'])
176
            ->inverseOf(null)
177
            ->asArray();
178
    }
179
}
180