Completed
Pull Request — devel (#18)
by
unknown
61:36 queued 21:23
created

TypeConverter::convertToModel()   C

Complexity

Conditions 14
Paths 13

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 6.2666
cc 14
nc 13
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * TypeConverter.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2017 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version 1.2.0
11
 * @link http://www.sweelix.net
12
 * @package sweelix\oauth2\server\traits\mySql
13
 */
14
15
namespace sweelix\oauth2\server\traits\mySql;
16
17
use yii\base\InvalidArgumentException;
18
use yii\helpers\Json;
19
20
/**
21
 * This trait convert data from the db to match original types
22
 *
23
 * @author Philippe Gaultier <[email protected]>
24
 * @copyright 2010-2017 Philippe Gaultier
25
 * @license http://www.sweelix.net/license license
26
 * @version 1.2.0
27
 * @link http://www.sweelix.net
28
 * @package modules\v1\traits\mySql
29
 * @since 1.0.0
30
 */
31
trait TypeConverter
32
{
33
    /**
34
     * @var array attributes definitions declared in model
35
     */
36
    private $attributesDefinitions;
37
38
    /**
39
     * @param array $attributesDefinitions attributes definitions declared in the model
40
     * @since 1.0.0
41
     */
42
    public function setAttributesDefinitions($attributesDefinitions)
43
    {
44
        $this->attributesDefinitions = $attributesDefinitions;
45
    }
46
47
    /**
48
     * @param string $key attribute name
49
     * @param mixed $value attribute value
50
     * @return mixed type compliant with mysql
51
     * @since 1.0.0
52
     */
53
    public function convertToDatabase($key, $value)
54
    {
55
        $bypassTypes = ['string'];
56
        if ((isset($this->attributesDefinitions[$key]) === true)
57
            && (in_array($this->attributesDefinitions[$key], $bypassTypes) === false)
58
        ) {
59
            switch($this->attributesDefinitions[$key]) {
60
                case 'bool':
61
                case 'boolean':
62
                    $value = $value ? 1 : 0;
63
                    break;
64
                case 'int':
65
                case 'integer':
66
                    break;
67
                case 'array':
68
                    if (is_array($value) === false) {
69
                        $value = [];
70
                    }
71
                    $value = Json::encode($value);
72
                    break;
73
                case 'date':
74
                    $value = date('Y-m-d H:i:s', (int)$value);
75
                    break;
76
                case 'real':
77
                case 'double':
78
                case 'float':
79
                    break;
80
            }
81
82
        }
83
        return $value;
84
    }
85
86
    /**
87
     * @param string $key attribute name
88
     * @param string $value attribute value
89
     * @return mixed value in original datatype
90
     * @since 1.0.0
91
     */
92
    public function convertToModel($key, $value)
93
    {
94
        $bypassTypes = ['string'];
95
        if ((isset($this->attributesDefinitions[$key]) === true)
96
            && (in_array($this->attributesDefinitions[$key], $bypassTypes) === false)
97
        ) {
98
            switch ($this->attributesDefinitions[$key]) {
99
                case 'bool':
100
                case 'boolean':
101
                    $value = (bool) $value;
102
                    break;
103
                case 'int':
104
                case 'integer':
105
                    $value = (int) $value;
106
                    break;
107
                case 'array':
108
                    if (is_array($value) === false) {
109
                        try {
110
                            $value = Json::decode($value);
111
                        } catch (InvalidArgumentException $e) {
112
                            $value = [];
113
                        }
114
                    }
115
                    break;
116
                case 'date':
117
                    $value = strtotime($value);
118
                    break;
119
                case 'real':
120
                case 'double':
121
                case 'float':
122
                    $value = (float) $value;
123
                    break;
124
            }
125
        }
126
        return $value;
127
    }
128
}
129