Add helper functions
Since Decimal is a final class, we cannot extend the class with custom methods. compareTo() gives us a comparison feature, but it's taking quite some time to get our minds wrapped around it every time.
It would be great with some helper functions: greaterThan($other) lessThan($other) equalTo($other) greaterThanOrEqualTo($other) lessThanOrEqualTo($other)
Would it be implausible to decorate the class for your use case?
class CompareDecimal {
private $decimal;
public function __construct(Decimal $decimal)
{
$this->decimal = $decimal;
}
public function greaterThan($other): bool
{
return $this->decimal->compareTo($other) === 1;
}
public function lessThan($other): bool
{
return $this->decimal->compareTo($other) === -1;
}
public function equalTo($other): bool
{
return $this->decimal->compareTo($other) === 0;
}
public function greaterThanOrEqualTo($other): bool
{
return $this->greaterThan($other) || $this->equalTo($other);
}
public function lessThanOrEqualTo($other): bool
{
return $this->lessThan($other) || $this->equalTo($other);
}
}
This is a great suggestion, I will add it to the API.
@Acen This will do the trick, but I believe it's smoother to just have it on the Decimal instance anyway, to avoid yet another complementing class. This class will do the trick for now, thanks, but when Decimal already has a compareTo() function (which includes the requested type of logic), I believe it's also logical to add the missing/complementing functions
@rtheunissen Which release will this be included in?