Completed
Push — master ( 9d637c...bbac66 )
by Dmitry
06:35
created

FormulaInput::formulaLinesCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\widgets;
4
5
use hipanel\modules\finance\models\Price;
6
use hiqdev\assets\autosize\AutosizeAsset;
7
use yii\bootstrap\Html;
8
use yii\helpers\BaseHtml;
9
use yii\widgets\InputWidget;
10
11
12
/**
13
 * Class FormulaInput
14
 *
15
 * @author Dmytro Naumenko <[email protected]>
16
 */
17
class FormulaInput extends InputWidget
18
{
19
    /** @var Price */
20
    public $model;
21
22
    private function getAttributeValue()
23
    {
24
        return BaseHtml::getAttributeName($this->attribute);
25
    }
26
27
    public function registerClientScript()
28
    {
29
        AutosizeAsset::register($this->view);
30
        $this->view->registerJs("autosize($('.formula-input'));");
31
    }
32
33
    public function run()
34
    {
35
        $this->registerClientScript();
36
37
        return Html::activeTextarea($this->model, $this->attribute, [
38
            'class' => 'form-control formula-input',
39
            'rows' => $this->formulaLinesCount(),
40
        ]);
41
    }
42
43
    private function formulaLinesCount()
44
    {
45
        return count(explode("\n", $this->getAttributeValue()));
46
    }
47
}
48