Passed
Push — master ( d4b74b...f37c5f )
by Jesse
01:55
created

KeyTransform::use()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Hydration\Mapping\Transform;
4
5
use Stratadox\Hydration\Mapping\AssertKey;
6
use Stratadox\HydrationMapping\Mapping;
7
8
final class KeyTransform implements Mapping
9
{
10
    /** @var string */
11
    private $key;
12
    /** @var Mapping */
13
    private $mapping;
14
15
    private function __construct(string $key, Mapping $mapping)
16
    {
17
        $this->key = $key;
18
        $this->mapping = $mapping;
19
    }
20
21
    public static function use(string $key, Mapping $mapping): Mapping
22
    {
23
        return new self($key, $mapping);
24
    }
25
26
    public function name(): string
27
    {
28
        return $this->mapping->name();
29
    }
30
31
    public function value(array $data, $owner = null)
32
    {
33
        AssertKey::exists($this, $data, $this->key);
34
        $data[$this->name()] = $data[$this->key];
35
        unset($data[$this->key]);
36
        return $this->mapping->value($data, $owner);
37
    }
38
}
39