Completed
Push — master ( f8145f...0d5f93 )
by Jesse
03:38
created

CanBeNull::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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