PellInputWidget   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 2
b 0
f 0
dl 0
loc 43
ccs 7
cts 7
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasModel() 0 3 2
A init() 0 6 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2013-2019 Sergio Coderius!
4
 * @link https://coderius.biz.ua
5
 * @license This program is free software: the MIT License (MIT)
6
 */
7
8
namespace coderius\pell;
9
10
use yii\base\Model;
11
use yii\helpers\Html;
12
use yii\base\Widget;
13
14
/**
15
 * InputWidget is the base class for Pell widget.
16
 *
17
 * Classes extending from this widget can be used in an [[yii\widgets\ActiveForm|ActiveForm]]
18
 * using the [[yii\widgets\ActiveField::widget()|widget()]] method, for example like this:
19
 *
20
 * ```php
21
 * <?= $form->field($model, 'from_date')->widget('WidgetClassName', [
22
 *     // configure additional widget properties here
23
 * ]) ?>
24
 * ```
25
 *
26
 * @author Sergio Coderius <[email protected]>
27
 */
28
class PellInputWidget extends Widget
29
{
30
    /**
31
     * @var Model the data model that this widget is associated with.
32
     */
33
    public $model;
34
    /**
35
     * @var string the model attribute that this widget is associated with.
36
     */
37
    public $attribute;
38
    /**
39
     * @var string the input name. This must be set if [[model]] and [[attribute]] are not set.
40
     */
41
    public $name;
42
    /**
43
     * @var string the input value.
44
     */
45
    public $value;
46
47
    /**
48
     * @var array the HTML attributes for the widget container tag.
49
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
50
     */
51
    public $inputOptions = [];
52
53
    /**
54
     * Initializes the widget.
55
     * If you override this method, make sure you call the parent implementation first.
56
     */
57 27
    public function init()
58
    {
59 27
        if ($this->hasModel() && !isset($this->inputOptions['id'])) {
60 12
            $this->inputOptions['id'] = Html::getInputId($this->model, $this->attribute);
61
        }
62 27
        parent::init();
63 27
    }
64
65
    /**
66
     * @return bool whether this widget is associated with a data model.
67
     */
68 27
    protected function hasModel()
69
    {
70 27
        return $this->model instanceof Model && $this->attribute !== null;
71
    }
72
}
73