Passed
Push — master ( 201e0e...28aced )
by Andy
01:40
created

CallableNormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
c 0
b 0
f 0
dl 0
loc 32
ccs 0
cts 12
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 9 2
A __construct() 0 4 1
A getCallback() 0 3 1
A create() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Palmtree\Csv\Normalizer;
6
7
class CallableNormalizer implements NormalizerInterface
8
{
9
    private \Closure $callback;
10
    private ?NormalizerInterface $normalizer;
11
12
    public function __construct(callable $callback, ?NormalizerInterface $normalizer = null)
13
    {
14
        $this->callback = \Closure::fromCallable($callback);
15
        $this->normalizer = $normalizer;
16
    }
17
18
    /** @psalm-suppress UnsafeInstantiation */
19
    public static function create(callable $callback, ?NormalizerInterface $normalizer = null): self
20
    {
21
        return new static($callback, $normalizer);
22
    }
23
24
    public function getCallback(): callable
25
    {
26
        return $this->callback;
27
    }
28
29
    /** @return mixed */
30
    public function normalize(string $value)
31
    {
32
        if ($this->normalizer) {
33
            $value = $this->normalizer->normalize($value);
34
        }
35
36
        $callback = $this->callback;
37
38
        return $callback($value, $this);
39
    }
40
}
41