Completed
Push — master ( 4fb1d6...a24b8d )
by Oscar
01:43
created

Field::getFactory()   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
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
12
{
13
    protected $table;
14
    protected $info;
15
    protected $config = [];
16
17
    public static function getFactory(): FieldFactory
18
    {
19
        return new FieldFactory(self::class);
20
    }
21
22
    public function __construct(Table $table, array $info)
23
    {
24
        $this->table = $table;
25
        $this->info = $info;
26
    }
27
28
    public function getName(): string
29
    {
30
        return $this->info['name'];
31
    }
32
33
    public function __toString()
34
    {
35
        return sprintf('%s.`%s`', $this->table, $this->info['name']);
36
    }
37
38
    public function select(Select $query)
39
    {
40
        $query->columns((string) $this);
41
    }
42
43
    public function insert(Insert $query, $value)
44
    {
45
        $query->column($this->getName(), $this->formatToDatabase($value));
46
    }
47
48
    public function update(Update $query, $value)
49
    {
50
        $query->column($this->getName(), $this->formatToDatabase($value));
51
    }
52
53
    public function format($value)
54
    {
55
        if ($value === '' && $this->info['null']) {
56
            return;
57
        }
58
59
        return $value;
60
    }
61
62
    public function getConfig(string $name)
63
    {
64
        return isset($this->config[$name]) ? $this->config[$name] : null;
65
    }
66
67
    public function setConfig(string $name, $value)
68
    {
69
        $this->config[$name] = $value;
70
    }
71
72
    protected function formatToDatabase($value)
73
    {
74
        return $this->format($value);
75
    }
76
}
77