Release 2.36.0 (#5514)
This commit is contained in:
26
src/Validator/Constraints/TimesheetNegativeDuration.php
Normal file
26
src/Validator/Constraints/TimesheetNegativeDuration.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
final class TimesheetNegativeDuration extends TimesheetConstraint
|
||||
{
|
||||
public const NEGATIVE_DURATION_ERROR = 'kimai-timesheet-negative-duration-01';
|
||||
|
||||
protected const ERROR_NAMES = [
|
||||
self::NEGATIVE_DURATION_ERROR => 'Duration cannot be negative.',
|
||||
];
|
||||
|
||||
public string $message = 'Duration cannot be negative.';
|
||||
|
||||
public function getTargets(): string
|
||||
{
|
||||
return self::CLASS_CONSTRAINT;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use App\Entity\Timesheet as TimesheetEntity;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
final class TimesheetNegativeDurationValidator extends ConstraintValidator
|
||||
{
|
||||
public function validate(mixed $value, Constraint $constraint): void
|
||||
{
|
||||
if (!($constraint instanceof TimesheetNegativeDuration)) {
|
||||
throw new UnexpectedTypeException($constraint, TimesheetNegativeDuration::class);
|
||||
}
|
||||
|
||||
if (!\is_object($value) || !($value instanceof TimesheetEntity)) {
|
||||
throw new UnexpectedTypeException($value, TimesheetEntity::class);
|
||||
}
|
||||
|
||||
if ($value->isRunning()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$duration = $value->getCalculatedDuration();
|
||||
|
||||
if ($duration !== null && $duration < 0) {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->atPath('duration')
|
||||
->setTranslationDomain('validators')
|
||||
->setCode(TimesheetNegativeDuration::NEGATIVE_DURATION_ERROR)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ final class TimesheetZeroDurationValidator extends ConstraintValidator
|
||||
$duration = $value->getCalculatedDuration();
|
||||
}
|
||||
|
||||
if ($duration <= 0) {
|
||||
if ($duration === 0) {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->atPath('duration')
|
||||
->setTranslationDomain('validators')
|
||||
|
||||
@@ -14,7 +14,7 @@ final class ValidationException extends \RuntimeException
|
||||
public function __construct(string $message = null)
|
||||
{
|
||||
if ($message === null) {
|
||||
$message = 'Validation failed';
|
||||
$message = 'Validation Failed';
|
||||
}
|
||||
parent::__construct($message, 400);
|
||||
}
|
||||
|
||||
@@ -13,10 +13,10 @@ use Symfony\Component\Validator\ConstraintViolationListInterface;
|
||||
|
||||
final class ValidationFailedException extends \RuntimeException
|
||||
{
|
||||
public function __construct(private ConstraintViolationListInterface $violations, ?string $message = null)
|
||||
public function __construct(private readonly ConstraintViolationListInterface $violations, ?string $message = null)
|
||||
{
|
||||
if ($message === null) {
|
||||
$message = 'Validation failed';
|
||||
$message = 'Validation Failed';
|
||||
}
|
||||
parent::__construct($message, 400);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user