FieldMapper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 44
ccs 10
cts 15
cp 0.6667
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A key() 0 4 1
A outputKey() 0 4 1
A map() 0 8 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace AppBuilder\Application\Utils\Mapper;
6
7
use Closure;
8
9
class FieldMapper implements Mapper
10
{
11
    /** @var string */
12
    private $key;
13
14
    /** @var string */
15
    private $outputKey;
16
17
    /** @var Closure */
18
    private $closure;
19
20 7
    public function __construct(string $key, Closure $closure, string $outputKey = '')
21
    {
22 7
        if ($outputKey === '') {
23 3
            $this->outputKey = $key;
24
        } else {
25 4
            $this->outputKey = $outputKey;
26
        }
27 7
        $this->key     = $key;
28 7
        $this->closure = $closure;
29 7
    }
30
31
    public function key() : string
32
    {
33
        return $this->key;
34
    }
35
36
    public function outputKey() : string
37
    {
38
        return $this->outputKey;
39
    }
40
41
    /**
42
     * Returns associative array with mapped data and context.
43
     */
44 7
    public function map(array $data, array $context = [])
45
    {
46 7
        if (!array_key_exists($this->key, $data)) {
47
            return null;
48
        }
49
50 7
        return ($this->closure)($data[$this->key], $context ?? $data);
51
    }
52
}
53