Completed
Push — master ( 7c9586...c8ce39 )
by Oscar
01:44
created

Field::__toString()   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
    /**
66
     * Return the default value
67
     */
68
    public function getDefaultValue()
69
    {
70
        return $this->dataFromDatabase($this->getConfig('default'));
0 ignored issues
show
Bug introduced by
The method dataFromDatabase() does not seem to exist on object<SimpleCrud\Fields\Field>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    }
72
}
73