Overview

Namespaces

  • Cron
  • None

Classes

  • AbstractField
  • CronExpression
  • DayOfMonthField
  • DayOfWeekField
  • FieldFactory
  • HoursField
  • MinutesField
  • MonthField
  • YearField

Interfaces

  • FieldInterface
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace Cron;
  4: 
  5: /**
  6:  * Abstract CRON expression field
  7:  */
  8: abstract class AbstractField implements FieldInterface
  9: {
 10:     /**
 11:      * Check to see if a field is satisfied by a value
 12:      *
 13:      * @param string $dateValue Date value to check
 14:      * @param string $value     Value to test
 15:      *
 16:      * @return bool
 17:      */
 18:     public function isSatisfied($dateValue, $value)
 19:     {
 20:         if ($this->isIncrementsOfRanges($value)) {
 21:             return $this->isInIncrementsOfRanges($dateValue, $value);
 22:         } elseif ($this->isRange($value)) {
 23:             return $this->isInRange($dateValue, $value);
 24:         }
 25: 
 26:         return $value == '*' || $dateValue == $value;
 27:     }
 28: 
 29:     /**
 30:      * Check if a value is a range
 31:      *
 32:      * @param string $value Value to test
 33:      *
 34:      * @return bool
 35:      */
 36:     public function isRange($value)
 37:     {
 38:         return strpos($value, '-') !== false;
 39:     }
 40: 
 41:     /**
 42:      * Check if a value is an increments of ranges
 43:      *
 44:      * @param string $value Value to test
 45:      *
 46:      * @return bool
 47:      */
 48:     public function isIncrementsOfRanges($value)
 49:     {
 50:         return strpos($value, '/') !== false;
 51:     }
 52: 
 53:     /**
 54:      * Test if a value is within a range
 55:      *
 56:      * @param string $dateValue Set date value
 57:      * @param string $value     Value to test
 58:      *
 59:      * @return bool
 60:      */
 61:     public function isInRange($dateValue, $value)
 62:     {
 63:         $parts = array_map('trim', explode('-', $value, 2));
 64: 
 65:         return $dateValue >= $parts[0] && $dateValue <= $parts[1];
 66:     }
 67: 
 68:     /**
 69:      * Test if a value is within an increments of ranges (offset[-to]/step size)
 70:      *
 71:      * @param string $dateValue Set date value
 72:      * @param string $value     Value to test
 73:      *
 74:      * @return bool
 75:      */
 76:     public function isInIncrementsOfRanges($dateValue, $value)
 77:     {
 78:         $parts = array_map('trim', explode('/', $value, 2));
 79:         $stepSize = isset($parts[1]) ? $parts[1] : 0;
 80:         if ($parts[0] == '*' || $parts[0] === '0') {
 81:             return (int) $dateValue % $stepSize == 0;
 82:         }
 83: 
 84:         $range = explode('-', $parts[0], 2);
 85:         $offset = $range[0];
 86:         $to = isset($range[1]) ? $range[1] : $dateValue;
 87:         // Ensure that the date value is within the range
 88:         if ($dateValue < $offset || $dateValue > $to) {
 89:             return false;
 90:         }
 91: 
 92:         for ($i = $offset; $i <= $to; $i+= $stepSize) {
 93:             if ($i == $dateValue) {
 94:                 return true;
 95:             }
 96:         }
 97: 
 98:         return false;
 99:     }
100: }
101: 
API documentation generated by ApiGen 2.8.0