mirror of
https://github.com/antonkomarev/github-profile-views-counter.git
synced 2026-08-04 19:15:11 -04:00
Improve big integer number formatting (#75)
This commit is contained in:
@@ -110,6 +110,7 @@ You can overwrite default `Profile views` text with your own label.
|
||||
|
||||
You can provide a `base` number to add to the counter.
|
||||
This is useful if you are migrating from another service.
|
||||
|
||||
For example, a user with 1000 views on another service who wants to migrate to GHPVC will use the following url
|
||||
to ensure the 1000 views are accounted for:
|
||||
```markdown
|
||||
@@ -128,7 +129,7 @@ This project provides minimalistic counter only. Use [Ÿ HŸPE] service if you w
|
||||
|
||||
### How to reset counter?
|
||||
|
||||
To reset counter you should login to the [Ÿ HŸPE] service and then you will be able to reset counter on the https://yhype.me/ghpvc page.
|
||||
To reset counter you should log in to the [Ÿ HŸPE] service, and then you will be able to reset counter on the https://yhype.me/ghpvc page.
|
||||
|
||||
### Why does the counter increase every time the page is reloaded?
|
||||
|
||||
|
||||
@@ -63,13 +63,13 @@ try {
|
||||
);
|
||||
if ($baseCount !== '0') {
|
||||
$count = $count->plus(
|
||||
Count::ofString($baseCount)
|
||||
Count::ofString($baseCount),
|
||||
);
|
||||
}
|
||||
|
||||
echo $badgeImageRenderer->renderBadgeWithCount(
|
||||
$badgeLabel,
|
||||
$count->toInt(),
|
||||
$count,
|
||||
$badgeMessageBackgroundFill,
|
||||
$badgeStyle,
|
||||
);
|
||||
|
||||
@@ -36,11 +36,11 @@ final class BadgeImageRendererService
|
||||
|
||||
public function renderBadgeWithCount(
|
||||
string $label,
|
||||
int $count,
|
||||
Count $count,
|
||||
string $messageBackgroundFill,
|
||||
string $badgeStyle
|
||||
): string {
|
||||
$message = number_format($count);
|
||||
$message = $this->formatNumber($count->toInt());
|
||||
|
||||
return $this->renderBadge(
|
||||
$label,
|
||||
@@ -84,4 +84,17 @@ final class BadgeImageRendererService
|
||||
Badge::DEFAULT_FORMAT,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method required because of native `number_format`
|
||||
* method has big integer format limitation.
|
||||
*/
|
||||
private function formatNumber(
|
||||
int $number
|
||||
): string {
|
||||
$reversedString = strrev(strval($number));
|
||||
$formattedNumber = implode(',', str_split($reversedString, 3));
|
||||
|
||||
return strrev($formattedNumber);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,29 +22,32 @@ final class Count
|
||||
private int $count;
|
||||
|
||||
public function __construct(
|
||||
float $count
|
||||
int $count
|
||||
) {
|
||||
$this->count = $count;
|
||||
|
||||
Assert::lessThan(
|
||||
$count,
|
||||
self::MAX_COUNT,
|
||||
'The maximum number of views has been reached'
|
||||
$count,
|
||||
self::MAX_COUNT,
|
||||
'The maximum number of views has been reached',
|
||||
);
|
||||
$this->count = intval($count);
|
||||
Assert::greaterThanEq(
|
||||
$count,
|
||||
0,
|
||||
"Received a negative number of views"
|
||||
$count,
|
||||
0,
|
||||
'Number of views cannot be negative',
|
||||
);
|
||||
}
|
||||
|
||||
public static function ofString(string $countStr): self
|
||||
{
|
||||
Assert::digits(
|
||||
$countStr,
|
||||
'The base count must only contain digits'
|
||||
);
|
||||
$count = floatval($countStr);
|
||||
return new self($count);
|
||||
public static function ofString(
|
||||
string $value
|
||||
): self {
|
||||
Assert::digits(
|
||||
$value,
|
||||
'The base count must only contain digits',
|
||||
);
|
||||
$count = intval($value);
|
||||
|
||||
return new self($count);
|
||||
}
|
||||
|
||||
public function toInt(): int
|
||||
@@ -52,8 +55,17 @@ final class Count
|
||||
return $this->count;
|
||||
}
|
||||
|
||||
public function plus(self $count): self
|
||||
{
|
||||
return new self($this->toInt() + $count->toInt());
|
||||
public function plus(
|
||||
self $that
|
||||
): self {
|
||||
$sum = $this->toInt() + $that->toInt();
|
||||
|
||||
if (!is_int($sum)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'The maximum number of views has been reached',
|
||||
);
|
||||
}
|
||||
|
||||
return new self($sum);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user