Passed
Push — master ( 9b2fbd...f8145f )
by Jesse
02:29
created

CanBeNull::value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydration\Mapping\Property\Scalar;
5
6
use function is_null;
7
use Stratadox\HydrationMapping\ExposesDataKey;
8
use Stratadox\HydrationMapping\MapsProperty;
9
10
/**
11
 * Decorates scalar type declaration with a nullable property.
12
 *
13
 * @package Stratadox\Hydrate
14
 * @author  Stratadox
15
 */
16
final class CanBeNull implements MapsProperty
17
{
18
    private $or;
19
20
    private function __construct(ExposesDataKey $mapping)
21
    {
22
        $this->or = $mapping;
23
    }
24
25
    /**
26
     * Creates a new nullable type wrapper.
27
     *
28
     * @param ExposesDataKey $mapping    The mapping to decorate.
29
     * @return self                      The custom truth boolean mapping.
30
     */
31
    public static function or(ExposesDataKey $mapping): self
32
    {
33
        return new self($mapping);
34
    }
35
36
    /** @inheritdoc */
37
    public function value(array $data, $owner = null)
38
    {
39
        if (is_null($data[$this->or->key()])) {
40
            return null;
41
        }
42
        return $this->or->value($data, $owner);
43
    }
44
45
    /** @inheritdoc */
46
    public function name(): string
47
    {
48
        return $this->or->name();
49
    }
50
}
51