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

CanBeNull   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A key() 0 3 1
A value() 0 6 2
A __construct() 0 3 1
A or() 0 3 1
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