FieldDenormalizer::denormalizeField()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 5
dl 0
loc 14
ccs 1
cts 1
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Denormalizer;
6
7
use Chubbyphp\Deserialization\Accessor\AccessorInterface;
8
use Chubbyphp\Deserialization\DeserializerRuntimeException;
9
10
final class FieldDenormalizer implements FieldDenormalizerInterface
11
{
12
    /**
13
     * @var AccessorInterface
14
     */
15
    private $accessor;
16
17
    /**
18
     * @var bool
19
     */
20
    private $emptyToNull;
21
22
    public function __construct(AccessorInterface $accessor, bool $emptyToNull = false)
23
    {
24
        $this->accessor = $accessor;
25
        $this->emptyToNull = $emptyToNull;
26 3
    }
27
28 3
    /**
29 3
     * @param object $object
30 3
     * @param mixed  $value
31
     *
32
     * @throws DeserializerRuntimeException
33
     */
34
    public function denormalizeField(
35
        string $path,
36
        $object,
37
        $value,
38
        DenormalizerContextInterface $context,
39
        DenormalizerInterface $denormalizer = null
40
    ): void {
41 3
        if ('' === $value && $this->emptyToNull) {
42
            $this->accessor->setValue($object, null);
43
44
            return;
45
        }
46
47
        $this->accessor->setValue($object, $value);
48 3
    }
49
}
50