SolicitudeValue::attributeLabels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace roaresearch\yii2\formgenerator\models;
4
5
use yii\db\ActiveQuery;
6
7
/**
8
 * Model class for table `{{%formgenerator_solicitude_value}}`
9
 *
10
 * @property int $section_id
11
 * @property int $field_id
12
 * @property int $solicitude_id
13
 * @property string $value
14
 * @property int $created_by
15
 * @property string $created_at
16
 * @property int $updated_by
17
 * @property string $updated_at
18
 *
19
 * @property SectionField $sectionField
20
 * @property Section $section
21
 * @property Field $field
22
 * @property Solicitude $solicitude
23
 */
24
class SolicitudeValue extends \roaresearch\yii2\rmdb\models\Entity
25
{
26
    /**
27
     * @var string full class name of the model used in the relation
28
     * `getSectionField()`.
29
     */
30
    protected $sectionFieldClass = SectionField::class;
31
32
    /**
33
     * @var string full class name of the model used in the relation
34
     * `getSection()`.
35
     */
36
    protected $sectionClass = Section::class;
37
38
    /**
39
     * @var string full class name of the model used in the relation
40
     * `getField()`.
41
     */
42
    protected $fieldClass = Field::class;
43
44
    /**
45
     * @var string full class name of the model used in the relation
46
     * `getSolicitude()`.
47
     */
48
    protected $solicitudeClass = Solicitude::class;
49
50
    /**
51
     * @inheritdoc
52
     */
53 10
    public static function tableName()
54
    {
55 10
        return '{{%formgenerator_solicitude_value}}';
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 6
    protected function attributeTypecast(): array
62
    {
63 6
        return parent::attributeTypecast() + [
64 6
            'section_id' => 'integer',
65
            'field_id' => 'integer',
66
            'solicitude_id' => 'integer',
67
        ];
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 2
    public function rules()
74
    {
75
        return [
76 2
            [['section_id', 'field_id', 'solicitude_id'], 'required'],
77
            [['section_id', 'field_id', 'solicitude_id'], 'integer'],
78
            [
79
                ['solicitude_id'],
80
                'exist',
81
                'skipOnError' => true,
82
                'targetClass' => Solicitude::class,
83
                'targetAttribute' => ['solicitude_id' => 'id'],
84
            ],
85
            [
86
                ['section_id'],
87 2
                'exist',
88
                'skipOnError' => true,
89
                'targetClass' => Section::class,
90
                'targetAttribute' => ['section_id' => 'id'],
91 2
                'when' => function () {
92 2
                    return !$this->hasErrors('solicitude_id');
93 2
                },
94 2
                'filter' => function ($query) {
95 2
                    $query->andWhere(['form_id' => $this->solicitude->form_id]);
96 2
                },
97 2
                'message' => 'Section is not associated to the form.',
98
            ],
99
            [
100
                ['field_id'],
101 2
                'exist',
102
                'skipOnError' => true,
103
                'targetClass' => SectionField::class,
104
                'targetAttribute' => [
105
                    'section_id' => 'section_id',
106
                    'field_id' => 'field_id',
107
                ],
108 2
                'when' => function () {
109 2
                    return !$this->hasErrors('section_id');
110 2
                },
111 2
                'message' => 'Field not associated to the Section.',
112
            ],
113
            [
114
                ['field_id'],
115 2
                'unique',
116
                'targetAttribute' => [
117
                    'section_id',
118
                    'field_id',
119
                    'solicitude_id',
120
                ],
121 2
                'when' => function () {
122 2
                    return !$this->hasErrors('section_id')
123 2
                        && !$this->hasErrors('solicitude_id');
124 2
                },
125 2
                'message' => 'Field already filled.',
126
            ],
127
            [['value'], 'trim'],
128
        ];
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134 2
    public function afterValidate()
135
    {
136 2
        if (!$this->hasErrors()) {
137 2
            $field = $this->getField()
138 2
                ->with([
139 2
                    'dataType',
140 2
                    'rules' => function ($query) {
141 2
                        $query->modelClass = FieldRule::class;
142 2
                    },
143 2
                    'rules.properties',
144
                ])
145 2
                ->one();
146 2
            $this->populateRelation('field', $field);
147
            foreach (
148 2
                $field->buildValidators($this, 'value') as $validator
149
            ) {
150 2
                $validator->validateAttributes($this, ['value']);
151
            }
152 2
            $field->dataType->castValue($this, 'value');
0 ignored issues
show
Bug introduced by
The method castValue() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
            $field->dataType->/** @scrutinizer ignore-call */ 
153
                              castValue($this, 'value');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
        }
154 2
        parent::afterValidate();
155 2
    }
156
157
    /**
158
     * @inheritdoc
159
     */
160 5
    public function attributeLabels()
161
    {
162 5
        return array_merge([
163 5
            'section_id' => 'Section ID',
164
            'field_id' => 'Field ID',
165
            'label' => 'label',
166 5
        ], parent::attributeLabels());
167
    }
168
169
    /**
170
     * @return ActiveQuery
171
     */
172
    public function getSectionField(): ActiveQuery
173
    {
174
        return $this->hasOne(
175
            $this->sectionFieldClass,
176
            ['section_id' => 'section_id', 'field_id' => 'field_id']
177
        );
178
    }
179
180
    /**
181
     * @return ActiveQuery
182
     */
183 5
    public function getSection(): ActiveQuery
184
    {
185 5
        return $this->hasOne($this->sectionClass, ['id' => 'section_id']);
186
    }
187
188
    /**
189
     * @return ActiveQuery
190
     */
191 5
    public function getField(): ActiveQuery
192
    {
193 5
        return $this->hasOne($this->fieldClass, ['id' => 'field_id']);
194
    }
195
196
    /**
197
     * @return ActiveQuery
198
     */
199 6
    public function getSolicitude(): ActiveQuery
200
    {
201 6
        return $this->hasOne($this->solicitudeClass, ['id' => 'solicitude_id']);
202
    }
203
}
204