Completed
Push — master ( 18f9b1...e2cca3 )
by Dawid
07:34
created

Date   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 12 3
A extract() 0 6 2
A getDefaultAttributes() 0 6 1
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Mapping\Strategy;
4
5
use Igni\Storage\Mapping\MappingStrategy;
6
7
final class Date implements MappingStrategy, DefaultAttributesProvider
8
{
9 5
    public static function hydrate(&$value, array $attributes = []): void
10
    {
11 5
        if ($value === null) {
12 3
            return;
13
        }
14
15 2
        if ($attributes['immutable']) {
16
            $value = new \DateTimeImmutable($value, new \DateTimeZone($attributes['timezone']));
17
            return;
18
        }
19
20 2
        $value = new \DateTime($value, new \DateTimeZone($attributes['timezone']));
21 2
    }
22
23 3
    public static function extract(&$value, array $attributes = []): void
24
    {
25 3
        if ($value instanceof \DateTimeInterface) {
26 1
            $value = $value->format($attributes['format']);
27
        } else {
28 2
            $value = null;
29
        }
30 3
    }
31
32 5
    public static function getDefaultAttributes(): array
33
    {
34
        return [
35 5
            'timezone' => 'UTC',
36
            'format' => 'Ymd',
37
            'immutable' => false,
38
        ];
39
    }
40
}
41