From 97f5f8276033cd5671e257aaba7c2560a5f5926a Mon Sep 17 00:00:00 2001 From: Omar Brikaa Date: Sat, 16 Sep 2023 19:56:36 +0300 Subject: [PATCH] Ability to provide a base number to be added to the counter (#74) * 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 --- README.md | 10 ++++++++ public/index.php | 13 +++++++++-- src/Count.php | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Request.php | 12 +++++++++- 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 src/Count.php diff --git a/README.md b/README.md index 92a19bd..0ca05b9 100644 --- a/README.md +++ b/README.md @@ -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? diff --git a/public/index.php b/public/index.php index f766e55..d357ff4 100644 --- a/public/index.php +++ b/public/index.php @@ -16,6 +16,7 @@ use Komarev\GitHubProfileViewsCounter\CounterRepositoryFactory; use Komarev\GitHubProfileViewsCounter\Request; use Komarev\GitHubProfileViewsCounter\Username; +use Komarev\GitHubProfileViewsCounter\Count; $appBasePath = realpath(__DIR__ . '/..'); @@ -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'; @@ -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, ); diff --git a/src/Count.php b/src/Count.php new file mode 100644 index 0000000..046595f --- /dev/null +++ b/src/Count.php @@ -0,0 +1,59 @@ + + * + * 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()); + } +} diff --git a/src/Request.php b/src/Request.php index 8cc70d2..d704543 100644 --- a/src/Request.php +++ b/src/Request.php @@ -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( @@ -53,6 +57,7 @@ public static function of( $get['label'] ?? null, $get['color'] ?? null, $get['style'] ?? null, + $get['base'] ?? null, ); } @@ -80,4 +85,9 @@ public function badgeStyle(): ?string { return $this->badgeStyle; } + + public function baseCount(): ?string + { + return $this->baseCount; + } }