Completed
Push — master ( 0da900...d5ed8a )
by Oscar
01:30
created

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