Completed
Push — master ( c8ce39...89e667 )
by Oscar
01:32
created

Field::getDefaultValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SimpleCrud\Fields;
4
5
use Atlas\Query\Insert;
6
use Atlas\Query\Update;
7
use SimpleCrud\Table;
8
9
class Field implements FieldInterface
10
{
11
    protected $table;
12
    protected $info;
13
    protected $config = [];
14
15
    public function __construct(Table $table, array $info)
16
    {
17
        $this->table = $table;
18
        $this->info = $info;
19
    }
20
21
    public function getName(): string
22
    {
23
        return $this->info['name'];
24
    }
25
26
    public function __toString()
27
    {
28
        return sprintf('%s.`%s`', $this->table, $this->info['name']);
29
    }
30
31
    public function insert(Insert $query, $value)
32
    {
33
        $query->column($this->info['name'], $this->formatToDatabase($value));
34
    }
35
36
    public function update(Update $query, $value)
37
    {
38
        $query->column($this->info['name'], $this->formatToDatabase($value));
39
    }
40
41
    public function format($value)
42
    {
43
        if ($value === '' && $this->info['null']) {
44
            return;
45
        }
46
47
        return $value;
48
    }
49
50
    public function getConfig(string $name)
51
    {
52
        return isset($this->config[$name]) ? $this->config[$name] : null;
53
    }
54
55
    public function setConfig(string $name, $value)
56
    {
57
        $this->config[$name] = $value;
58
    }
59
60
    protected function formatToDatabase($value)
61
    {
62
        return $this->format($value);
63
    }
64
}
65