Completed
Push — master ( 2bde54...385f57 )
by wen
16:28
created

Input::setMinLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
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
    protected $readonly = false;
16
17
    public function getSize()
18
    {
19
        return $this->size;
20
    }
21
22
    public function largeSize()
23
    {
24
        $this->size = 'large';
25
26
        return $this;
27
    }
28
29
    public function miniSize()
30
    {
31
        $this->size = 'mini';
32
33
        return $this;
34
    }
35
36
    protected function getModelFieldLength()
37
    {
38
        $column = $this->getModelColumn();
39
        if ($column->getType()->getName() == Type::STRING) {
40
            return $column->getLength();
41
        }
42
        return;
43
    }
44
45
    protected function getModelColumn()
46
    {
47
        // Doctrine\DBAL\Platforms\MySQL57Platform not support "enum" "string"
48
        $databasePlatform = DB::getDoctrineSchemaManager()->getDatabasePlatform();
49
        $databasePlatform->registerDoctrineTypeMapping('enum', 'string');
50
51
        $table  = DB::getTablePrefix() . $this->getModel()->getTable();
52
        $column = $this->getName();
53
        return DB::getDoctrineColumn($table, $column);
54
    }
55
56
    public function getMaxLength()
57
    {
58
        if ($this->maxLength) {
59
            return $this->maxLength;
60
        }
61
62
        return $this->getModelFieldLength();
63
    }
64
65
    public function setMaxLength($value)
66
    {
67
        $value           = intval($value);
68
        $this->maxLength = $value;
69
        $this->addValidationRule('min:' . $value);
70
        return $this;
71
    }
72
73
    public function getMinLength()
74
    {
75
        return $this->minLength;
76
    }
77
78
    public function setMinLength($value)
79
    {
80
        $value           = intval($value);
81
        $this->minLength = $value;
82
83
        $this->addValidationRule('max:' . $value);
84
85
        return $this;
86
    }
87
88
    public function isReadonly()
89
    {
90
        return $this->readonly;
91
    }
92
93
    public function readonly()
94
    {
95
        $this->readonly = true;
96
97
        return $this;
98
    }
99
100
    public function toArray()
101
    {
102
        return parent::toArray() + [
103
                'minLength' => $this->getMinLength(),
104
                'maxLength' => $this->getMaxLength(),
105
                'size'      => $this->getSize(),
106
                'readonly'  => $this->isReadonly(),
107
            ];
108
    }
109
}
110