Passed
Push — master ( e30ab9...bbf3e5 )
by Dominik
03:22
created

DateTimeFieldDenormalizer::denormalizeField()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 12
cts 12
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 17
nc 6
nop 5
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Denormalizer;
6
7
use Chubbyphp\Deserialization\DeserializerRuntimeException;
8
9
final class DateTimeFieldDenormalizer implements FieldDenormalizerInterface
10
{
11
    /**
12
     * @var FieldDenormalizerInterface
13
     */
14
    private $fieldDenormalizer;
15
16
    /**
17
     * @param FieldDenormalizerInterface $fieldDenormalizer
18
     */
19 12
    public function __construct(FieldDenormalizerInterface $fieldDenormalizer)
20
    {
21 12
        $this->fieldDenormalizer = $fieldDenormalizer;
22 12
    }
23
24
    /**
25
     * @param string                       $path
26
     * @param object                       $object
27
     * @param mixed                        $value
28
     * @param DenormalizerContextInterface $context
29
     * @param DenormalizerInterface|null   $denormalizer
30
     *
31
     * @throws DeserializerRuntimeException
32
     */
33 12
    public function denormalizeField(
34
        string $path,
35
        $object,
36
        $value,
37
        DenormalizerContextInterface $context,
38
        DenormalizerInterface $denormalizer = null
39
    ) {
40 12
        if (!is_string($value) || '' === $trimmedValue = trim($value)) {
41 6
            $this->fieldDenormalizer->denormalizeField($path, $object, $value, $context, $denormalizer);
42
43 6
            return;
44
        }
45
46
        try {
47 6
            $dateTime = new \DateTime($trimmedValue);
48 3
            $errors = \DateTime::getLastErrors();
49
50 3
            if (0 === $errors['warning_count'] && 0 === $errors['error_count']) {
51 3
                $value = $dateTime;
52
            }
53 3
        } catch (\Exception $exception) {
54 3
            error_clear_last();
55
        }
56
57 6
        $this->fieldDenormalizer->denormalizeField($path, $object, $value, $context, $denormalizer);
58 6
    }
59
}
60