Completed
Push — master ( 477b17...1ec9e1 )
by Jared
01:32
created

Type::cast()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
cc 5
nc 4
nop 2
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @see http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
12
namespace Pulsar;
13
14
/**
15
 * Handles value type casting.
16
 */
17
class Type
18
{
19
    /**
20
     * Marshals a value for a given property from storage.
21
     *
22
     * @param mixed $value
23
     *
24
     * @return mixed type-casted value
25
     */
26
    public static function cast(Property $property, $value)
27
    {
28
        if (null === $value) {
29
            return null;
30
        }
31
32
        // handle empty strings as null
33
        if ($property->isNullable() && '' === $value) {
34
            return null;
35
        }
36
37
        $type = $property->getType();
38
        if (!$type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
39
            return $value;
40
        }
41
42
        $m = 'to_'.$property->getType();
43
44
        return self::$m($value);
45
    }
46
47
    /**
48
     * Casts a value to a string.
49
     *
50
     * @param mixed $value
51
     */
52
    public static function to_string($value): string
53
    {
54
        return (string) $value;
55
    }
56
57
    /**
58
     * Casts a value to an integer.
59
     *
60
     * @param mixed $value
61
     */
62
    public static function to_integer($value): int
63
    {
64
        return (int) $value;
65
    }
66
67
    /**
68
     * Casts a value to a float.
69
     *
70
     * @param mixed $value
71
     */
72
    public static function to_float($value): float
73
    {
74
        return (float) $value;
75
    }
76
77
    /**
78
     * Casts a value to a boolean.
79
     *
80
     * @param mixed $value
81
     */
82
    public static function to_boolean($value): bool
83
    {
84
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
85
    }
86
87
    /**
88
     * Casts a date value as a UNIX timestamp.
89
     *
90
     * @param mixed $value
91
     */
92
    public static function to_date($value): int
93
    {
94
        if (!is_numeric($value)) {
95
            return strtotime($value);
96
        } else {
97
            return $value + 0;
98
        }
99
    }
100
101
    /**
102
     * Casts a value to an array.
103
     *
104
     * @param mixed $value
105
     */
106
    public static function to_array($value): array
107
    {
108
        // decode JSON strings into an array
109
        if (is_string($value)) {
110
            return (array) json_decode($value, true);
111
        }
112
113
        return (array) $value;
114
    }
115
116
    /**
117
     * Casts a value to an object.
118
     *
119
     * @param mixed $value
120
     *
121
     * @return object
122
     */
123
    public static function to_object($value): \stdClass
124
    {
125
        // decode JSON strings into an object
126
        if (is_string($value)) {
127
            return (object) json_decode($value);
128
        }
129
130
        return (object) $value;
131
    }
132
}
133