73 lines
1.4 KiB
PHP
73 lines
1.4 KiB
PHP
<?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;
|
|
}
|
|
} |