BooleanParser   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
B toObjectValue() 0 24 9
A toArrayValue() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the rafrsr/lib-array2object package.
5
 *
6
 * (c) Rafael SR <https://github.com/rafrsr>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Rafrsr\LibArray2Object\Parser;
12
13
class BooleanParser implements ValueParserInterface
14
{
15
    const NAME = 'boolean';
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function getName()
21
    {
22
        return self::NAME;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function toObjectValue($value, $type, \ReflectionProperty $property, $object)
29
    {
30
        if ($type === 'boolean' || $type === 'bool') {
31
            if (is_string($value)) {
32
                switch (strtolower($value)) {
33
                    case 'true':
34
                    case 'yes':
35
                    case 'ok':
36
                        $value = true;
37
                        break;
38
                    case 'false':
39
                    case 'no':
40
                        $value = false;
41
                        break;
42
                    default:
43
                        $value = (boolean) $value;
44
                }
45
            } else {
46
                $value = (boolean) $value;
47
            }
48
        }
49
50
        return $value;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function toArrayValue($value, $type, \ReflectionProperty $property, $object)
57
    {
58
        return $value;
59
    }
60
}
61