FieldDenormalizer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 2
b 1
f 0
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
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