|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Utility - Collection of various PHP utility functions. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Eric Sizemore <[email protected]> |
|
9
|
|
|
* @version 2.0.0 |
|
10
|
|
|
* @copyright (C) 2017 - 2024 Eric Sizemore |
|
11
|
|
|
* @license The MIT License (MIT) |
|
12
|
|
|
* |
|
13
|
|
|
* Copyright (C) 2017 - 2024 Eric Sizemore <https://www.secondversion.com>. |
|
14
|
|
|
* |
|
15
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
16
|
|
|
* of this software and associated documentation files (the "Software"), to |
|
17
|
|
|
* deal in the Software without restriction, including without limitation the |
|
18
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
19
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
|
20
|
|
|
* furnished to do so, subject to the following conditions: |
|
21
|
|
|
* |
|
22
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
23
|
|
|
* all copies or substantial portions of the Software. |
|
24
|
|
|
* |
|
25
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
26
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
27
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
28
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
29
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
30
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
31
|
|
|
* THE SOFTWARE. |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
namespace Esi\Utility; |
|
35
|
|
|
|
|
36
|
|
|
// Exceptions |
|
37
|
|
|
use Random\RandomException; |
|
|
|
|
|
|
38
|
|
|
use ValueError; |
|
39
|
|
|
use InvalidArgumentException; |
|
40
|
|
|
|
|
41
|
|
|
// Functions |
|
42
|
|
|
use function number_format; |
|
43
|
|
|
use function abs; |
|
44
|
|
|
use function random_int; |
|
45
|
|
|
use function sprintf; |
|
46
|
|
|
use function count; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Number utilities. |
|
50
|
|
|
*/ |
|
51
|
|
|
final class Numbers |
|
52
|
|
|
{ |
|
53
|
|
|
/** |
|
54
|
|
|
* inside() |
|
55
|
|
|
* |
|
56
|
|
|
* Determines if a number is inside the min and max. |
|
57
|
|
|
* |
|
58
|
|
|
* @param float|int $number The number to check. |
|
59
|
|
|
* @param float|int $min The minimum. |
|
60
|
|
|
* @param float|int $max The maximum. |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function inside(float | int $number, float | int $min, float | int $max): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return ($number >= $min && $number <= $max); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* outside() |
|
70
|
|
|
* |
|
71
|
|
|
* Determines if a number is outside the min and max. |
|
72
|
|
|
* |
|
73
|
|
|
* @param float|int $number The number to check. |
|
74
|
|
|
* @param float|int $min The minimum. |
|
75
|
|
|
* @param float|int $max The maximum. |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function outside(float | int $number, float | int $min, float | int $max): bool |
|
79
|
|
|
{ |
|
80
|
|
|
return ($number < $min || $number > $max); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* random() |
|
85
|
|
|
* |
|
86
|
|
|
* Generate a cryptographically secure pseudo-random integer. |
|
87
|
|
|
* |
|
88
|
|
|
* @param int<min, max> $min The lowest value to be returned, which must be PHP_INT_MIN or higher. |
|
89
|
|
|
* @param int<min, max> $max The highest value to be returned, which must be less than or equal to PHP_INT_MAX. |
|
90
|
|
|
* @return int<min, max> |
|
91
|
|
|
* |
|
92
|
|
|
* @throws RandomException | ValueError |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function random(int $min, int $max): int |
|
95
|
|
|
{ |
|
96
|
|
|
// Generate random int |
|
97
|
|
|
return random_int($min, $max); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* ordinal() |
|
102
|
|
|
* |
|
103
|
|
|
* Retrieve the ordinal version of a number. |
|
104
|
|
|
* |
|
105
|
|
|
* Basically, it will append th, st, nd, or rd based on what the number ends with. |
|
106
|
|
|
* |
|
107
|
|
|
* @param int $number The number to create an ordinal version of. |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function ordinal(int $number): string |
|
111
|
|
|
{ |
|
112
|
|
|
static $suffixes = ['th', 'st', 'nd', 'rd']; |
|
113
|
|
|
|
|
114
|
|
|
$absNumber = abs($number); |
|
115
|
|
|
|
|
116
|
|
|
$suffix = $absNumber % 100 >= 11 && $absNumber % 100 <= 13 ? $suffixes[0] : $suffixes[$absNumber % 10] ?? $suffixes[0]; |
|
117
|
|
|
return $number . $suffix; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* sizeFormat() |
|
122
|
|
|
* |
|
123
|
|
|
* Format bytes to a human-readable format. |
|
124
|
|
|
* |
|
125
|
|
|
* @param int $bytes The number in bytes. |
|
126
|
|
|
* @param int $precision Sets the number of decimal digits. |
|
127
|
|
|
* @param string $standard Determines which mod ('base') to use in the conversion. |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public static function sizeFormat(int $bytes, int $precision = 0, string $standard = 'binary'): string |
|
131
|
|
|
{ |
|
132
|
|
|
// The units/labels for each 'system' |
|
133
|
|
|
static $standards = [ |
|
134
|
|
|
'binary' => ['base' => 1024, 'units' => ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']], |
|
135
|
|
|
'metric' => ['base' => 1000, 'units' => ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']], |
|
136
|
|
|
]; |
|
137
|
|
|
|
|
138
|
|
|
// Just a sanity check |
|
139
|
|
|
if (!isset($standards[$standard])) { |
|
140
|
|
|
throw new InvalidArgumentException('Invalid $standard specified, must be either metric or binary'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Metric or Binary? |
|
144
|
|
|
$base = $standards[$standard]['base']; |
|
145
|
|
|
$units = $standards[$standard]['units']; |
|
146
|
|
|
|
|
147
|
|
|
// If $bytes is less than our base, there is no need for any conversion |
|
148
|
|
|
if ($bytes < $base) { |
|
149
|
|
|
return sprintf('%s %s', $bytes, $units[0]); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// Perform the conversion |
|
153
|
|
|
for ($i = 0; ($bytes / $base) > 0.9 && ($i < count($units) - 1); $i++) { |
|
154
|
|
|
$bytes /= $base; |
|
155
|
|
|
} |
|
156
|
|
|
// @phpstan-ignore-next-line |
|
157
|
|
|
return number_format($bytes, $precision, '.', '') . ' ' . $units[$i]; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths