Skip to content

Commit 194c238

Browse files
authored
Update Byte.php
1 parent d9c53d8 commit 194c238

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/Byte.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,13 @@ public function toString(): string
202202
*/
203203
public function to(string $units = 'b', ?int $precision = null, string $delim = ' '): string
204204
{
205-
$units = strtolower($units);
206205
foreach (self::units as $unit => $exponent) {
207-
if ($units == strtolower($unit)) {
206+
if (strcasecmp($units, $unit) === 0) {
208207
if ($precision) return round($this->value / pow(self::amount, $exponent), $precision)
209208
. "$delim$unit";
210209

211210
return $this->value / pow(self::amount, $exponent)
212-
. "$delim$units";
211+
. "$delim$unit";
213212
}
214213
}
215214

@@ -327,7 +326,7 @@ public function toYb(?int $precision = null, string $delim = ' '): string
327326
{
328327
return $this->to('yb', $precision, $delim);
329328
}
330-
329+
331330
/**
332331
* Compares this Byte instance with another value.
333332
*
@@ -469,29 +468,29 @@ public static function humanize(int|float $bytes, int $precision = 2, string $de
469468
* @throws \InvalidArgumentException If the input is a string that does not contain a valid numeric part or if the provided unit is not recognized.
470469
*
471470
* Error Description:
472-
* The method expects a string in the format "[number][unit]" (for example, "1024 kB") or a similar valid variation.
471+
* The method expects a string in the format "[number][space][unit]" (for example, "1024 kB") or a similar valid variation.
473472
* If the numeric part is not valid or the unit is not found within the supported units, the method throws an InvalidArgumentException
474473
* with the message "Failed to parse string". This error indicates that the input value does not conform to an expected byte format.
475474
*/
476475
public static function parse(self|string $value): int|float
477476
{
478477
// If the value is numeric, simply return it as an integer or float.
479478
if (is_numeric($value)) return $value + 0;
480-
479+
481480
// If the value is already a Byte instance, use its stored byte value.
482481
if ($value instanceof self) return $value->value;
483482

484483
// Assume the string format ends with a two-character unit (e.g., "MB", "GB").
485484
$bytes = trim(substr($value, 0, -2));
486-
$units = strtoupper(trim(substr($value, -2, 2)));
485+
$units = trim(substr($value, -2, 2));
487486

488487
// If the numeric part is not valid, or if the unit is not recognized, throw an exception.
489488
if (!is_numeric($bytes)) {
490489
throw new \InvalidArgumentException('Failed to parse string: The numeric portion is invalid.');
491490
}
492491

493492
foreach (self::units as $unit => $exponent) {
494-
if ($unit == $units) return $bytes * pow(self::amount, $exponent);
493+
if (strcasecmp($unit, $units) === 0) return $bytes * pow(self::amount, $exponent);
495494
}
496495

497496
throw new \InvalidArgumentException('Failed to parse string: Unrecognized unit.');

0 commit comments

Comments
 (0)