Completed
Push — master ( ccfeeb...7d9725 )
by Alexey
02:50 queued 46s
created

Passfield::run()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 0
1
<?php
2
3
namespace modules\users\widgets\passfield;
4
5
use yii\base\InvalidConfigException;
6
use yii\helpers\Html;
7
use yii\widgets\InputWidget;
8
use modules\users\widgets\passfield\assets\PassfieldAsset;
9
10
/**
11
 * Class Passfield
12
 * @package modules\users\widgets\passfield
13
 */
14
class Passfield extends InputWidget
15
{
16
    /**
17
     * @var \yii\widgets\ActiveForm
18
     */
19
    public $form;
20
    /**
21
     * @var array Passfield options
22
     */
23
    public $config = [];
24
25
    /**
26
     * @var string
27
     */
28
    public $label;
29
30
    /**
31
     * @inheritdoc
32
     * @return $this|string
33
     * @throws InvalidConfigException
34
     */
35
    public function run()
36
    {
37
        $this->registerAssets();
38
        if ($this->hasModel()) {
39
            if ($this->form == null) {
40
                throw new InvalidConfigException(__CLASS__ . '.form property must be specified');
41
            }
42
            if (empty($this->label))
43
                return $this->form->field($this->model, $this->attribute)->passwordInput($this->options);
44
            return $this->form->field($this->model, $this->attribute)->passwordInput($this->options)->label($this->label);
45
        } else {
46
            return Html::passwordInput($this->name, $this->value, $this->options);
47
        }
48
    }
49
50
    /**
51
     * Register Assets
52
     */
53
    public function registerAssets()
54
    {
55
        PassfieldAsset::register($this->view);
56
        $config = empty($this->config) ? json_encode(['locale' => \Yii::$app->language]) : json_encode($this->config);
57
        $this->view->registerJs(sprintf('$("#%s").passField(%s)', $this->options['id'], $config));
58
    }
59
}
60