Passed
Push — master ( 01c6a8...5a4258 )
by Jesse
09:00
created

DifferentKey::value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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