First commit
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user