ClosureTransformer::transformFromHttp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Bdf\Form\Transformer;
4
5
use Bdf\Form\ElementBuilderInterface;
6
use Bdf\Form\ElementInterface;
7
use Bdf\Form\Registry\RegistryInterface;
8
9
/**
10
 * Wrap a closure into a Transformer
11
 *
12
 * <code>
13
 * new ClosureTransformer(function ($value, ElementInterface $element, bool $toPhp) {
14
 *     return $toPhp ? parse($value) : normalize($value);
15
 * });
16
 * </code>
17
 *
18
 * @see RegistryInterface::transformer() With callbable should return a ClosureTransformer
19
 * @see ElementBuilderInterface::transformer() For register a transformer on an element
20
 */
21
final class ClosureTransformer implements TransformerInterface
22
{
23
    /**
24
     * @var callable
25
     */
26
    private $callback;
27
28
29
    /**
30
     * @param callable $callback
31
     */
32 68
    public function __construct(callable $callback)
33
    {
34 68
        $this->callback = $callback;
35 68
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 28
    public function transformToHttp($value, ElementInterface $input)
41
    {
42 28
        return ($this->callback)($value, $input, false);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 59
    public function transformFromHttp($value, ElementInterface $input)
49
    {
50 59
        return ($this->callback)($value, $input, true);
51
    }
52
}
53