allow negative duration via internal API (#3573)

This commit is contained in:
Kevin Papst
2022-10-10 17:47:11 +02:00
committed by GitHub
parent 1959d4d8df
commit 0cefcf0e16
9 changed files with 39 additions and 46 deletions

View File

@@ -171,7 +171,6 @@ class Timesheet implements EntityWithMetaFields, ExportItemInterface
* @Serializer\Groups({"Default"})
*
* @ORM\Column(name="duration", type="integer", nullable=true)
* @Assert\GreaterThanOrEqual(0)
*/
private $duration = 0;
/**

View File

@@ -117,10 +117,6 @@ class Duration
throw new \InvalidArgumentException(sprintf('Unsupported duration format "%s"', $mode));
}
if ($seconds < 0) {
return 0;
}
return $seconds;
}
@@ -155,13 +151,15 @@ class Duration
);
}
$i = 0;
foreach ($parts as $part) {
if (\strlen($part) === 0) {
throw new \InvalidArgumentException(
sprintf('Colon format cannot parse "%s"', $duration)
);
}
if (((int) $part) < 0) {
// the entire time could be negative
if ($i++ > 0 && ((int) $part) < 0) {
throw new \InvalidArgumentException(
sprintf('Negative input is not allowed in "%s"', $duration)
);
@@ -175,7 +173,11 @@ class Duration
}
$seconds += (int) $parts[1] * 60;
$seconds += (int) $parts[0] * 3600;
$seconds += abs((int) $parts[0] * 3600);
if ($duration[0] === '-') {
$seconds = $seconds * -1;
}
return $seconds;
}

View File

@@ -51,9 +51,6 @@ final class LocaleHelper
public function durationDecimal(int $seconds)
{
$value = round($seconds / 3600, 2);
if ($value <= 0) {
$value = 0;
}
return $this->getDurationFormatter()->format((float) $value);
}

View File

@@ -21,11 +21,12 @@ class Duration extends Regex
{
$patterns = [
// decimal times (can be separated by comma or dot, depending on the locale)
'[0-9]{1,}',
'[0-9]{1,}[,.]{1}[0-9]{1,}',
// negative times -? are allowed, because plugins could allow negative times
'-?[0-9]{1,}',
'-?[0-9]{1,}[,.]{1}[0-9]{1,}',
// ASP.NET style time spans - https://momentjs.com/docs/#/durations/
'[0-9]{1,}:[0-9]{1,}:[0-9]{1,}',
'[0-9]{1,}:[0-9]{1,}',
'-?[0-9]{1,}:[0-9]{1,}:[0-9]{1,}',
'-?[0-9]{1,}:[0-9]{1,}',
// https://en.wikipedia.org/wiki/ISO_8601#Time_intervals
'[0-9]{1,}[hHmMsS]{1}',
'[0-9]{1,}[hH]{1}[0-9]{1,}[mM]{1}',

View File

@@ -132,9 +132,6 @@ final class TimesheetBasicValidator extends ConstraintValidator
->addViolation();
}
$pathStart = 'begin';
$pathEnd = 'end';
$projectBegin = $project->getStart();
$projectEnd = $project->getEnd();
@@ -142,10 +139,13 @@ final class TimesheetBasicValidator extends ConstraintValidator
return;
}
$pathStart = 'begin';
$pathEnd = 'end';
$timesheetStart = $timesheet->getBegin();
$timesheetEnd = $timesheet->getEnd();
if (null !== $timesheetStart && $pathStart !== null) {
if (null !== $timesheetStart) {
if (null !== $projectBegin && $timesheetStart->getTimestamp() < $projectBegin->getTimestamp()) {
$context->buildViolation('The project has not started at that time.')
->atPath($pathStart)
@@ -161,7 +161,7 @@ final class TimesheetBasicValidator extends ConstraintValidator
}
}
if (null !== $timesheetEnd && $pathEnd !== null) {
if (null !== $timesheetEnd) {
if (null !== $projectEnd && $timesheetEnd->getTimestamp() > $projectEnd->getTimestamp()) {
$context->buildViolation('The project is finished at that time.')
->atPath($pathEnd)