HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
DayOfMonthField.php
Go to the documentation of this file.
1 <?php
2 
22 {
32  private static function getNearestWeekday($currentYear, $currentMonth, $targetDay)
33  {
34  $tday = str_pad($targetDay, 2, '0', STR_PAD_LEFT);
35  $target = DateTime::createFromFormat('Y-m-d', "$currentYear-$currentMonth-$tday");
36  $currentWeekday = (int) $target->format('N');
37 
38  if ($currentWeekday < 6) {
39  return $target;
40  }
41 
42  $lastDayOfMonth = $target->format('t');
43 
44  foreach (array(-1, 1, -2, 2) as $i) {
45  $adjusted = $targetDay + $i;
46  if ($adjusted > 0 && $adjusted <= $lastDayOfMonth) {
47  $target->setDate($currentYear, $currentMonth, $adjusted);
48  if ($target->format('N') < 6 && $target->format('m') == $currentMonth) {
49  return $target;
50  }
51  }
52  }
53  }
54 
58  public function isSatisfiedBy(DateTime $date, $value)
59  {
60  // ? states that the field value is to be skipped
61  if ($value == '?') {
62  return true;
63  }
64 
65  $fieldValue = $date->format('d');
66 
67  // Check to see if this is the last day of the month
68  if ($value == 'L') {
69  return $fieldValue == $date->format('t');
70  }
71 
72  // Check to see if this is the nearest weekday to a particular value
73  if (strpos($value, 'W')) {
74  // Parse the target day
75  $targetDay = substr($value, 0, strpos($value, 'W'));
76  // Find out if the current day is the nearest day of the week
77  return $date->format('j') == self::getNearestWeekday($date->format('Y'), $date->format('m'), $targetDay)->format('j');
78  }
79 
80  return $this->isSatisfied($date->format('d'), $value);
81  }
82 
86  public function increment(DateTime $date, $invert = false)
87  {
88  if ($invert) {
89  $date->sub(new DateInterval('P1D'));
90  $date->setTime(23, 59, 0);
91  } else {
92  $date->add(new DateInterval('P1D'));
93  $date->setTime(0, 0, 0);
94  }
95 
96  return $this;
97  }
98 
102  public function validate($value)
103  {
104  return (bool) preg_match('/[\*,\/\-\?LW0-9A-Za-z]+/', $value);
105  }
106 }