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

KeyTransform   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

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