Skip to content

Commit

Permalink
Ability to provide a base number to be added to the counter (#74)
Browse files Browse the repository at this point in the history
* Ability to provide a base number to be added to the counter

* baseNumber -> baseCount

* Throw an error if the base count or the final count is inappropriate

* BaseCount, Count -> Count & PHP_INT_MAX for max base count

* Remove unneeded comment

* Add negative count check in the constructor
  • Loading branch information
Brikaa committed Sep 16, 2023
1 parent f2be5be commit 97f5f82
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ You can overwrite default `Profile views` text with your own label.

> **NOTE**: Replace whitespace with `+` character in multi-word labels.
### Base number

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
![](https://komarev.com/ghpvc/?username=your-github-username&base=1000)
```

## FAQ

### Can I see detailed statistics?
Expand Down
13 changes: 11 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Komarev\GitHubProfileViewsCounter\CounterRepositoryFactory;
use Komarev\GitHubProfileViewsCounter\Request;
use Komarev\GitHubProfileViewsCounter\Username;
use Komarev\GitHubProfileViewsCounter\Count;

$appBasePath = realpath(__DIR__ . '/..');

Expand All @@ -34,6 +35,7 @@

$badgeLabel = $request->badgeLabel() ?? 'Profile views';
$badgeMessageBackgroundFill = $request->badgeColor() ?? 'blue';
$baseCount = $request->baseCount() ?? '0';
$badgeStyle = $request->badgeStyle() ?? 'flat';
if (!in_array($badgeStyle, ['flat', 'flat-square', 'plastic', 'for-the-badge', 'pixel'], true)) {
$badgeStyle = 'flat';
Expand All @@ -56,11 +58,18 @@
if ($badgeStyle === 'pixel') {
echo $badgeImageRenderer->renderPixel();
} else {
$count = $counterRepository->getViewsCountByUsername($username);
$count = new Count(
$counterRepository->getViewsCountByUsername($username)
);
if ($baseCount !== '0') {
$count = $count->plus(
Count::ofString($baseCount)
);
}

echo $badgeImageRenderer->renderBadgeWithCount(
$badgeLabel,
$count,
$count->toInt(),
$badgeMessageBackgroundFill,
$badgeStyle,
);
Expand Down
59 changes: 59 additions & 0 deletions src/Count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of GitHub Profile Views Counter.
*
* (c) Anton Komarev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Komarev\GitHubProfileViewsCounter;

use Webmozart\Assert\Assert;

final class Count
{
private const MAX_COUNT = PHP_INT_MAX;

private int $count;

public function __construct(
float $count
) {
Assert::lessThan(
$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"
);
}

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 function toInt(): int
{
return $this->count;
}

public function plus(self $count): self
{
return new self($this->toInt() + $count->toInt());
}
}
12 changes: 11 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ final class Request

private ?string $badgeStyle;

private ?string $baseCount;

public function __construct(
string $userAgent,
string $username,
?string $badgeLabel,
?string $badgeColor,
?string $badgeStyle
?string $badgeStyle,
?string $baseCount
) {
$this->userAgent = $userAgent;
$this->username = $username;
$this->badgeLabel = $badgeLabel;
$this->badgeColor = $badgeColor;
$this->badgeStyle = $badgeStyle;
$this->baseCount = $baseCount;
}

public static function of(
Expand All @@ -53,6 +57,7 @@ public static function of(
$get['label'] ?? null,
$get['color'] ?? null,
$get['style'] ?? null,
$get['base'] ?? null,
);
}

Expand Down Expand Up @@ -80,4 +85,9 @@ public function badgeStyle(): ?string
{
return $this->badgeStyle;
}

public function baseCount(): ?string
{
return $this->baseCount;
}
}

0 comments on commit 97f5f82

Please sign in to comment.