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

MultilanguageValidateComponent::setModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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