BooleanGuesser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 24
c 2
b 0
f 0
dl 0
loc 47
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fake() 0 3 1
A supports() 0 5 1
A getName() 0 3 1
A transform() 0 28 2
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