Passed
Push — master ( beaa4b...da3f2a )
by Shahrad
02:02
created

Toolkit   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 85
c 0
b 0
f 0
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A prettyJson() 0 3 1
A buildUrl() 0 8 2
A randomString() 0 9 2
A bytesToHuman() 0 16 4
A insensitiveString() 0 3 1
1
<?php
2
3
namespace EasyHttp\Utils;
4
5
/**
6
 * Toolkit class
7
 *
8
 * @link    https://github.com/shahradelahi/easy-http
9
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
10
 * @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License)
11
 */
12
class Toolkit
13
{
14
15
    /**
16
     * Builds url from array of parameters
17
     *
18
     * @param string $base The base url
19
     * @param array $body Each segment of the url
20
     * @param array $params The query string parameters
21
     * @return string
22
     */
23
    public static function buildUrl(string $base, array $body, array $params): string
24
    {
25
        $url = $base;
26
        $url .= '/' . implode('/', $body);
27
        if (count($params) > 0) {
28
            $url .= '?' . http_build_query($params);
29
        }
30
        return $url;
31
    }
32
33
    /**
34
     * Generates a random string
35
     *
36
     * @param int $length The length of the string
37
     * @return string
38
     */
39
    public static function randomString(int $length = 10): string
40
    {
41
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
42
        $charactersLength = strlen($characters);
43
        $randomString = '';
44
        for ($i = 0; $i < $length; $i++) {
45
            $randomString .= $characters[rand(0, $charactersLength - 1)];
46
        }
47
        return $randomString;
48
    }
49
50
    /**
51
     * Make Json string pretty
52
     *
53
     * @param string $json The json string
54
     * @return string
55
     */
56
    public static function prettyJson(string $json): string
57
    {
58
        return json_encode(json_decode($json), JSON_PRETTY_PRINT);
59
    }
60
61
    /**
62
     * Convert bytes to human-readable format
63
     *
64
     * @param int $bytes The bytes
65
     * @param bool $binaryPrefix Whether to use binary prefixes
66
     * @return string
67
     */
68
    public static function bytesToHuman(int $bytes, bool $binaryPrefix = true): string
69
    {
70
        if ($binaryPrefix) {
71
            $unit = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
72
            if ($bytes == 0) {
73
                return '0 ' . $unit[0];
74
            }
75
76
            return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 2) . ' ' . ($unit[$i] ?? 'B');
77
        } else {
78
            $unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
79
            if ($bytes == 0) {
80
                return '0 ' . $unit[0];
81
            }
82
83
            return @round($bytes / pow(1000, ($i = floor(log($bytes, 1000)))), 2) . ' ' . ($unit[$i] ?? 'B');
84
        }
85
    }
86
87
    /**
88
     * Validate insensitive case string
89
     *
90
     * @param string $string The string
91
     * @param string $value The second value to compare with
92
     * @return bool
93
     */
94
    public static function insensitiveString(string $string, string $value): bool
95
    {
96
        return (bool)preg_match_all('/' . $value . '/i', $string);
97
    }
98
99
}