AbstractField.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace Cron;
  3. /**
  4. * Abstract CRON expression field
  5. */
  6. abstract class AbstractField implements FieldInterface
  7. {
  8. /**
  9. * Full range of values that are allowed for this field type
  10. * @var array
  11. */
  12. protected $fullRange = [];
  13. /**
  14. * Literal values we need to convert to integers
  15. * @var array
  16. */
  17. protected $literals = [];
  18. /**
  19. * Start value of the full range
  20. * @var integer
  21. */
  22. protected $rangeStart;
  23. /**
  24. * End value of the full range
  25. * @var integer
  26. */
  27. protected $rangeEnd;
  28. public function __construct()
  29. {
  30. $this->fullRange = range($this->rangeStart, $this->rangeEnd);
  31. }
  32. /**
  33. * Check to see if a field is satisfied by a value
  34. *
  35. * @param string $dateValue Date value to check
  36. * @param string $value Value to test
  37. *
  38. * @return bool
  39. */
  40. public function isSatisfied($dateValue, $value)
  41. {
  42. if ($this->isIncrementsOfRanges($value)) {
  43. return $this->isInIncrementsOfRanges($dateValue, $value);
  44. } elseif ($this->isRange($value)) {
  45. return $this->isInRange($dateValue, $value);
  46. }
  47. return $value == '*' || $dateValue == $value;
  48. }
  49. /**
  50. * Check if a value is a range
  51. *
  52. * @param string $value Value to test
  53. *
  54. * @return bool
  55. */
  56. public function isRange($value)
  57. {
  58. return strpos($value, '-') !== false;
  59. }
  60. /**
  61. * Check if a value is an increments of ranges
  62. *
  63. * @param string $value Value to test
  64. *
  65. * @return bool
  66. */
  67. public function isIncrementsOfRanges($value)
  68. {
  69. return strpos($value, '/') !== false;
  70. }
  71. /**
  72. * Test if a value is within a range
  73. *
  74. * @param string $dateValue Set date value
  75. * @param string $value Value to test
  76. *
  77. * @return bool
  78. */
  79. public function isInRange($dateValue, $value)
  80. {
  81. $parts = array_map('trim', explode('-', $value, 2));
  82. return $dateValue >= $parts[0] && $dateValue <= $parts[1];
  83. }
  84. /**
  85. * Test if a value is within an increments of ranges (offset[-to]/step size)
  86. *
  87. * @param string $dateValue Set date value
  88. * @param string $value Value to test
  89. *
  90. * @return bool
  91. */
  92. public function isInIncrementsOfRanges($dateValue, $value)
  93. {
  94. $chunks = array_map('trim', explode('/', $value, 2));
  95. $range = $chunks[0];
  96. $step = isset($chunks[1]) ? $chunks[1] : 0;
  97. // No step or 0 steps aren't cool
  98. if (is_null($step) || '0' === $step || 0 === $step) {
  99. return false;
  100. }
  101. // Expand the * to a full range
  102. if ('*' == $range) {
  103. $range = $this->rangeStart . '-' . $this->rangeEnd;
  104. }
  105. // Generate the requested small range
  106. $rangeChunks = explode('-', $range, 2);
  107. $rangeStart = $rangeChunks[0];
  108. $rangeEnd = isset($rangeChunks[1]) ? $rangeChunks[1] : $rangeStart;
  109. if ($rangeStart < $this->rangeStart || $rangeStart > $this->rangeEnd || $rangeStart > $rangeEnd) {
  110. throw new \OutOfRangeException('Invalid range start requested');
  111. }
  112. if ($rangeEnd < $this->rangeStart || $rangeEnd > $this->rangeEnd || $rangeEnd < $rangeStart) {
  113. throw new \OutOfRangeException('Invalid range end requested');
  114. }
  115. if ($step > ($rangeEnd - $rangeStart) + 1) {
  116. throw new \OutOfRangeException('Step cannot be greater than total range');
  117. }
  118. $thisRange = range($rangeStart, $rangeEnd, $step);
  119. return in_array($dateValue, $thisRange);
  120. }
  121. /**
  122. * Returns a range of values for the given cron expression
  123. *
  124. * @param string $expression The expression to evaluate
  125. * @param int $max Maximum offset for range
  126. *
  127. * @return array
  128. */
  129. public function getRangeForExpression($expression, $max)
  130. {
  131. $values = array();
  132. if ($this->isRange($expression) || $this->isIncrementsOfRanges($expression)) {
  133. if (!$this->isIncrementsOfRanges($expression)) {
  134. list ($offset, $to) = explode('-', $expression);
  135. $stepSize = 1;
  136. }
  137. else {
  138. $range = array_map('trim', explode('/', $expression, 2));
  139. $stepSize = isset($range[1]) ? $range[1] : 0;
  140. $range = $range[0];
  141. $range = explode('-', $range, 2);
  142. $offset = $range[0];
  143. $to = isset($range[1]) ? $range[1] : $max;
  144. }
  145. $offset = $offset == '*' ? 0 : $offset;
  146. for ($i = $offset; $i <= $to; $i += $stepSize) {
  147. $values[] = $i;
  148. }
  149. sort($values);
  150. }
  151. else {
  152. $values = array($expression);
  153. }
  154. return $values;
  155. }
  156. protected function convertLiterals($value)
  157. {
  158. if (count($this->literals)) {
  159. $key = array_search($value, $this->literals);
  160. if ($key !== false) {
  161. return $key;
  162. }
  163. }
  164. return $value;
  165. }
  166. /**
  167. * Checks to see if a value is valid for the field
  168. *
  169. * @param string $value
  170. * @return bool
  171. */
  172. public function validate($value)
  173. {
  174. $value = $this->convertLiterals($value);
  175. // All fields allow * as a valid value
  176. if ('*' === $value) {
  177. return true;
  178. }
  179. // You cannot have a range and a list at the same time
  180. if (strpos($value, ',') !== false && strpos($value, '-') !== false) {
  181. return false;
  182. }
  183. if (strpos($value, '/') !== false) {
  184. list($range, $step) = explode('/', $value);
  185. return $this->validate($range) && filter_var($step, FILTER_VALIDATE_INT);
  186. }
  187. if (strpos($value, '-') !== false) {
  188. if (substr_count($value, '-') > 1) {
  189. return false;
  190. }
  191. $chunks = explode('-', $value);
  192. $chunks[0] = $this->convertLiterals($chunks[0]);
  193. $chunks[1] = $this->convertLiterals($chunks[1]);
  194. if ('*' == $chunks[0] || '*' == $chunks[1]) {
  195. return false;
  196. }
  197. return $this->validate($chunks[0]) && $this->validate($chunks[1]);
  198. }
  199. // Validate each chunk of a list individually
  200. if (strpos($value, ',') !== false) {
  201. foreach (explode(',', $value) as $listItem) {
  202. if (!$this->validate($listItem)) {
  203. return false;
  204. }
  205. }
  206. return true;
  207. }
  208. // We should have a numeric by now, so coerce this into an integer
  209. if (filter_var($value, FILTER_VALIDATE_INT) !== false) {
  210. $value = (int) $value;
  211. }
  212. return in_array($value, $this->fullRange, true);
  213. }
  214. }