Completed
Push — master ( 48ff0c...a157cb )
by wen
03:02
created

Input::getMin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use DB;
6
use Doctrine\DBAL\Types\Type;
7
8
class Input extends NamedElement
9
{
10
    protected $maxLength;
11
    protected $minLength = 0;
12
13
    protected function getModelFieldLength()
14
    {
15
        $column = $this->getModelColumn();
16
        if ($column->getType()->getName() == Type::STRING) {
17
            return $column->getLength();
18
        }
19
        return;
20
    }
21
22
    protected function getModelColumn()
23
    {
24
        $table  = DB::getTablePrefix() . $this->getModel()->getTable();
25
        $column = $this->getName();
26
        return DB::getDoctrineColumn($table, $column);
27
    }
28
29
    public function getMaxLength()
30
    {
31
        if ($this->maxLength) {
32
            return $this->maxLength;
33
        }
34
35
        return $this->getModelFieldLength();
36
    }
37
38
    public function setMaxLength($value)
39
    {
40
        $this->maxLength = (int)$value;
41
42
        return $this;
43
    }
44
45
    public function getMinLength()
46
    {
47
        return $this->minLength;
48
    }
49
50
    public function setMinLength($value)
51
    {
52
        $this->minLength = (int)$value;
53
54
        return $this;
55
    }
56
57
    public function toArray()
58
    {
59
        return parent::toArray() + [
60
                'minLength' => $this->getMinLength(),
61
                'maxLength' => $this->getMaxLength(),
62
            ];
63
    }
64
}
65