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

Field   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 3
dl 0
loc 61
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A __toString() 0 4 1
A select() 0 4 1
A insert() 0 4 1
A update() 0 4 1
A format() 0 8 3
A getConfig() 0 4 2
A setConfig() 0 4 1
A formatToDatabase() 0 4 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