CastingManager   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 117
rs 10
wmc 28

11 Methods

Rating   Name   Duplication   Size   Complexity  
A cast() 0 20 4
A json() 0 15 5
A int() 0 3 2
A register() 0 3 1
A bool() 0 3 1
A float() 0 3 2
A castArray() 0 13 3
A json_for_db() 0 12 4
A bool_for_db() 0 3 2
A decimal() 0 3 1
A castArrayForDb() 0 13 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm;
5
6
class CastingManager
7
{
8
    protected $casts = [];
9
10
    public function register(string $name, callable $func)
11
    {
12
        $this->casts[$name] = $func;
13
    }
14
15
    public function cast($type, $value, ...$args)
16
    {
17
        if (strpos($type, ':')) {
18
            list($cast, $args) = explode(':', $type);
19
            $args = explode(',', $args);
20
        } else {
21
            $cast = $type;
22
        }
23
24
        if (method_exists($this, $cast)) {
25
            return $this->$cast($value, ...$args);
26
        }
27
28
        if (isset($this->casts[$cast])) {
29
            $func = $this->casts[$cast];
30
31
            return $func($value, ...$args);
32
        }
33
34
        return $value;
35
    }
36
37
    public function castArray($arr, $rules)
38
    {
39
        $result = [];
40
41
        foreach ($arr as $col => $val) {
42
            if (isset($rules[$col])) {
43
                $result[$col] = $this->cast($rules[$col], $val);
44
            } else {
45
                $result[$col] = $val;
46
            }
47
        }
48
49
        return $result;
50
    }
51
52
    public function castArrayForDb($arr, $rules)
53
    {
54
        $result = [];
55
56
        foreach ($arr as $col => $val) {
57
            if (isset($rules[$col])) {
58
                $result[$col] = $this->cast($rules[$col] . '_for_db', $val);
59
            } else {
60
                $result[$col] = $val;
61
            }
62
        }
63
64
        return $result;
65
    }
66
67
    public function bool($value)
68
    {
69
        return ! ! $value;
70
    }
71
72
    // phpcs:ignore
73
    public function bool_for_db($value)
74
    {
75
        return $value ? 1 : 0;
76
    }
77
78
    public function int($value)
79
    {
80
        return $value === null ? null : (int)$value;
81
    }
82
83
    public function float($value)
84
    {
85
        return $value === null ? null : (float) $value;
86
    }
87
88
    public function decimal($value, $digits)
89
    {
90
        return round((float)$value, (int)$digits);
91
    }
92
93
    public function json($value)
94
    {
95
        if (! $value) {
96
            return new \ArrayObject();
97
        }
98
        if (is_array($value)) {
99
            return new \ArrayObject($value);
100
        }
101
        if (is_string($value)) {
102
            return new \ArrayObject(json_decode($value, true));
103
        }
104
        if ($value instanceof \ArrayObject) {
105
            return $value;
106
        }
107
        throw new \InvalidArgumentException('Value has to be a string, an array or an ArrayObject');
108
    }
109
110
    // phpcs:ignore
111
    public function json_for_db($value)
112
    {
113
        if (!$value) {
114
            return null;
115
        }
116
        if (is_array($value)) {
117
            return json_encode($value);
118
        }
119
        if ($value instanceof \ArrayObject) {
120
            return json_encode($value->getArrayCopy());
121
        }
122
        return $value;
123
    }
124
}
125