added hourly and money budgets to activity, project and customer (#843)
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
namespace App\Utils;
|
||||
|
||||
/**
|
||||
* A simple class to help with timesheet record durations.
|
||||
* Convert duration strings into seconds.
|
||||
*/
|
||||
class Duration
|
||||
{
|
||||
@@ -50,6 +50,8 @@ class Duration
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the seconds, which were given as $duration string.
|
||||
*
|
||||
* @param string $duration
|
||||
* @return int
|
||||
*/
|
||||
@@ -67,6 +69,8 @@ class Duration
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the seconds, which were given as $mode formatted $duration string.
|
||||
*
|
||||
* @param string $duration
|
||||
* @param string $mode
|
||||
* @return int
|
||||
@@ -78,31 +82,13 @@ class Duration
|
||||
return 0;
|
||||
}
|
||||
|
||||
$seconds = 0;
|
||||
|
||||
switch ($mode) {
|
||||
case self::FORMAT_COLON:
|
||||
$parts = explode(':', $duration);
|
||||
if (count($parts) < 2) {
|
||||
throw new \InvalidArgumentException('Colon format cannot parse: ' . $duration);
|
||||
}
|
||||
$seconds = 0;
|
||||
if (3 == count($parts)) {
|
||||
$seconds += (int) array_pop($parts);
|
||||
}
|
||||
$seconds += (int) $parts[1] * 60;
|
||||
$seconds += (int) $parts[0] * 3600;
|
||||
$seconds = $this->parseColonFormat($duration);
|
||||
break;
|
||||
|
||||
case self::FORMAT_NATURAL:
|
||||
try {
|
||||
$interval = new \DateInterval('PT' . strtoupper($duration));
|
||||
$reference = new \DateTimeImmutable();
|
||||
$endTime = $reference->add($interval);
|
||||
$seconds = $endTime->getTimestamp() - $reference->getTimestamp();
|
||||
} catch (\Exception $e) {
|
||||
throw new \InvalidArgumentException('Invalid input for natural format: ' . $duration);
|
||||
}
|
||||
$seconds = $this->parseNaturalFormat($duration);
|
||||
break;
|
||||
|
||||
case self::FORMAT_SECONDS:
|
||||
@@ -110,7 +96,7 @@ class Duration
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \InvalidArgumentException('Invalid duration format: ' . $mode);
|
||||
throw new \InvalidArgumentException(sprintf('Unsupported duration format "%s"', $mode));
|
||||
}
|
||||
|
||||
if ($seconds < 0) {
|
||||
@@ -119,4 +105,51 @@ class Duration
|
||||
|
||||
return $seconds;
|
||||
}
|
||||
|
||||
protected function parseNaturalFormat(string $duration): int
|
||||
{
|
||||
try {
|
||||
$interval = new \DateInterval('PT' . strtoupper($duration));
|
||||
$reference = new \DateTimeImmutable();
|
||||
$endTime = $reference->add($interval);
|
||||
|
||||
return $endTime->getTimestamp() - $reference->getTimestamp();
|
||||
} catch (\Exception $e) {
|
||||
throw new \InvalidArgumentException('Invalid input for natural format: ' . $duration);
|
||||
}
|
||||
}
|
||||
|
||||
protected function parseColonFormat(string $duration): int
|
||||
{
|
||||
$parts = explode(':', $duration);
|
||||
if (count($parts) < 2 || count($parts) > 3) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('Invalid colon format given in "%s"', $duration)
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($parts as $part) {
|
||||
if (strlen($part) === 0) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('Colon format cannot parse "%s"', $duration)
|
||||
);
|
||||
}
|
||||
if (((int) $part) < 0) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('Negative input is not allowed in "%s"', $duration)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$seconds = 0;
|
||||
|
||||
if (3 == count($parts)) {
|
||||
$seconds += (int) array_pop($parts);
|
||||
}
|
||||
|
||||
$seconds += (int) $parts[1] * 60;
|
||||
$seconds += (int) $parts[0] * 3600;
|
||||
|
||||
return $seconds;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user