mirror of
https://github.com/antonkomarev/github-profile-views-counter.git
synced 2026-03-31 06:24:11 -04:00
Feat: Abbreviation (#88)
* Feat: Abbreviation --------- Co-authored-by: Anton Komarev
This commit is contained in:
10
README.md
10
README.md
@@ -117,6 +117,16 @@ to ensure the 1000 views are accounted for:
|
||||

|
||||
```
|
||||
|
||||
### Abbreviation
|
||||
|
||||
You can set the `abbreviated` parameter to `true` if you would like the counter to be abbreviated.
|
||||
|
||||
For example, a counter with 12345 views, will be displayed as 12K.
|
||||
|
||||
```markdown
|
||||

|
||||
```
|
||||
|
||||
## FAQ
|
||||
|
||||
### Can I see detailed statistics?
|
||||
|
||||
@@ -36,6 +36,7 @@ $isGitHubUserAgent = strpos($request->userAgent(), 'github-camo') === 0;
|
||||
$badgeLabel = $request->badgeLabel() ?? 'Profile views';
|
||||
$badgeMessageBackgroundFill = $request->badgeColor() ?? 'blue';
|
||||
$baseCount = $request->baseCount() ?? '0';
|
||||
$isCountAbbreviated = $request->isCountAbbreviated();
|
||||
$badgeStyle = $request->badgeStyle() ?? 'flat';
|
||||
if (!in_array($badgeStyle, ['flat', 'flat-square', 'plastic', 'for-the-badge', 'pixel'], true)) {
|
||||
$badgeStyle = 'flat';
|
||||
@@ -72,6 +73,7 @@ try {
|
||||
$count,
|
||||
$badgeMessageBackgroundFill,
|
||||
$badgeStyle,
|
||||
$isCountAbbreviated,
|
||||
);
|
||||
}
|
||||
} catch (InvalidPathException $exception) {
|
||||
|
||||
@@ -24,6 +24,8 @@ final class BadgeImageRendererService
|
||||
{
|
||||
private Poser $poser;
|
||||
|
||||
private const ABBREVIATIONS = ['', 'K', 'M', 'B', 'T', 'Qa', 'Qi'];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->poser = new Poser([
|
||||
@@ -38,9 +40,10 @@ final class BadgeImageRendererService
|
||||
string $label,
|
||||
Count $count,
|
||||
string $messageBackgroundFill,
|
||||
string $badgeStyle
|
||||
string $badgeStyle,
|
||||
bool $isCountAbbreviated
|
||||
): string {
|
||||
$message = $this->formatNumber($count->toInt());
|
||||
$message = $this->formatNumber($count->toInt(), $isCountAbbreviated);
|
||||
|
||||
return $this->renderBadge(
|
||||
$label,
|
||||
@@ -90,11 +93,29 @@ final class BadgeImageRendererService
|
||||
* method has big integer format limitation.
|
||||
*/
|
||||
private function formatNumber(
|
||||
int $number
|
||||
int $number,
|
||||
bool $isCountAbbreviated
|
||||
): string {
|
||||
if ($isCountAbbreviated) {
|
||||
return $this->formatAbbreviatedNumber($number);
|
||||
}
|
||||
|
||||
$reversedString = strrev(strval($number));
|
||||
$formattedNumber = implode(',', str_split($reversedString, 3));
|
||||
|
||||
return strrev($formattedNumber);
|
||||
}
|
||||
|
||||
public function formatAbbreviatedNumber(
|
||||
int $number
|
||||
): string {
|
||||
$abbreviationIndex = 0;
|
||||
|
||||
while ($number >= 1000) {
|
||||
$number /= 1000;
|
||||
$abbreviationIndex++;
|
||||
}
|
||||
|
||||
return round($number, 1) . self::ABBREVIATIONS[$abbreviationIndex];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,13 +27,16 @@ final class Request
|
||||
|
||||
private ?string $baseCount;
|
||||
|
||||
private bool $isCountAbbreviated;
|
||||
|
||||
public function __construct(
|
||||
string $userAgent,
|
||||
string $username,
|
||||
?string $badgeLabel,
|
||||
?string $badgeColor,
|
||||
?string $badgeStyle,
|
||||
?string $baseCount
|
||||
?string $baseCount,
|
||||
bool $isCountAbbreviated
|
||||
) {
|
||||
$this->userAgent = $userAgent;
|
||||
$this->username = $username;
|
||||
@@ -41,6 +44,7 @@ final class Request
|
||||
$this->badgeColor = $badgeColor;
|
||||
$this->badgeStyle = $badgeStyle;
|
||||
$this->baseCount = $baseCount;
|
||||
$this->isCountAbbreviated = $isCountAbbreviated;
|
||||
}
|
||||
|
||||
public static function of(
|
||||
@@ -58,6 +62,7 @@ final class Request
|
||||
$get['color'] ?? null,
|
||||
$get['style'] ?? null,
|
||||
$get['base'] ?? null,
|
||||
boolval($get['abbreviated'] ?? false),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -90,4 +95,9 @@ final class Request
|
||||
{
|
||||
return $this->baseCount;
|
||||
}
|
||||
|
||||
public function isCountAbbreviated(): bool
|
||||
{
|
||||
return $this->isCountAbbreviated;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user