Completed
Push — master ( 7f45a1...6afdf1 )
by Andrey
01:18
created

MultilanguageValidateComponent   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 0
loc 37
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A setModel() 0 18 2
1
<?php
2
3
namespace Itstructure\AdminModule\components;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use yii\db\ActiveRecordInterface;
8
use yii\base\{Component, InvalidConfigException};
9
use Itstructure\AdminModule\{
10
    models\MultilanguageValidateModel,
11
    interfaces\ModelInterface,
12
    interfaces\ValidateComponentInterface
13
};
14
15
/**
16
 * Class MultilanguageValidateComponent
17
 * Component class for validation multilanguage fields.
18
 *
19
 * @property array $models Array of models with multilanguage fields.
20
 *
21
 * @package Itstructure\AdminModule\components
22
 *
23
 * @author Andrey Girnik <[email protected]>
24
 */
25
class MultilanguageValidateComponent extends Component implements ValidateComponentInterface
26
{
27
    /**
28
     * Array of models with multilanguage fields.
29
     *
30
     * @var array
31
     */
32
    public $models = [];
33
34
    /**
35
     * Sets a specific model for the general validation model.
36
     *
37
     * @param ActiveRecordInterface $mainModel
38
     *
39
     * @throws InvalidConfigException
40
     *
41
     * @return ModelInterface
42
     */
43
    public function setModel(ActiveRecordInterface $mainModel): ModelInterface
44
    {
45
        $config = $this->models[$mainModel::tableName()];
46
47
        if (!is_array($config['dynamicFields'])) {
48
            throw new InvalidConfigException('No dynamic fields are specified.');
49
        }
50
51
        /** @var ModelInterface $object */
52
        $object = Yii::createObject(ArrayHelper::merge(
53
            [
54
                'class' => MultilanguageValidateModel::class,
55
                'mainModel' => $mainModel,
56
            ], $config)
57
        );
58
59
        return $object;
60
    }
61
}
62