|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bagsiz\EasyFunctions; |
|
4
|
|
|
|
|
5
|
|
|
class EasyFunction |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
* Check a model's field against a random value |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
public static function checkFieldForRandom($model, string $field, string $type = 'str', int $length = 8) |
|
13
|
|
|
{ |
|
14
|
|
|
if($type == 'str') { |
|
15
|
|
|
$rnd = self::randomStr($length); |
|
16
|
|
|
} else if($type == 'int') { |
|
17
|
|
|
$rnd = self::randomInt($length); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
$continue = true; |
|
21
|
|
|
while ($continue) { |
|
22
|
|
|
|
|
23
|
|
|
$check = $model->where($field, $rnd)->first(); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
if (!$check) |
|
26
|
|
|
$continue = false; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
return $rnd; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private static function randomStr($length = 16) |
|
33
|
|
|
{ |
|
34
|
|
|
$string = ''; |
|
35
|
|
|
|
|
36
|
|
|
while (($len = strlen($string)) < $length) { |
|
37
|
|
|
$size = $length - $len; |
|
38
|
|
|
|
|
39
|
|
|
$bytes = random_bytes($size); |
|
40
|
|
|
|
|
41
|
|
|
$string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $string; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private static function randomInt($length = 4) |
|
48
|
|
|
{ |
|
49
|
|
|
$rand = ''; |
|
50
|
|
|
while( !( isset( $rand[$length-1] ) ) ) { |
|
51
|
|
|
$rand .= mt_rand( ); |
|
52
|
|
|
} |
|
53
|
|
|
return (int)substr( $rand , 0 , $length ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function decimalToTime($decimal) |
|
57
|
|
|
{ |
|
58
|
|
|
if(!$decimal) { |
|
59
|
|
|
return "Empty data given."; |
|
60
|
|
|
} |
|
61
|
|
|
if (strpos((string) $decimal, ".") !== false) { |
|
62
|
|
|
$timeArray = explode(".", $decimal); |
|
63
|
|
|
if(isset($timeArray[1])) { |
|
64
|
|
|
if(strlen((string) $timeArray[1]) == 1) { |
|
65
|
|
|
$timeArray[1] = sprintf("%1s0", $timeArray[1]); |
|
66
|
|
|
return gmdate('H:i:s', $timeArray[0]).'.'.$timeArray[1]; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
return gmdate('H:i:s', $timeArray[0]).'.'.str_pad($timeArray[1], 2, "0", STR_PAD_LEFT); |
|
69
|
|
|
} else { |
|
70
|
|
|
return gmdate('H:i:s', $timeArray[0]).'.00'; |
|
71
|
|
|
} |
|
72
|
|
|
} else { |
|
73
|
|
|
return 'Integer must contain "." for decimals'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |