BooleanGuesser::supports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Knp\FriendlyContexts\Guesser;
4
5
class BooleanGuesser extends AbstractGuesser implements GuesserInterface
6
{
7
    public function supports(array $mapping)
8
    {
9
        $mapping = array_merge([ 'type' => null ], $mapping);
10
11
        return $mapping['type'] === 'boolean';
12
    }
13
14
    public function transform($str, array $mapping = null)
15
    {
16
        $str = strtolower($str);
17
18
        $formats = [
19
            'active'    => true,
20
            'activated' => true,
21
            'enabled'   => true,
22
            'disabled'  => false,
23
            'true'      => true,
24
            'false'     => false,
25
            'yes'       => true,
26
            'no'        => false,
27
            '1'         => true,
28
            '0'         => false,
29
        ];
30
31
        if (false === array_key_exists($str, $formats)) {
32
            throw new \Exception(
33
                sprintf(
34
                    '"%s" is not a supported format. Supported format : [%s].',
35
                    $str,
36
                    implode(', ', array_keys($formats))
37
                )
38
            );
39
        }
40
41
        return $formats[$str];
42
    }
43
44
    public function fake(array $mapping)
45
    {
46
        return true;
47
    }
48
49
    public function getName()
50
    {
51
        return 'boolean';
52
    }
53
}
54