Test Failed
Push — master ( 31c635...ef6440 )
by Paul
07:17 queued 01:32
created

Cast   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 0
Metric Value
wmc 20
eloc 29
dl 0
loc 93
ccs 34
cts 35
cp 0.9714
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A toObject() 0 6 2
A to() 0 7 3
B toString() 0 15 7
A toInt() 0 3 1
A toFloat() 0 3 1
A toBool() 0 3 1
A toArray() 0 13 5
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Helpers;
4
5
use GeminiLabs\SiteReviews\Helper;
6
7
class Cast
8
{
9
    /**
10
     * @param string $cast
11
     * @param mixed ...$args
12
     * @return mixed
13
     */
14 27
    public static function to($cast = '', ...$args)
15
    {
16 27
        $method = Helper::buildMethodName($cast, 'to');
17 27
        if (!empty($cast) && method_exists(__CLASS__, $method)) {
18 27
            return call_user_func_array('static::'.$method, $args);
19
        }
20 25
        return array_shift($args);
21
    }
22
23
    /**
24
     * @param mixed $value
25
     * @return array
26
     */
27 42
    public static function toArray($value, $explode = true)
28
    {
29 42
        if (is_object($value)) {
30 3
            $reflection = new \ReflectionObject($value);
31 3
            $properties = $reflection->hasMethod('toArray')
32
                ? $value->toArray()
33 3
                : get_object_vars($value);
34 3
            return json_decode(json_encode($properties), true);
35
        }
36 42
        if (is_scalar($value) && $explode) {
37 41
            return Arr::convertFromString($value);
38
        }
39 31
        return (array) $value;
40
    }
41
42
    /**
43
     * @param mixed $value
44
     * @return bool
45
     */
46 27
    public static function toBool($value)
47
    {
48 27
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
49
    }
50
51
    /**
52
     * @param mixed $value
53
     * @return float
54
     */
55 32
    public static function toFloat($value)
56
    {
57 32
        return (float) filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_FRACTION|FILTER_FLAG_ALLOW_THOUSAND);
58
    }
59
60
    /**
61
     * @param mixed $value
62
     * @return int
63
     */
64 31
    public static function toInt($value)
65
    {
66 31
        return (int) round(static::toFloat($value));
67
    }
68
69
    /**
70
     * @param mixed $value
71
     * @return object
72
     */
73 2
    public static function toObject($value)
74
    {
75 2
        if (!is_object($value)) {
76 2
            return (object) static::toArray($value);
77
        }
78 1
        return $value;
79
    }
80
81
    /**
82
     * @param mixed $value
83
     * @return string
84
     */
85 51
    public static function toString($value, $strict = true)
86
    {
87 51
        if (is_object($value) && in_array('__toString', get_class_methods($value))) {
88 1
            return (string) $value->__toString();
89
        }
90 51
        if (Helper::isEmpty($value)) {
91 50
            return '';
92
        }
93 48
        if (Arr::isIndexedAndFlat($value)) {
94 8
            return implode(', ', $value);
95
        }
96 48
        if (!is_scalar($value)) {
97 8
            return $strict ? '' : serialize($value);
98
        }
99 48
        return (string) $value;
100
    }
101
}
102