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

CallableNormalizer::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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