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 'int': |
61
|
|
|
case 'integer': |
62
|
|
|
return (int)$value; |
63
|
|
|
case 'real': |
64
|
|
|
case 'float': |
65
|
|
|
case 'double': |
66
|
|
|
return (float)$value; |
67
|
|
|
case 'string': |
68
|
|
|
return (string)$value; |
69
|
|
|
case 'bool': |
70
|
|
|
case 'boolean': |
71
|
|
|
return (bool)$value; |
72
|
|
|
case 'object': |
73
|
|
|
return (new static)->fromJson($value, true); |
74
|
|
|
case 'array': |
75
|
|
|
case 'json': |
76
|
|
|
return (new static)->fromJson($value); |
77
|
|
|
case 'collection': |
78
|
|
|
return collect(is_array($value) ? $value : (new static)->fromJson($value)); |
79
|
|
|
case 'date': |
80
|
|
|
return (new static)->asDate($value); |
81
|
|
|
case 'datetime': |
82
|
|
|
return (new static)->asDateTime($value); |
83
|
|
|
case 'bytes': |
84
|
|
|
return static::formatBytes($value); |
85
|
|
|
break; |
|
|
|
|
86
|
|
|
default: |
87
|
|
|
return $value; |
88
|
|
|
break; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Encode the given value as JSON. |
94
|
|
|
* |
95
|
|
|
* @param mixed $value |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function asJson($value) |
100
|
|
|
{ |
101
|
|
|
return json_encode($value); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Decode the given JSON back into an array or object. |
106
|
|
|
* |
107
|
|
|
* @param string $value |
108
|
|
|
* @param bool $asObject |
109
|
|
|
* |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
protected function fromJson($value, $asObject = false) |
113
|
|
|
{ |
114
|
|
|
return json_decode($value, !$asObject); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return a timestamp as DateTime object with time set to 00:00:00. |
119
|
|
|
* |
120
|
|
|
* @param mixed $value |
121
|
|
|
* |
122
|
|
|
* @return \Illuminate\Support\Carbon |
123
|
|
|
*/ |
124
|
|
|
protected function asDate($value) |
125
|
|
|
{ |
126
|
|
|
return $this->asDateTime($value)->startOfDay(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Return a timestamp as DateTime object. |
131
|
|
|
* |
132
|
|
|
* @param mixed $value |
133
|
|
|
* |
134
|
|
|
* @return \Illuminate\Support\Carbon |
135
|
|
|
*/ |
136
|
|
|
protected function asDateTime($value) |
137
|
|
|
{ |
138
|
|
|
// If this value is already a Carbon instance, we shall just return it as is. |
139
|
|
|
// This prevents us having to re-instantiate a Carbon instance when we know |
140
|
|
|
// it already is one, which wouldn't be fulfilled by the DateTime check. |
141
|
|
|
if ($value instanceof Carbon) { |
142
|
|
|
return $value; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// If this value is an integer, we will assume it is a UNIX timestamp's value |
146
|
|
|
// and format a Carbon object from this timestamp. This allows flexibility |
147
|
|
|
// when defining your date fields as they might be UNIX timestamps here. |
148
|
|
|
if (is_numeric($value)) { |
149
|
|
|
return Carbon::createFromTimestamp($value); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $value; |
153
|
|
|
} |
154
|
|
|
} |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.