Utils   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 60
c 1
b 0
f 1
dl 0
loc 110
rs 9.92
wmc 31

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toBool() 0 12 6
A toString() 0 12 6
B toFloat() 0 25 9
B toInt() 0 29 10
1
<?php
2
3
namespace Apie\ObjectAccessNormalizer;
4
5
use Apie\ObjectAccessNormalizer\Exceptions\CouldNotConvertException;
6
use ReflectionClass;
7
8
class Utils
9
{
10
    /**
11
     * Converts any value to int if possible.
12
     *
13
     * @param $input
14
     * @return int
15
     */
16
    public static function toInt($input): int
17
    {
18
        $displayValue = gettype($input);
19
        switch (gettype($input)) {
20
            case 'integer':
21
                return $input;
22
            case 'boolean':
23
                return $input ? 1 : 0;
24
            case 'double':
25
                if (round($input) === $input) {
26
                    return (int) $input;
27
                }
28
                $displayValue = $input;
29
                break;
30
            case 'object':
31
                if (!is_callable([$input, '__toString'])) {
32
                    $displayValue = 'object ' . (new ReflectionClass($input))->getShortName();
33
                    break;
34
                }
35
                $input = (string) $input;
36
                // no break
37
            case 'string':
38
                if (!preg_match('/^\s*[1-9][0-9]*(\.0+){0,1}\s*$/', $input)) {
39
                    $displayValue = $input;
40
                    break;
41
                }
42
                return (int) $input;
43
        }
44
        throw new CouldNotConvertException('int', $displayValue);
45
    }
46
47
    /**
48
     * Converts any value to float if possible.
49
     *
50
     * @param mixed $input
51
     * @return float
52
     */
53
    public static function toFloat($input): float
54
    {
55
        $displayValue = gettype($input);
56
        switch (gettype($input)) {
57
            case 'integer':
58
                return (float) $input;
59
            case 'boolean':
60
                return $input ? 1.0 : 0.0;
61
            case 'double':
62
                return $input;
63
            case 'object':
64
                if (!is_callable([$input, '__toString'])) {
65
                    $displayValue = 'object ' . (new ReflectionClass($input))->getShortName();
66
                    break;
67
                }
68
                $input = (string) $input;
69
                // no break
70
            case 'string':
71
                if (!preg_match('/^\s*[0-9]/', $input)) {
72
                    $displayValue = $input;
73
                    break;
74
                }
75
                return (float) $input;
76
        }
77
        throw new CouldNotConvertException('float', $displayValue);
78
    }
79
80
    /**
81
     * Converts any value to string if possible.
82
     *
83
     * @param mixed $input
84
     * @return string
85
     */
86
    public static function toString($input): string
87
    {
88
        $displayValue = gettype($input);
89
        switch (gettype($input)) {
90
            case 'object':
91
            case 'integer':
92
            case 'boolean':
93
            case 'double':
94
            case 'string':
95
                return (string) $input;
96
        }
97
        throw new CouldNotConvertException('string', $displayValue);
98
    }
99
100
    /**
101
     * Converts any value to bool if possible.
102
     *
103
     * @param mixed $input
104
     * @return bool
105
     */
106
    public static function toBool($input): bool
107
    {
108
        $displayValue = gettype($input);
109
        switch (gettype($input)) {
110
            case 'object':
111
            case 'integer':
112
            case 'boolean':
113
            case 'double':
114
            case 'string':
115
                return (bool) $input;
116
        }
117
        throw new CouldNotConvertException('bool', $displayValue);
118
    }
119
}
120