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

SectionField::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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