forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.php
More file actions
77 lines (66 loc) · 1.96 KB
/
helpers.php
File metadata and controls
77 lines (66 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
use ChrisUllyott\FileSize;
if (! function_exists('convertSize')) {
/**
* Using FileSize convert bytes to the output format with precision.
*/
function convertSize(float $input, string $output = 'MB', int $precision = 4): float
{
$size = new FileSize($input, 10);
return (float) $size->as($output, $precision);
}
}
if (! function_exists('toBits')) {
/**
* Takes a byte based float and transforms it into bits with precision.
*/
function toBits(float $size, int $precision = 4): float
{
return (float) number_format(($size * 8), $precision, '.', '');
}
}
if (! function_exists('percentChange')) {
function percentChange(float $dividend, float $divisor, int $precision = 0): string
{
$quotient = ($dividend - $divisor) / $divisor;
return number_format(round($quotient * 100, $precision), $precision);
}
}
if (! function_exists('absoluteDownloadThresholdFailed')) {
function absoluteDownloadThresholdFailed(float $threshold, float $download): bool
{
return toBits(convertSize($download), 2) < $threshold;
}
}
if (! function_exists('absoluteUploadThresholdFailed')) {
function absoluteUploadThresholdFailed(float $threshold, float $upload): bool
{
return toBits(convertSize($upload), 2) < $threshold;
}
}
if (! function_exists('absolutePingThresholdFailed')) {
function absolutePingThresholdFailed(float $threshold, float $ping): bool
{
return $ping > $threshold;
}
}
/**
* Determine if the string provided is valid json.
*
* This function will be overwritten in php 8.3 https://wiki.php.net/rfc/json_validate
*
* @deprecated
*
* @param string $data
* @return bool
*/
if (! function_exists('json_validate')) {
function json_validate($data)
{
if (! empty($data)) {
return is_string($data) &&
is_array(json_decode($data, true)) ? true : false;
}
return false;
}
}