-
-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters