ProfileValidateComponent::getFormFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Itstructure\UsersModule\components;
4
5
use Yii;
6
use yii\db\ActiveRecordInterface;
7
use yii\web\IdentityInterface;
8
use yii\base\{Component, InvalidConfigException};
9
use yii\rbac\ManagerInterface;
10
use Itstructure\UsersModule\{
11
    interfaces\ModelInterface,
12
    interfaces\ValidateComponentInterface,
13
    models\ProfileValidate
14
};
15
16
/**
17
 * Class ProfileValidateComponent
18
 * Component class for validation user fields.
19
 *
20
 * @property array $rules
21
 * @property array $attributes
22
 * @property array $attributeLabels
23
 * @property bool  $rbacManage
24
 * @property bool  $customRewrite
25
 * @property array $formFields
26
 * @property string|callable $formTemplate Form fields template.
27
 * @property array $indexViewColumns
28
 * @property array $detailViewAttributes
29
 * @property ManagerInterface $authManager
30
 *
31
 * @package Itstructure\UsersModule\components
32
 */
33
class ProfileValidateComponent extends Component implements ValidateComponentInterface
34
{
35
    /**
36
     * Validate fields with rules.
37
     *
38
     * @var array
39
     */
40
    private $rules = [];
41
42
    /**
43
     * Attributes.
44
     *
45
     * @var array
46
     */
47
    private $attributes = [];
48
49
    /**
50
     * Attribute labels.
51
     *
52
     * @var array
53
     */
54
    private $attributeLabels = [];
55
56
    /**
57
     * Set manage for roles.
58
     *
59
     * @var bool
60
     */
61
    private $rbacManage = false;
62
63
    /**
64
     * Rewrite rules, labels, attributes by custom.
65
     *
66
     * @var bool
67
     */
68
    private $customRewrite = false;
69
70
    /**
71
     * Form fields for the template.
72
     *
73
     * @var array
74
     */
75
    private $formFields = [];
76
77
    /**
78
     * Form fields template.
79
     *
80
     * @var string|callable
81
     */
82
    private $formTemplate = '';
83
84
    /**
85
     * Columns for GridView widget in index template file.
86
     *
87
     * @var array
88
     */
89
    private $indexViewColumns = [];
90
91
    /**
92
     * Attributes for DetailView widget in view template file.
93
     *
94
     * @var array
95
     */
96
    private $detailViewAttributes = [];
97
98
    /**
99
     * Auth manager.
100
     *
101
     * @var ManagerInterface
102
     */
103
    private $authManager;
104
105
    /**
106
     * Initialize.
107
     */
108
    public function init()
109
    {
110
        if ($this->rbacManage && null === $this->authManager){
111
            throw new InvalidConfigException('The authManager is not defined.');
112
        }
113
    }
114
115
    /**
116
     * Set validate fields with rules.
117
     *
118
     * @param array $rules
119
     */
120
    public function setRules(array $rules): void
121
    {
122
        $this->rules = $rules;
123
    }
124
125
    /**
126
     * Get validate fields with rules.
127
     *
128
     * @return array
129
     */
130
    public function getRules(): array
131
    {
132
        return $this->rules;
133
    }
134
135
    /**
136
     * Set attributes.
137
     *
138
     * @param array $attributes
139
     */
140
    public function setAttributes(array $attributes): void
141
    {
142
        $this->attributes = $attributes;
143
    }
144
145
    /**
146
     * Get attributes.
147
     *
148
     * @return array
149
     */
150
    public function getAttributes(): array
151
    {
152
        return $this->attributes;
153
    }
154
155
    /**
156
     * Set attribute labels.
157
     *
158
     * @param array $attributeLabels
159
     */
160
    public function setAttributeLabels(array $attributeLabels): void
161
    {
162
        $this->attributeLabels = $attributeLabels;
163
    }
164
165
    /**
166
     * Get attribute labels.
167
     *
168
     * @return array
169
     */
170
    public function getAttributeLabels(): array
171
    {
172
        return $this->attributeLabels;
173
    }
174
175
    /**
176
     * Set manage for roles.
177
     *
178
     * @param bool $rbacManage
179
     */
180
    public function setRbacManage(bool $rbacManage): void
181
    {
182
        $this->rbacManage = $rbacManage;
183
    }
184
185
    /**
186
     * Is manage for roles.
187
     *
188
     * @return bool
189
     */
190
    public function getRbacManage(): bool
191
    {
192
        return $this->rbacManage;
193
    }
194
195
    /**
196
     * Set rewrite rules, labels, attributes by custom.
197
     *
198
     * @param bool $customRewrite
199
     */
200
    public function setCustomRewrite(bool $customRewrite): void
201
    {
202
        $this->customRewrite = $customRewrite;
203
    }
204
205
    /**
206
     * Get rewrite rules, labels, attributes by custom.
207
     *
208
     * @return bool
209
     */
210
    public function getCustomRewrite(): bool
211
    {
212
        return $this->customRewrite;
213
    }
214
215
    /**
216
     * Set form fields for the template.
217
     *
218
     * @param array $formFields
219
     */
220
    public function setFormFields(array $formFields): void
221
    {
222
        $this->formFields = $formFields;
223
    }
224
225
    /**
226
     * Get Form fields for the template.
227
     *
228
     * @return array
229
     */
230
    public function getFormFields(): array
231
    {
232
        return $this->formFields;
233
    }
234
235
    /**
236
     * Set form fields template.
237
     *
238
     * @param string|callable $formTemplate
239
     */
240
    public function setFormTemplate($formTemplate): void
241
    {
242
        $this->formTemplate = $formTemplate;
243
    }
244
245
    /**
246
     * Get form fields template.
247
     *
248
     * @return string|callable
249
     */
250
    public function getFormTemplate()
251
    {
252
        return $this->formTemplate;
253
    }
254
255
    /**
256
     * Set columns for GridView widget in index template file.
257
     *
258
     * @param array $indexViewColumns
259
     */
260
    public function setIndexViewColumns(array $indexViewColumns): void
261
    {
262
        $this->indexViewColumns = $indexViewColumns;
263
    }
264
265
    /**
266
     * Get columns for GridView widget in index template file.
267
     *
268
     * @return array
269
     */
270
    public function getIndexViewColumns(): array
271
    {
272
        return $this->indexViewColumns;
273
    }
274
275
    /**
276
     * Set attributes for DetailView widget in view template file.
277
     *
278
     * @param array $detailViewAttributes
279
     */
280
    public function setDetailViewAttributes(array $detailViewAttributes): void
281
    {
282
        $this->detailViewAttributes = $detailViewAttributes;
283
    }
284
285
    /**
286
     * Get attributes for DetailView widget in view template file.
287
     *
288
     * @return array
289
     */
290
    public function getDetailViewAttributes(): array
291
    {
292
        return $this->detailViewAttributes;
293
    }
294
295
    /**
296
     * Set authManager (RBAC).
297
     *
298
     * @param ManagerInterface $authManager
299
     */
300
    public function setAuthManager(ManagerInterface $authManager): void
301
    {
302
        $this->authManager = $authManager;
303
    }
304
305
    /**
306
     * Get authManager (RBAC).
307
     *
308
     * @return ManagerInterface
309
     */
310
    public function getAuthManager(): ManagerInterface
311
    {
312
        return $this->authManager;
313
    }
314
315
    /**
316
     * Set a user model for ProfileValidateComponent validation model.
317
     *
318
     * @param $model
319
     *
320
     * @throws InvalidConfigException
321
     *
322
     * @return ModelInterface
323
     */
324
    public function setModel($model): ModelInterface
325
    {
326
        ProfileValidate::checkProfileModel($model);
327
328
        /** @var ModelInterface $object */
329
        $object = Yii::createObject([
330
            'class' => ProfileValidate::class,
331
            'profileModel' => $model,
332
            'rules' => $this->rules,
333
            'attributes' => $this->attributes,
334
            'attributeLabels' => $this->attributeLabels,
335
            'rbacManage' => $this->rbacManage,
336
            'customRewrite' => $this->customRewrite,
337
            'authManager' => $this->authManager,
338
        ]);
339
340
        return $object;
341
    }
342
}
343