Completed
Push — master ( 0babfa...04d5c7 )
by Murat
05:43
created

EasyFunction::decimalToTime()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 18
rs 9.5222
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();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rnd does not seem to be defined for all execution paths leading up to this point.
Loading history...
24
25
			if (!$check)
26
				$continue = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $continue is dead and can be removed.
Loading history...
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];
0 ignored issues
show
Bug introduced by
$timeArray[0] of type string is incompatible with the type integer expected by parameter $timestamp of gmdate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
                    return gmdate('H:i:s', /** @scrutinizer ignore-type */ $timeArray[0]).'.'.$timeArray[1];
Loading history...
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
}