Completed
Push — master ( 89fe2f...2bde54 )
by wen
05:41
created

Input::getSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
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 $size = '';
14
15
    public function getSize()
16
    {
17
        return $this->size;
18
    }
19
20
    public function largeSize()
21
    {
22
        $this->size = 'large';
23
24
        return $this;
25
    }
26
27
    public function miniSize()
28
    {
29
        $this->size = 'mini';
30
31
        return $this;
32
    }
33
34
    protected function getModelFieldLength()
35
    {
36
        $column = $this->getModelColumn();
37
        if ($column->getType()->getName() == Type::STRING) {
38
            return $column->getLength();
39
        }
40
        return;
41
    }
42
43
    protected function getModelColumn()
44
    {
45
        $table  = DB::getTablePrefix() . $this->getModel()->getTable();
46
        $column = $this->getName();
47
        return DB::getDoctrineColumn($table, $column);
48
    }
49
50
    public function getMaxLength()
51
    {
52
        if ($this->maxLength) {
53
            return $this->maxLength;
54
        }
55
56
        return $this->getModelFieldLength();
57
    }
58
59
    public function setMaxLength($value)
60
    {
61
        $this->maxLength = (int)$value;
62
63
        return $this;
64
    }
65
66
    public function getMinLength()
67
    {
68
        return $this->minLength;
69
    }
70
71
    public function setMinLength($value)
72
    {
73
        $this->minLength = (int)$value;
74
75
        return $this;
76
    }
77
78
    public function toArray()
79
    {
80
        return parent::toArray() + [
81
                'minLength' => $this->getMinLength(),
82
                'maxLength' => $this->getMaxLength(),
83
                'size'      => $this->getSize(),
84
            ];
85
    }
86
}
87