format($intToFormat); } catch (\Exception | \TypeError $e) { throw new TransformationFailedException($e->getMessage()); } } /** * @param string|null $formatToInt * @return int|null */ public function reverseTransform(mixed $formatToInt): ?int { if (null === $formatToInt) { return null; } if (empty($formatToInt)) { return 0; } // we need this one here, because the data transformer is executed BEFORE the constraint is called if (!preg_match((new DurationConstraint())->pattern, $formatToInt)) { throw new TransformationFailedException('Invalid duration format given'); } try { $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 ;-) if ($seconds > 315360000000000) { throw new TransformationFailedException('Maximum duration exceeded.'); } return $seconds; } catch (\Exception $e) { throw new TransformationFailedException($e->getMessage()); } } }