-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Percentage)!: Tax renamed Vat, Percent renamed Discount
- Loading branch information
Showing
12 changed files
with
242 additions
and
186 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,30 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\Format\Number; | ||
|
||
use h4kuna\Format\Utils; | ||
|
||
/** | ||
* properties will become readonly | ||
*/ | ||
final class Discount extends Percentage | ||
{ | ||
|
||
public function diff(float $number): float | ||
{ | ||
return Utils\Percentage::diffDeduct($this->percentage, $number); | ||
} | ||
|
||
|
||
public function deduct(float $number): float | ||
{ | ||
return Utils\Percentage::deduct($this->percentage, $number); | ||
} | ||
|
||
|
||
public function diffWith(float $number): float | ||
{ | ||
return Utils\Percentage::diffWith($this->ratio, $number); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\Format\Number; | ||
|
||
use h4kuna\Format\Utils; | ||
|
||
abstract class Percentage | ||
{ | ||
public float $ratio; | ||
|
||
public float $smallRatio; | ||
|
||
|
||
public function __construct(public float $percentage) | ||
{ | ||
$this->smallRatio = Utils\Percentage::smallRatio($this->percentage); | ||
$this->ratio = Utils\Percentage::ratio($this->smallRatio); | ||
} | ||
|
||
|
||
public function with(float $number): float | ||
{ | ||
return Utils\Percentage::with($this->ratio, $number); | ||
} | ||
|
||
|
||
abstract public function diff(float $number): float; | ||
|
||
|
||
public function __toString() | ||
{ | ||
return (string) $this->percentage; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\Format\Number; | ||
|
||
use h4kuna\Format\Utils; | ||
|
||
/** | ||
* properties will become readonly | ||
*/ | ||
class Vat extends Percentage | ||
{ | ||
|
||
public function diff(float $number): float | ||
{ | ||
return Utils\Percentage::diffWithout($this->ratio, $number); | ||
} | ||
|
||
|
||
public function without(float $number): float | ||
{ | ||
return Utils\Percentage::without($this->ratio, $number); | ||
} | ||
|
||
} |
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,81 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\Format\Utils; | ||
|
||
/** | ||
* Parameter $percentage is float represent percent, for example 17.5% -> 17.5 | ||
*/ | ||
final class Percentage | ||
{ | ||
/** | ||
* 17.5% -> 0.175 | ||
*/ | ||
public static function smallRatio(float $percentage): float | ||
{ | ||
return $percentage / 100; | ||
} | ||
|
||
|
||
/** | ||
* 17.5% -> 1.175 | ||
*/ | ||
public static function ratio(float $smallRatio): float | ||
{ | ||
return $smallRatio + 1.0; | ||
} | ||
|
||
|
||
/** | ||
* N + 17.5% -> N * 1.175 | ||
*/ | ||
public static function with(float $ratio, float $number): float | ||
{ | ||
return $number * $ratio; | ||
} | ||
|
||
|
||
/** | ||
* N * 1.175 - N | ||
*/ | ||
public static function diffWith(float $ratio, float $number): float | ||
{ | ||
return self::with($ratio, $number) - $number; | ||
} | ||
|
||
|
||
/** | ||
* If you have price with percentage, and you need price without percentage. | ||
* N / 1.175 | ||
*/ | ||
public static function without(float $ratio, float $number): float | ||
{ | ||
return $number / $ratio; | ||
} | ||
|
||
|
||
/** | ||
* N - (N / 1.175) | ||
*/ | ||
public static function diffWithout(float $ratio, float $number): float | ||
{ | ||
return $number - self::without($ratio, $number); | ||
} | ||
|
||
|
||
/** | ||
* N - ((N * 17.5) / 100) | ||
*/ | ||
public static function deduct(float $percentage, float $number): float | ||
{ | ||
return $number - self::diffDeduct($percentage, $number); | ||
} | ||
|
||
|
||
/** | ||
* (N * 17.5) / 100 | ||
*/ | ||
public static function diffDeduct(float $percentage, float $number): float | ||
{ | ||
return self::smallRatio($number * $percentage); | ||
} | ||
} |
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,32 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\Format\Tests\Number; | ||
|
||
use h4kuna\Format\Number\Discount; | ||
use h4kuna\Format\Tests\TestCase; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../../bootstrap.php'; | ||
|
||
/** | ||
* @testCase | ||
*/ | ||
final class DiscountTest extends TestCase | ||
{ | ||
|
||
public function testDiscount(): void | ||
{ | ||
$discount = new Discount(20); | ||
Assert::same(120.0, $discount->with(100)); | ||
Assert::same(96.0, $discount->deduct(120)); | ||
Assert::same(24.0, $discount->diff(120)); | ||
Assert::same(20.0, $discount->diffWith(100)); | ||
Assert::same(20.0, $discount->percentage); | ||
Assert::same(1.20, $discount->ratio); | ||
Assert::same(0.20, $discount->smallRatio); | ||
Assert::same('20', (string) $discount); | ||
} | ||
|
||
} | ||
|
||
(new DiscountTest)->run(); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.