Completed
Pull Request — 3.x (#5937)
by Peter
05:08 queued 01:40
created

BooleanToStringTransformer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 22
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 0 4 2
A reverseTransform() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Form\DataTransformer;
15
16
use Symfony\Component\Form\DataTransformerInterface;
17
18
/**
19
 * This is analog of Symfony\Component\Form\Extension\Core\DataTransformer\BooleanToStringTransformer
20
 * which allows you to use non-strings in reverseTransform() method.
21
 *
22
 * @author Peter Gribanov <[email protected]>
23
 */
24
final class BooleanToStringTransformer implements DataTransformerInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    private $trueValue;
30
31
    public function __construct(string $trueValue)
32
    {
33
        $this->trueValue = $trueValue;
34
    }
35
36
    public function transform($value): ?string
37
    {
38
        return $value ? $this->trueValue : null;
39
    }
40
41
    public function reverseTransform($value): bool
42
    {
43
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
44
    }
45
}
46