Utils::toString()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 9
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 12
rs 9.2222
1
<?php
2
3
namespace W2w\Lib\ApieObjectAccessNormalizer;
4
5
use ReflectionClass;
6
use W2w\Lib\ApieObjectAccessNormalizer\Exceptions\CouldNotConvertException;
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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
36
            case 'string':
37
                if (!preg_match('/^\s*[1-9][0-9]*(\.0+){0,1}\s*$/', $input)) {
38
                    $displayValue = $input;
39
                    break;
40
                }
41
                return (int) $input;
42
        }
43
        throw new CouldNotConvertException('int', $displayValue);
44
    }
45
46
    /**
47
     * Converts any value to float if possible.
48
     *
49
     * @param mixed $input
50
     * @return float
51
     */
52
    public static function toFloat($input): float
53
    {
54
        $displayValue = gettype($input);
55
        switch (gettype($input)) {
56
            case 'integer':
57
                return (float) $input;
58
            case 'boolean':
59
                return $input ? 1.0 : 0.0;
60
            case 'double':
61
                return $input;
62
            case 'object':
63
                if (!is_callable([$input, '__toString'])) {
64
                    $displayValue = 'object ' . (new ReflectionClass($input))->getShortName();
65
                    break;
66
                }
67
                $input = (string) $input;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
68
            case 'string':
69
                if (!preg_match('/^\s*[0-9]/', $input)) {
70
                    $displayValue = $input;
71
                    break;
72
                }
73
                return (float) $input;
74
        }
75
        throw new CouldNotConvertException('float', $displayValue);
76
    }
77
78
    /**
79
     * Converts any value to string if possible.
80
     *
81
     * @param mixed $input
82
     * @return string
83
     */
84
    public static function toString($input): string
85
    {
86
        $displayValue = gettype($input);
87
        switch (gettype($input)) {
88
            case 'object':
89
            case 'integer':
90
            case 'boolean':
91
            case 'double':
92
            case 'string':
93
                return (string) $input;
94
        }
95
        throw new CouldNotConvertException('string', $displayValue);
96
    }
97
98
    /**
99
     * Converts any value to bool if possible.
100
     *
101
     * @param mixed $input
102
     * @return bool
103
     */
104
    public static function toBool($input): bool
105
    {
106
        $displayValue = gettype($input);
107
        switch (gettype($input)) {
108
            case 'object':
109
            case 'integer':
110
            case 'boolean':
111
            case 'double':
112
            case 'string':
113
                return (bool) $input;
114
        }
115
        throw new CouldNotConvertException('bool', $displayValue);
116
    }
117
}
118