HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
DayOfWeekField.php
Go to the documentation of this file.
1 <?php
2 
3 
20 {
24  public function isSatisfiedBy(DateTime $date, $value)
25  {
26  if ($value == '?') {
27  return true;
28  }
29 
30  // Convert text day of the week values to integers
31  $value = str_ireplace(
32  array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'),
33  range(0, 6),
34  $value
35  );
36 
37  $currentYear = $date->format('Y');
38  $currentMonth = $date->format('m');
39  $lastDayOfMonth = $date->format('t');
40 
41  // Find out if this is the last specific weekday of the month
42  if (strpos($value, 'L')) {
43  $weekday = str_replace('7', '0', substr($value, 0, strpos($value, 'L')));
44  $tdate = clone $date;
45  $tdate->setDate($currentYear, $currentMonth, $lastDayOfMonth);
46  while ($tdate->format('w') != $weekday) {
47  $tdate->setDate($currentYear, $currentMonth, --$lastDayOfMonth);
48  }
49 
50  return $date->format('j') == $lastDayOfMonth;
51  }
52 
53  // Handle # hash tokens
54  if (strpos($value, '#')) {
55  list($weekday, $nth) = explode('#', $value);
56  // Validate the hash fields
57  if ($weekday < 1 || $weekday > 5) {
58  throw new InvalidArgumentException("Weekday must be a value between 1 and 5. {$weekday} given");
59  }
60  if ($nth > 5) {
61  throw new InvalidArgumentException('There are never more than 5 of a given weekday in a month');
62  }
63  // The current weekday must match the targeted weekday to proceed
64  if ($date->format('N') != $weekday) {
65  return false;
66  }
67 
68  $tdate = clone $date;
69  $tdate->setDate($currentYear, $currentMonth, 1);
70  $dayCount = 0;
71  $currentDay = 1;
72  while ($currentDay < $lastDayOfMonth + 1) {
73  if ($tdate->format('N') == $weekday) {
74  if (++$dayCount >= $nth) {
75  break;
76  }
77  }
78  $tdate->setDate($currentYear, $currentMonth, ++$currentDay);
79  }
80 
81  return $date->format('j') == $currentDay;
82  }
83 
84  // Handle day of the week values
85  if (strpos($value, '-')) {
86  $parts = explode('-', $value);
87  if ($parts[0] == '7') {
88  $parts[0] = '0';
89  } elseif ($parts[1] == '0') {
90  $parts[1] = '7';
91  }
92  $value = implode('-', $parts);
93  }
94 
95  // Test to see which Sunday to use -- 0 == 7 == Sunday
96  $format = in_array(7, str_split($value)) ? 'N' : 'w';
97  $fieldValue = $date->format($format);
98 
99  return $this->isSatisfied($fieldValue, $value);
100  }
101 
105  public function increment(DateTime $date, $invert = false)
106  {
107  if ($invert) {
108  $date->sub(new DateInterval('P1D'));
109  $date->setTime(23, 59, 0);
110  } else {
111  $date->add(new DateInterval('P1D'));
112  $date->setTime(0, 0, 0);
113  }
114 
115  return $this;
116  }
117 
121  public function validate($value)
122  {
123  return (bool) preg_match('/[\*,\/\-0-9A-Z]+/', $value);
124  }
125 }