Skip to content

Commit

Permalink
feat(Percentage)!: Tax renamed Vat, Percent renamed Discount
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Dec 12, 2023
1 parent cc703de commit 55c1f2f
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 186 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,24 @@ $formats->get('decimal')->format(1000.1235); // 1 000,124
$formats->get('currency')->format(1000.1235); // 1 000,12 Kč
```

## Tax
## Vat

```php
use h4kuna\Format\Number;

$tax = new Number\Tax(20);
echo $tax->add(100); // 120
echo $tax->deduct(120); // 100.0
$tax = new Number\Vat(20);
echo $tax->with(100); // 120
echo $tax->without(120); // 100.0
echo $tax->diff(120); // 20.0
```

## Percent
## Discount

```php
use h4kuna\Format\Number;

$percent = new Number\Percent(20);
echo $percent->add(100); // 120.0
$percent = new Number\Discount(20);
echo $percent->with(100); // 120.0
echo $percent->deduct(120); // 96.0
echo $percent->diff(120); // 24.0
```
Expand Down
4 changes: 2 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ back compatibility with v5
use h4kuna;
class_alias(h4kuna\Format\Number\Formats::class, 'h4kuna\Number\Utils\Formats');
class_alias(h4kuna\Format\Number\NumberFormat::class, 'h4kuna\Number\Format');
class_alias(h4kuna\Format\Number\Percent::class, 'h4kuna\Number\Percent');
class_alias(h4kuna\Format\Number\Discount::class, 'h4kuna\Number\Percent');
class_alias(h4kuna\Format\Number\Round::class, 'h4kuna\Number\Utils\Round');
class_alias(h4kuna\Format\Number\Tax::class, 'h4kuna\Number\Tax');
class_alias(h4kuna\Format\Number\Vat::class, 'h4kuna\Number\Tax');
class_alias(h4kuna\Format\Number\UnitValue::class, 'h4kuna\Number\Utils\UnitValue');

// parameters
Expand Down
30 changes: 30 additions & 0 deletions src/Number/Discount.php
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);
}

}
58 changes: 0 additions & 58 deletions src/Number/Percent.php

This file was deleted.

35 changes: 35 additions & 0 deletions src/Number/Percentage.php
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;
}

}
48 changes: 0 additions & 48 deletions src/Number/Tax.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/Number/Vat.php
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);
}

}
81 changes: 81 additions & 0 deletions src/Utils/Percentage.php
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);
}
}
32 changes: 32 additions & 0 deletions tests/src/Number/DiscountTest.php
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();
40 changes: 0 additions & 40 deletions tests/src/Number/PercentTest.php

This file was deleted.

Loading

0 comments on commit 55c1f2f

Please sign in to comment.