First commit
This commit is contained in:
0
packages/mmt/ddd-shared-kernel/DomainException.php
Normal file
0
packages/mmt/ddd-shared-kernel/DomainException.php
Normal file
10
packages/mmt/ddd-shared-kernel/Entity.php
Normal file
10
packages/mmt/ddd-shared-kernel/Entity.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel;
|
||||
|
||||
use MMT\DDDSharedKernel\ValueObjects\UID;
|
||||
|
||||
abstract class Entity
|
||||
{
|
||||
abstract public UID $id {get; set;}
|
||||
}
|
||||
15
packages/mmt/ddd-shared-kernel/Enums/FlowRate.php
Normal file
15
packages/mmt/ddd-shared-kernel/Enums/FlowRate.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\Enums;
|
||||
|
||||
/**
|
||||
*
|
||||
* Indica como debe ser tratado un monto ya sea de comision, pago por ganancia u otros.
|
||||
*
|
||||
*/
|
||||
enum AmountType : int
|
||||
{
|
||||
case ABSOLUTE = 1;
|
||||
|
||||
case PERCENTAGE = 2;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\Exceptions;
|
||||
|
||||
class NotImplementedExcpetion extends \Exception
|
||||
{
|
||||
public function __construct(string $message = "", int $code = 0, ?\Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
11
packages/mmt/ddd-shared-kernel/Interfaces/Commission.php
Normal file
11
packages/mmt/ddd-shared-kernel/Interfaces/Commission.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\Interfaces;
|
||||
|
||||
use MMT\DDDSharedKernel\ValueObjects\Money;
|
||||
use MMT\DDDSharedKernel\ValueObjects\ValueObject;
|
||||
|
||||
abstract class Commission extends ValueObject
|
||||
{
|
||||
abstract public function applyTo(Money $money): Money;
|
||||
}
|
||||
19
packages/mmt/ddd-shared-kernel/ValueObjects/Email.php
Normal file
19
packages/mmt/ddd-shared-kernel/ValueObjects/Email.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\ValueObjects;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
final class Email extends ValueObject
|
||||
{
|
||||
public function __construct(
|
||||
public private(set) string $email
|
||||
)
|
||||
{
|
||||
$this->email = trim(mb_strtolower($email));
|
||||
|
||||
if (!filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
|
||||
throw new InvalidArgumentException("Invalid email format");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\ValueObjects;
|
||||
|
||||
use MMT\DDDSharedKernel\Interfaces\Commission;
|
||||
|
||||
class FixedCommission extends Commission
|
||||
{
|
||||
public function __construct(
|
||||
public private(set) Money $value
|
||||
)
|
||||
{
|
||||
if($value->isNegative()) {
|
||||
throw new \InvalidArgumentException('The amount of a commission cannot be negative');
|
||||
}
|
||||
}
|
||||
|
||||
public function applyTo(?Money $money = null): Money
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
73
packages/mmt/ddd-shared-kernel/ValueObjects/Money.php
Normal file
73
packages/mmt/ddd-shared-kernel/ValueObjects/Money.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\ValueObjects;
|
||||
|
||||
final class Money extends ValueObject
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
public private(set) int $amount,
|
||||
public private(set) string $currency
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function __toString() : string
|
||||
{
|
||||
$amount = $this->toFloat();
|
||||
|
||||
return $this->isPositive()
|
||||
? "{$this->currency} {$amount}"
|
||||
: "-{$this->currency} {-$amount}";
|
||||
}
|
||||
|
||||
public function toFloat() : float
|
||||
{
|
||||
return round($this->amount /100, 2);
|
||||
}
|
||||
|
||||
public function formatted() : string
|
||||
{
|
||||
$amount = $this->toFloat();
|
||||
|
||||
return $this->isPositive()
|
||||
? "\${$amount}"
|
||||
: "-\${-$amount}";
|
||||
}
|
||||
|
||||
public function isPositive() : bool
|
||||
{
|
||||
return $this->amount >= 0;
|
||||
}
|
||||
|
||||
public function isNegative() : bool
|
||||
{
|
||||
return $this->amount < 0;
|
||||
}
|
||||
|
||||
public function isZero() : bool
|
||||
{
|
||||
return $this->amount === 0;
|
||||
}
|
||||
|
||||
public function greaterThan(Money $other) : bool
|
||||
{
|
||||
return $this->amount > $other->amount;
|
||||
}
|
||||
|
||||
public function greaterThanOrEqual(Money $other) : bool
|
||||
{
|
||||
return $this->amount >= $other->amount;
|
||||
}
|
||||
|
||||
public function lessThan(Money $other) : bool
|
||||
{
|
||||
return $this->amount < $other->amount;
|
||||
}
|
||||
|
||||
public function lessThanOrEqual(Money $other) : bool
|
||||
{
|
||||
return $this->amount <= $other->amount;
|
||||
}
|
||||
}
|
||||
19
packages/mmt/ddd-shared-kernel/ValueObjects/Percentage.php
Normal file
19
packages/mmt/ddd-shared-kernel/ValueObjects/Percentage.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\ValueObjects;
|
||||
|
||||
final class Percentage extends ValueObject
|
||||
{
|
||||
public function __construct(
|
||||
public private(set) int $value
|
||||
) {
|
||||
if ($value < 0 || $value > 100) {
|
||||
throw new \InvalidArgumentException("Percentage must be 0-100");
|
||||
}
|
||||
}
|
||||
|
||||
public function asFloat(): float
|
||||
{
|
||||
return $this->value / 100;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\ValueObjects;
|
||||
|
||||
use MMT\DDDSharedKernel\Interfaces\Commission;
|
||||
|
||||
final class PercentageCommission extends Commission
|
||||
{
|
||||
public function __construct(
|
||||
public private(set) int $value
|
||||
)
|
||||
{
|
||||
if($value < 0) {
|
||||
throw new \InvalidArgumentException('The amount of a commission cannot be negative');
|
||||
}
|
||||
|
||||
if($value > 100) {
|
||||
throw new \InvalidArgumentException('The amount commission cannot be exceed 100 when the commission is based on a percentage');
|
||||
}
|
||||
}
|
||||
|
||||
public function applyTo(Money $money): Money
|
||||
{
|
||||
$finalCommissionInCents = $money->amount * $this->value / 100;
|
||||
return new Money($finalCommissionInCents, $money->currency);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\ValueObjects;
|
||||
|
||||
class PositiveMoney extends ValueObject
|
||||
{
|
||||
public function __construct(
|
||||
private Money $money
|
||||
)
|
||||
{
|
||||
if($this->money->isNegative()) {
|
||||
throw new \InvalidArgumentException("The value cannot be negative");
|
||||
}
|
||||
}
|
||||
|
||||
public function greaterThan(PositiveMoney $positiveMoney): bool
|
||||
{
|
||||
return $this->money->greaterThan($positiveMoney->money);
|
||||
}
|
||||
|
||||
public function greaterThanOrEqual(PositiveMoney $positiveMoney): bool
|
||||
{
|
||||
return $this->money->greaterThanOrEqual($positiveMoney->money);
|
||||
}
|
||||
}
|
||||
15
packages/mmt/ddd-shared-kernel/ValueObjects/Price.php
Normal file
15
packages/mmt/ddd-shared-kernel/ValueObjects/Price.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\ValueObjects;
|
||||
|
||||
class Price extends ValueObject
|
||||
{
|
||||
public function __construct(
|
||||
private Money $money
|
||||
)
|
||||
{
|
||||
if($this->money->isNegative()) {
|
||||
throw new \InvalidArgumentException("The price cannot be negative");
|
||||
}
|
||||
}
|
||||
}
|
||||
10
packages/mmt/ddd-shared-kernel/ValueObjects/UID.php
Normal file
10
packages/mmt/ddd-shared-kernel/ValueObjects/UID.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\ValueObjects;
|
||||
|
||||
final class UID extends ValueObject
|
||||
{
|
||||
public function __construct(
|
||||
public private(set) int $value
|
||||
) { }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace MMT\DDDSharedKernel\ValueObjects;
|
||||
|
||||
class ValueObject
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user