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