Skip to content

Commit 2800c5f

Browse files
committed
add functions
1 parent a40f1b0 commit 2800c5f

File tree

3 files changed

+68
-40
lines changed

3 files changed

+68
-40
lines changed

src/LazyArrayAccess.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,39 @@
66
use ArrayIterator;
77
use IteratorAggregate;
88

9+
/**
10+
* @template T
11+
* @implements ArrayAccess<T>
12+
*/
913
final class LazyArrayAccess implements ArrayAccess
1014
{
1115

12-
/** @var callable */
16+
/** @var callable(): T */
1317
private $callback;
1418

15-
private mixed $arrayAccess;
19+
/** @var T[] */
20+
private array $cache;
1621

22+
/**
23+
* @param callable(): T
24+
*/
1725
public function __construct(callable $callback)
1826
{
1927
$this->callback = $callback;
2028
}
2129

22-
public function getSource(): mixed
30+
/**
31+
* @return T[]|ArrayAccess<T>
32+
*/
33+
public function getSource(): array|ArrayAccess
2334
{
2435
return ($this->callback)();
2536
}
2637

27-
private function getArrayAccess(): mixed
38+
/**
39+
* @return T[]|ArrayAccess<T>
40+
*/
41+
private function getArrayAccess(): array|ArrayAccess
2842
{
2943
if (!isset($this->arrayAccess)) {
3044
$result = ($this->callback)();
@@ -43,11 +57,17 @@ public function offsetExists(mixed $offset): bool
4357
return isset($this->getArrayAccess()[$offset]) || array_key_exists($offset, $this->array);
4458
}
4559

60+
/**
61+
* @return T
62+
*/
4663
public function offsetGet(mixed $offset): mixed
4764
{
4865
return $this->getArrayAccess()[$offset];
4966
}
5067

68+
/**
69+
* @param T $value
70+
*/
5171
public function offsetSet(mixed $offset, mixed $value): void
5272
{
5373
$this->getArrayAccess();

src/Numbers.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@
22

33
namespace Utilitte\Php;
44

5+
use InvalidArgumentException;
56
use JetBrains\PhpStorm\Deprecated;
67
use Utilitte\Php\Numbers\NumberFormatter;
78

89
final class Numbers
910
{
1011

12+
public static function convertToFloat(string|int|float $number): float
13+
{
14+
if (!is_numeric($number)) {
15+
throw new InvalidArgumentException('Given argument is not a number.');
16+
}
17+
18+
$number = (float) $number;
19+
20+
if (is_nan($number) || is_infinite($number)) {
21+
throw new InvalidArgumentException('Given argument is not a number.');
22+
}
23+
24+
return $number;
25+
}
26+
1127
public static function minMax(int $value, int $min, int $max): int
1228
{
1329
return min(max($value, $min), $max);

src/Numbers/NumberFormatter.php

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
namespace Utilitte\Php\Numbers;
44

5+
use InvalidArgumentException;
56
use LogicException;
7+
use Utilitte\Php\Numbers;
68

79
final class NumberFormatter
810
{
911

10-
public static function formatBytes(int|float $number, ?int $decimals = null, bool $fixed = false): string
12+
public static function formatBytes(string|int|float $number, ?int $decimals = null, bool $fixed = true): string
1113
{
1214
static $end = 'PB';
1315
static $units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'];
1416

17+
$number = Numbers::convertToFloat($number);
18+
1519
$unit = 'B';
1620
foreach ($units as $unit) {
1721
if (abs($number) < 1024 || $unit === $end) {
@@ -21,10 +25,10 @@ public static function formatBytes(int|float $number, ?int $decimals = null, boo
2125
$number /= 1024;
2226
}
2327

24-
return self::formatNumber($number, $decimals, $fixed). $unit;
28+
return self::formatNumber($number, $decimals, $fixed) . $unit;
2529
}
2630

27-
public static function formatShort(int|float $number, ?int $decimals = null, bool $fixed = false): string
31+
public static function formatShort(string|int|float $number, ?int $decimals = null, bool $fixed = true): string
2832
{
2933
static $formatters = [
3034
'' => 1_000,
@@ -34,6 +38,7 @@ public static function formatShort(int|float $number, ?int $decimals = null, boo
3438
'T' => null,
3539
];
3640

41+
$number = Numbers::convertToFloat($number);
3742
$divider = 1;
3843
foreach ($formatters as $str => $limit) {
3944
if ($limit === null || $number < $limit) {
@@ -47,48 +52,35 @@ public static function formatShort(int|float $number, ?int $decimals = null, boo
4752
}
4853

4954
public static function formatPercentage(
50-
string|int|float|null $number,
51-
int $decimals = 2,
52-
bool $sign = true,
55+
string|int|float $number,
56+
?int $decimals = null,
5357
bool $fixed = true,
54-
): ?string
58+
bool $sign = false,
59+
): string
5560
{
56-
if ($number === null || is_string($number) && !is_numeric($number)) {
57-
return null;
58-
}
59-
60-
$value = self::formatNumber($number, $decimals, $fixed);
61-
62-
if ($value === null) {
63-
return null;
64-
}
65-
66-
return ($sign && $number > 0 ? '+' : '') . $value . '%';
61+
return self::formatNumber($number, $decimals, $fixed) . '%';
6762
}
6863

69-
public static function formatNumber(string|int|float|null $number, ?int $decimals = null, bool $fixed = false): ?string
64+
public static function formatNumber(
65+
string|int|float|null $number,
66+
?int $decimals = null,
67+
bool $fixed = true,
68+
bool $sign = false,
69+
): string
7070
{
71-
if (!is_numeric($number)) {
72-
return null;
73-
}
74-
75-
$number = (float) $number;
76-
77-
if (is_nan($number) || is_infinite($number)) {
78-
return null;
79-
}
71+
$number = Numbers::convertToFloat($number);
8072

8173
if ($decimals === null) {
82-
$decimals = 2;
83-
84-
if ($number < 0.01) {
85-
$decimals = 6;
86-
} elseif ($number < 0.1) {
74+
if ($fixed === true) {
75+
$decimals = 0;
76+
} elseif ($number < 0.01) {
8777
$decimals = 5;
88-
} elseif ($number < 1) {
78+
} elseif ($number < 0.1) {
8979
$decimals = 4;
90-
} elseif ($number < 10) {
80+
} elseif ($number < 1) {
9181
$decimals = 3;
82+
} else {
83+
$decimals = 2;
9284
}
9385
}
9486

@@ -98,7 +90,7 @@ public static function formatNumber(string|int|float|null $number, ?int $decimal
9890
$number = self::removeZerosAfterDot($number);
9991
}
10092

101-
return $number;
93+
return ($sign && $number > 0 ? '+' : '') . $number . '%';
10294
}
10395

10496
public static function removeZerosAfterDot(string $number): string

0 commit comments

Comments
 (0)