Passed
Push — release ( e4b72f...07d039 )
by Henry
09:15 queued 06:21
created

DefaultGetMapper::getIntegerValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * This file is part of the Divergence package.
4
 *
5
 * (c) Henry Paradiz <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Divergence\Models\Mapping;
12
13
use Divergence\Models\Interfaces\FieldGetMapper;
14
15
/**
16
 * This is the default type mapping from PHP userspace to our Model field value
17
 *
18
 * @package Divergence
19
 * @author  Henry Paradiz <[email protected]>
20
 *
21
 */
22
class DefaultGetMapper implements FieldGetMapper
23
{
24
    public static function getStringValue($value): ?string
25
    {
26
        return $value;
27
    }
28
29
30 16
    public static function getBooleanValue($value): bool
31
    {
32 16
        return $value;
33
    }
34
35 19
    public static function getDecimalValue($value): ?float
36
    {
37 19
        return floatval($value);
38
    }
39
40 64
    public static function getIntegerValue($value): ?int
41
    {
42 64
        return intval($value);
43
    }
44
45
    public static function getDateValue($value)
46
    {
47
        return $value;
48
    }
49
50 33
    public static function getTimestampValue($value): ?int
51
    {
52 33
        if ($value && is_string($value) && $value != '0000-00-00 00:00:00') {
53 33
            return strtotime($value);
54 4
        } elseif (is_integer($value)) {
55 4
            return $value;
56
        }
57
        return null;
58
    }
59 16
    public static function getSerializedValue($value)
60
    {
61 16
        if (is_string($value)) {
62 16
            return unserialize($value);
63
        } else {
64
            return $value;
65
        }
66
    }
67
68 16
    public static function getListValue($value, ?string $delimiter = ','): array
69
    {
70 16
        if (is_array($value)) {
71 9
            return $value;
72
        }
73 8
        return array_filter(preg_split('/\s*'.$delimiter.'\s*/', $value));
74
    }
75
}
76