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

Date::hydrate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 9.8666
c 0
b 0
f 0
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