diff --git a/app/src/Service/Valor.php b/app/src/Service/Valor.php index cebfbd6..573d275 100644 --- a/app/src/Service/Valor.php +++ b/app/src/Service/Valor.php @@ -1,8 +1,14 @@ getDateTime($date); + if (abs((float) $value - (int) $value) > 0 or $force) { + return round($value * $this->ufService->get($date)); + } + return (int) $value; + } + public function toUF(string $value, null|string|DateTimeInterface $date = null, bool $force = false): float + { + $date = $this->getDateTime($date); + if (abs((float) $value - (int) $value) > 0 and !$force) { + return (float) $value; + } + return $value / $this->ufService->get($date); + } + + protected function getDateTime(null|string|DateTimeInterface $date): DateTimeInterface + { + if ($date === null) { + return new DateTimeImmutable(); + } + if (is_string($date)) { + try { + return new DateTimeImmutable($date); + } catch (DateMalformedStringException) { + return new DateTimeImmutable(); + } + } + return $date; + } }