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

Json   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 28
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFactory() 0 7 1
A format() 0 4 2
A formatToDatabase() 0 8 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Fields;
5
6
final class Json extends Field
7
{
8
    protected $config = [
9
        'assoc' => true,
10
    ];
11
12
    public static function getFactory(): FieldFactory
13
    {
14
        return new FieldFactory(
15
            self::class,
16
            ['json']
17
        );
18
    }
19
20
    public function format($value)
21
    {
22
        return empty($value) ? [] : json_decode($value, $this->config['assoc']);
23
    }
24
25
    protected function formatToDatabase($value): ?string
26
    {
27
        if (!is_string($value)) {
28
            return json_encode($value) ?: null;
29
        }
30
31
        return $value ?: null;
32
    }
33
}
34