-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
184 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace CustomerGauge\Math\Remainder\Definition; | ||
|
||
use LogicException; | ||
|
||
final class NullNumber implements Number | ||
{ | ||
public function integer(): ?int | ||
{ | ||
return null; | ||
} | ||
|
||
public function value(): ?float | ||
{ | ||
return null; | ||
} | ||
|
||
public function increment(): void | ||
{ | ||
throw new LogicException("A Null Value cannot be incremented."); | ||
} | ||
|
||
public function decimal(): ?float | ||
{ | ||
return null; | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace CustomerGauge\Math\Remainder\Definition; | ||
|
||
interface Number | ||
{ | ||
public function integer(): ?int; | ||
|
||
public function value(): ?float; | ||
|
||
public function increment(): void; | ||
|
||
public function decimal(): ?float; | ||
} |
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,16 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace CustomerGauge\Math\Remainder\Definition; | ||
|
||
final class NumberFactory | ||
{ | ||
|
||
public static function make($key, ?int $number, int $sum, int $precision) | ||
{ | ||
if ($sum === 0 || $number === null) { | ||
return new NullNumber(); | ||
} | ||
|
||
return new RegularNumber($key, $number, $sum, $precision); | ||
} | ||
} |
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,83 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace CustomerGauge\Math\Remainder\Definition; | ||
|
||
final class RegularNumber implements Number | ||
{ | ||
/** | ||
* Original array key that identifies the number's position. Useful to return | ||
* back an array in the exact same order that it was received. | ||
* | ||
* @var int|string | ||
*/ | ||
private $key; | ||
|
||
/** | ||
* Result of the division between $part and $total, multiplied by $precision. | ||
* | ||
* @var float|int | ||
*/ | ||
private $quotient; | ||
|
||
/** | ||
* $quotient rounded down, disregarding any decimal places. | ||
* | ||
* @var int | ||
*/ | ||
private $integer; | ||
|
||
/** | ||
* Final value after applying the Largest Remainder Method. It starts as $integer, but | ||
* can be incremented when deemed necessary. | ||
* | ||
* @var int | ||
*/ | ||
private $value; | ||
|
||
/** | ||
* All of the decimal places of quotient. It is the difference between $quotient - $integer. | ||
* It is used to determine which numbers receive an increment by sorting on largest | ||
* decimal places. | ||
* | ||
* @var float | ||
*/ | ||
private $decimal; | ||
|
||
/** | ||
* Amount of decimal places that will be taken into account before incrementing. | ||
* | ||
* @var int | ||
*/ | ||
private $precision; | ||
|
||
public function __construct($key, ?int $part, ?int $total, int $precision) | ||
{ | ||
$this->key = $key; | ||
$this->quotient = ($part / $total) * $precision; | ||
$this->integer = (int) $this->quotient; | ||
$this->value = $this->integer; | ||
$this->decimal = $this->quotient - $this->integer; | ||
$this->precision = $precision; | ||
} | ||
|
||
public function integer(): int | ||
{ | ||
return $this->integer; | ||
} | ||
|
||
public function value(): float | ||
{ | ||
// Since the quotient was multiplied by the precision, we need to divide it back. | ||
return $this->value / $this->precision; | ||
} | ||
|
||
public function increment(): void | ||
{ | ||
$this->value++; | ||
} | ||
|
||
public function decimal(): float | ||
{ | ||
return $this->decimal; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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