TimeUnitsAliassesHelper.php 839 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\helpers;
  3. use Exception;
  4. class TimeUnitsAliassesHelper
  5. {
  6. private static $units = [
  7. 'M' => 60,
  8. 'H' => 3600,
  9. 'D' => 86400,
  10. 'W' => 604800
  11. ];
  12. /**
  13. * @param $val
  14. * @return float|int
  15. * @throws Exception if can't parse $val to seconds
  16. */
  17. public static function convertToSeconds( $val )
  18. {
  19. if (preg_match('/^\d+$/', $val))
  20. {
  21. return (int)$val;
  22. }
  23. preg_match('/(\d+)([MHDW])/i', $val, $matches);
  24. if (!$matches)
  25. {
  26. throw new Exception('Wrong TTL Syntax for: '.$val);
  27. }
  28. $secondsPerSingeUnit = static::$units[strtoupper($matches[2])];
  29. $amount = (int)$matches[1];
  30. return $secondsPerSingeUnit * $amount;
  31. }
  32. }