Completed
Push — master ( 366fbf...5f1478 )
by Rafael
02:45
created

BooleanParser::toArrayValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 4
1
<?php
2
3
/**
4
 * LICENSE: This file is subject to the terms and conditions defined in
5
 * file 'LICENSE', which is part of this source code package.
6
 *
7
 * @copyright 2016 Copyright(c) - All rights reserved.
8
 */
9
10
namespace Rafrsr\LibArray2Object\Parser;
11
12
class BooleanParser implements ValueParserInterface
13
{
14
    const NAME = 'boolean';
15
16
    /**
17
     * @inheritDoc
18
     */
19
    public function getName()
20
    {
21
        return self::NAME;
22
    }
23
24
    /**
25
     * @inheritDoc
26
     */
27
    public function toObjectValue($value, $type, \ReflectionProperty $property, $object)
28
    {
29
        if ($type === 'boolean' || $type === 'bool') {
30
            if (is_string($value)) {
31
                switch (strtolower($value)) {
32
                    case 'true':
33
                    case 'yes':
34
                    case 'ok':
35
                        $value = true;
36
                        break;
37
                    case 'false':
38
                    case 'no':
39
                        $value = false;
40
                        break;
41
                    default:
42
                        $value = (boolean)$value;
43
                }
44
            } else {
45
                $value = (boolean)$value;
46
            }
47
        }
48
49
        return $value;
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function toArrayValue($value, $type, \ReflectionProperty $property, $object)
56
    {
57
        return $value;
58
    }
59
}