ParameterNormalizer.php 898 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Adapter;
  11. /**
  12. * @author Lars Strojny <lars@strojny.net>
  13. */
  14. final class ParameterNormalizer
  15. {
  16. public static function normalizeDuration(string $duration): int
  17. {
  18. if (is_numeric($duration)) {
  19. return $duration;
  20. }
  21. if (false !== $time = strtotime($duration, 0)) {
  22. return $time;
  23. }
  24. try {
  25. return \DateTime::createFromFormat('U', 0)->add(new \DateInterval($duration))->getTimestamp();
  26. } catch (\Exception $e) {
  27. throw new \InvalidArgumentException(sprintf('Cannot parse date interval "%s".', $duration), 0, $e);
  28. }
  29. }
  30. }