First commit

This commit is contained in:
David
2026-01-06 10:46:49 -05:00
parent c6b2e3e56f
commit 3fdcee57ad
88 changed files with 12306 additions and 0 deletions

View 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");
}
}
}

View File

@@ -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;
}
}

View 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;
}
}

View 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;
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View 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");
}
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace MMT\DDDSharedKernel\ValueObjects;
final class UID extends ValueObject
{
public function __construct(
public private(set) int $value
) { }
}

View File

@@ -0,0 +1,8 @@
<?php
namespace MMT\DDDSharedKernel\ValueObjects;
class ValueObject
{
}