Next major version 2 with PHP 8.1, Symfony 6, Tabler UI, 2FA ... (#2902)
This commit is contained in:
@@ -14,32 +14,16 @@ use App\Validator\Constraints\Duration as DurationConstraint;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
|
||||
class DurationStringToSecondsTransformer implements DataTransformerInterface
|
||||
final class DurationStringToSecondsTransformer implements DataTransformerInterface
|
||||
{
|
||||
/**
|
||||
* @var Duration
|
||||
*/
|
||||
protected $formatter;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $pattern;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->formatter = new Duration();
|
||||
$constraint = new DurationConstraint();
|
||||
$this->pattern = $constraint->pattern;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $intToFormat
|
||||
* @return string|null
|
||||
*/
|
||||
public function transform($intToFormat)
|
||||
public function transform(mixed $intToFormat): ?string
|
||||
{
|
||||
try {
|
||||
return $this->formatter->format($intToFormat);
|
||||
return (new Duration())->format($intToFormat);
|
||||
} catch (\Exception | \TypeError $e) {
|
||||
throw new TransformationFailedException($e->getMessage());
|
||||
}
|
||||
@@ -49,7 +33,7 @@ class DurationStringToSecondsTransformer implements DataTransformerInterface
|
||||
* @param string|null $formatToInt
|
||||
* @return int|null
|
||||
*/
|
||||
public function reverseTransform($formatToInt)
|
||||
public function reverseTransform(mixed $formatToInt): ?int
|
||||
{
|
||||
if (null === $formatToInt) {
|
||||
return null;
|
||||
@@ -60,12 +44,12 @@ class DurationStringToSecondsTransformer implements DataTransformerInterface
|
||||
}
|
||||
|
||||
// we need this one here, because the data transformer is executed BEFORE the constraint is called
|
||||
if (!preg_match($this->pattern, $formatToInt)) {
|
||||
if (!preg_match((new DurationConstraint())->pattern, $formatToInt)) {
|
||||
throw new TransformationFailedException('Invalid duration format given');
|
||||
}
|
||||
|
||||
try {
|
||||
$seconds = $this->formatter->parseDurationString($formatToInt);
|
||||
$seconds = (new Duration())->parseDurationString($formatToInt);
|
||||
|
||||
// DateTime throws if a duration with too many seconds is passed and an amount of so
|
||||
// many seconds is likely not required in a time-tracking application ;-)
|
||||
|
||||
Reference in New Issue
Block a user