Passed
Push — develop ( f30815...940473 )
by Henry
03:36
created

DefaultSetMapper   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 93.02%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 96
ccs 40
cts 43
cp 0.9302
rs 10
wmc 27

9 Methods

Rating   Name   Duplication   Size   Complexity  
B setDateValue() 0 28 11
A setBooleanValue() 0 3 1
A setDecimalValue() 0 3 2
A setTimestampValue() 0 13 4
A setEnumValue() 0 3 2
A setSerializedValue() 0 3 1
A setListValue() 0 7 3
A setStringValue() 0 3 1
A setIntegerValue() 0 3 2
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\SetMappers;
12
13
use Divergence\IO\Database\MySQL as DB;
14
use Divergence\Models\Interfaces\FieldSetMapper;
15
16
/**
17
 * This is the default type mapping from PHP userspace to our Model field value
18
 *
19
 * @package Divergence
20
 * @author  Henry Paradiz <[email protected]>
21
 *
22
 */
23
class DefaultSetMapper implements FieldSetMapper
24
{
25 36
    public function setStringValue($value): ?string
26
    {
27 36
        return $value;
28
    }
29
30 12
    public function setBooleanValue($value): bool
31
    {
32 12
        return (bool)$value;
33
    }
34
35 15
    public function setDecimalValue($value): ?float
36
    {
37 15
        return is_null($value) ? null : (float)preg_replace('/[^-\d.]/', '', $value);
38
    }
39
40 15
    public function setIntegerValue($value): ?int
41
    {
42 15
        return is_null($value) ? null : (int)preg_replace('/[^-\d]/', '', $value);
43
    }
44
45 14
    public function setTimestampValue($value): ?string
46
    {
47 14
        if (is_numeric($value)) {
48 12
            return date('Y-m-d H:i:s', $value);
49 3
        } elseif (is_string($value)) {
50
            // trim any extra stuff, or leave as-is if it doesn't fit the pattern
51 3
            if (preg_match('/^(\d{4})\D?(\d{2})\D?(\d{2})T?(\d{2})\D?(\d{2})\D?(\d{2})/', $value)) {
52
                return preg_replace('/^(\d{4})\D?(\d{2})\D?(\d{2})T?(\d{2})\D?(\d{2})\D?(\d{2})/', '$1-$2-$3 $4:$5:$6', $value);
53
            } else {
54 3
                return date('Y-m-d H:i:s', strtotime($value));
55
            }
56
        }
57
        return null;
58
    }
59
60
    /**
61
     * Parses a potential date value.
62
     * - If passed a number will assume it's a unix timestamp and convert to Y-m-d based on the provided timestamp.
63
     * - If passed a string will attempt to match m/d/y format.
64
     * - If not valid will then attempt  Y/m/d
65
     * - If passed an array will attempt to look for numeric values in the keys 'yyyy' for year, 'mm' for month, and 'dd' for day.
66
     * - If none of the above worked will attempt to use PHP's strtotime.
67
     * - Otherwise null.
68
     *
69
     * @param string|int|array $value Date or timestamp in various formats.
70
     * @return null|string Date formatted as Y-m-d
71
     */
72 13
    public function setDateValue($value): ?string
73
    {
74 13
        if (is_numeric($value)) {
75 9
            $value = date('Y-m-d', $value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type string; however, parameter $timestamp of date() does only seem to accept integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            $value = date('Y-m-d', /** @scrutinizer ignore-type */ $value);
Loading history...
76 6
        } elseif (is_string($value)) {
77
            // check if m/d/y format
78 6
            if (preg_match('/^(\d{2})\D?(\d{2})\D?(\d{4}).*/', $value)) {
79 1
                $value = preg_replace('/^(\d{2})\D?(\d{2})\D?(\d{4}).*/', '$3-$1-$2', $value);
80
            }
81
82
            // trim time and any extra crap, or leave as-is if it doesn't fit the pattern
83 6
            $value = preg_replace('/^(\d{4})\D?(\d{2})\D?(\d{2}).*/', '$1-$2-$3', $value);
84 1
        } elseif (is_array($value) && count(array_filter($value))) {
85
            // collapse array date to string
86 1
            $value = sprintf(
87 1
                '%04u-%02u-%02u',
88 1
                is_numeric($value['yyyy']) ? $value['yyyy'] : 0,
89 1
                is_numeric($value['mm']) ? $value['mm'] : 0,
90 1
                is_numeric($value['dd']) ? $value['dd'] : 0
91 1
            );
92
        } else {
93 1
            if ($value = strtotime($value)) {
94
                $value = date('Y-m-d', $value) ?: null;
95
            } else {
96 1
                $value = null;
97
            }
98
        }
99 13
        return $value;
100
    }
101
102 1
    public function setSerializedValue($value): string
103
    {
104 1
        return serialize($value);
105
    }
106
107 106
    public function setEnumValue(array $values, $value)
108
    {
109 106
        return (in_array($value, $values) ? $value : null);
110
    }
111
112 12
    public function setListValue($value, ?string $delimiter): array
113
    {
114 12
        if (!is_array($value)) {
115 1
            $delim = empty($delimiter) ? ',' : $delimiter;
116 1
            $value = array_filter(preg_split('/\s*'.$delim.'\s*/', $value));
117
        }
118 12
        return $value;
119
    }
120
}
121