Date::extract()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Mapping\Strategy;
4
5
use DateTime;
6
use DateTimeImmutable;
7
use DateTimeInterface;
8
use DateTimeZone;
9
use Igni\Storage\Mapping\MappingStrategy;
10
11
final class Date implements MappingStrategy, DefaultAttributesProvider
12
{
13 4
    public static function hydrate(&$value, array $attributes = []): void
14
    {
15 4
        if ($value === null) {
16 2
            return;
17
        }
18
19 2
        if ($attributes['immutable']) {
20
            $value = new DateTimeImmutable($value, new DateTimeZone($attributes['timezone']));
21
            return;
22
        }
23
24 2
        $value = new DateTime($value, new DateTimeZone($attributes['timezone']));
25 2
    }
26
27 3
    public static function extract(&$value, array $attributes = []): void
28
    {
29 3
        if ($value instanceof DateTimeInterface) {
30 1
            $value = $value->format($attributes['format']);
31
        } else {
32 2
            $value = null;
33
        }
34 3
    }
35
36 5
    public static function getDefaultAttributes(): array
37
    {
38
        return [
39 5
            'timezone' => 'UTC',
40
            'format' => 'Ymd',
41
            'immutable' => false,
42
        ];
43
    }
44
}
45