FieldDenormalizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 9
c 2
b 1
f 0
dl 0
loc 38
ccs 5
cts 5
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A denormalizeField() 0 14 3
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