Passed
Push — master ( d22e0d...afa1cf )
by Irfaq
16:39 queued 01:39
created

Helper::castAttribute()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 17
rs 8.8333
c 0
b 0
f 0
cc 7
nc 6
nop 2
1
<?php
2
3
namespace Transmission;
4
5
use Illuminate\Support\Carbon;
6
7
/**
8
 * Helper
9
 */
10
class Helper
11
{
12
    /**
13
     * Format Bytes.
14
     *
15
     * @param int  $bytes
16
     * @param bool $format     Should we suffix symbol? Default: true.
17
     * @param bool $binaryMode Should we calculate in binary mode? Default: false.
18
     *
19
     * @return string
20
     */
21
    public static function formatBytes(int $bytes, bool $format = true, bool $binaryMode = false): string
22
    {
23
        if (!$format) {
24
            return $bytes;
25
        }
26
27
        if ($binaryMode) {
28
            $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
29
            $base = 1024; // Binary
30
        } else {
31
            $units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
32
            $base = 1000; // Decimal
33
        }
34
35
        $i = 0;
36
37
        while ($bytes > $base) {
38
            $bytes /= $base;
39
            $i++;
40
        }
41
42
        return round($bytes, 2) . ' ' . $units[$i];
43
    }
44
45
    /**
46
     * Cast an attribute to a native PHP type.
47
     *
48
     * @param mixed  $value
49
     * @param string $type
50
     *
51
     * @return mixed
52
     */
53
    public static function castAttribute($type, $value)
54
    {
55
        if (is_null($value)) {
56
            return $value;
57
        }
58
59
        switch ($type) {
60
            case 'collection':
61
                return collect(is_array($value) ? $value : (new static)->fromJson($value));
62
            case 'date':
63
                return (new static)->asDate($value);
64
            case 'datetime':
65
                return (new static)->asDateTime($value);
66
            case 'bytes':
67
                return static::formatBytes($value);
68
            default:
69
                return $value;
70
        }
71
    }
72
73
    /**
74
     * Encode the given value as JSON.
75
     *
76
     * @param  mixed $value
77
     *
78
     * @return string
79
     */
80
    protected function asJson($value)
81
    {
82
        return json_encode($value);
83
    }
84
85
    /**
86
     * Decode the given JSON back into an array or object.
87
     *
88
     * @param  string $value
89
     * @param  bool   $asObject
90
     *
91
     * @return mixed
92
     */
93
    protected function fromJson($value, $asObject = false)
94
    {
95
        return json_decode($value, !$asObject);
96
    }
97
98
    /**
99
     * Return a timestamp as DateTime object with time set to 00:00:00.
100
     *
101
     * @param  mixed $value
102
     *
103
     * @return \Illuminate\Support\Carbon
104
     */
105
    protected function asDate($value)
106
    {
107
        return $this->asDateTime($value)->startOfDay();
108
    }
109
110
    /**
111
     * Return a timestamp as DateTime object.
112
     *
113
     * @param  mixed $value
114
     *
115
     * @return \Illuminate\Support\Carbon
116
     */
117
    protected function asDateTime($value)
118
    {
119
        // If this value is already a Carbon instance, we shall just return it as is.
120
        // This prevents us having to re-instantiate a Carbon instance when we know
121
        // it already is one, which wouldn't be fulfilled by the DateTime check.
122
        if ($value instanceof Carbon) {
123
            return $value;
124
        }
125
126
        // If this value is an integer, we will assume it is a UNIX timestamp's value
127
        // and format a Carbon object from this timestamp. This allows flexibility
128
        // when defining your date fields as they might be UNIX timestamps here.
129
        if (is_numeric($value)) {
130
            return Carbon::createFromTimestamp($value);
131
        }
132
133
        return $value;
134
    }
135
}