ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Rule.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
8 class Rule
9 {
10  const AUTOFILTER_RULETYPE_FILTER = 'filter';
11  const AUTOFILTER_RULETYPE_DATEGROUP = 'dateGroupItem';
12  const AUTOFILTER_RULETYPE_CUSTOMFILTER = 'customFilter';
13  const AUTOFILTER_RULETYPE_DYNAMICFILTER = 'dynamicFilter';
14  const AUTOFILTER_RULETYPE_TOPTENFILTER = 'top10Filter';
15 
16  private static $ruleTypes = [
17  // Currently we're not handling
18  // colorFilter
19  // extLst
20  // iconFilter
21  self::AUTOFILTER_RULETYPE_FILTER,
22  self::AUTOFILTER_RULETYPE_DATEGROUP,
23  self::AUTOFILTER_RULETYPE_CUSTOMFILTER,
24  self::AUTOFILTER_RULETYPE_DYNAMICFILTER,
25  self::AUTOFILTER_RULETYPE_TOPTENFILTER,
26  ];
27 
34 
35  private static $dateTimeGroups = [
36  self::AUTOFILTER_RULETYPE_DATEGROUP_YEAR,
37  self::AUTOFILTER_RULETYPE_DATEGROUP_MONTH,
38  self::AUTOFILTER_RULETYPE_DATEGROUP_DAY,
39  self::AUTOFILTER_RULETYPE_DATEGROUP_HOUR,
40  self::AUTOFILTER_RULETYPE_DATEGROUP_MINUTE,
41  self::AUTOFILTER_RULETYPE_DATEGROUP_SECOND,
42  ];
43 
61  const AUTOFILTER_RULETYPE_DYNAMIC_JANUARY = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_1;
63  const AUTOFILTER_RULETYPE_DYNAMIC_FEBRUARY = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_2;
65  const AUTOFILTER_RULETYPE_DYNAMIC_MARCH = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_3;
67  const AUTOFILTER_RULETYPE_DYNAMIC_APRIL = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_4;
69  const AUTOFILTER_RULETYPE_DYNAMIC_MAY = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_5;
71  const AUTOFILTER_RULETYPE_DYNAMIC_JUNE = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_6;
73  const AUTOFILTER_RULETYPE_DYNAMIC_JULY = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_7;
75  const AUTOFILTER_RULETYPE_DYNAMIC_AUGUST = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_8;
77  const AUTOFILTER_RULETYPE_DYNAMIC_SEPTEMBER = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_9;
79  const AUTOFILTER_RULETYPE_DYNAMIC_OCTOBER = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_10;
81  const AUTOFILTER_RULETYPE_DYNAMIC_NOVEMBER = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_11;
83  const AUTOFILTER_RULETYPE_DYNAMIC_DECEMBER = self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_12;
90 
91  private static $dynamicTypes = [
92  self::AUTOFILTER_RULETYPE_DYNAMIC_YESTERDAY,
93  self::AUTOFILTER_RULETYPE_DYNAMIC_TODAY,
94  self::AUTOFILTER_RULETYPE_DYNAMIC_TOMORROW,
95  self::AUTOFILTER_RULETYPE_DYNAMIC_YEARTODATE,
96  self::AUTOFILTER_RULETYPE_DYNAMIC_THISYEAR,
97  self::AUTOFILTER_RULETYPE_DYNAMIC_THISQUARTER,
98  self::AUTOFILTER_RULETYPE_DYNAMIC_THISMONTH,
99  self::AUTOFILTER_RULETYPE_DYNAMIC_THISWEEK,
100  self::AUTOFILTER_RULETYPE_DYNAMIC_LASTYEAR,
101  self::AUTOFILTER_RULETYPE_DYNAMIC_LASTQUARTER,
102  self::AUTOFILTER_RULETYPE_DYNAMIC_LASTMONTH,
103  self::AUTOFILTER_RULETYPE_DYNAMIC_LASTWEEK,
104  self::AUTOFILTER_RULETYPE_DYNAMIC_NEXTYEAR,
105  self::AUTOFILTER_RULETYPE_DYNAMIC_NEXTQUARTER,
106  self::AUTOFILTER_RULETYPE_DYNAMIC_NEXTMONTH,
107  self::AUTOFILTER_RULETYPE_DYNAMIC_NEXTWEEK,
108  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_1,
109  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_2,
110  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_3,
111  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_4,
112  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_5,
113  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_6,
114  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_7,
115  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_8,
116  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_9,
117  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_10,
118  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_11,
119  self::AUTOFILTER_RULETYPE_DYNAMIC_MONTH_12,
120  self::AUTOFILTER_RULETYPE_DYNAMIC_QUARTER_1,
121  self::AUTOFILTER_RULETYPE_DYNAMIC_QUARTER_2,
122  self::AUTOFILTER_RULETYPE_DYNAMIC_QUARTER_3,
123  self::AUTOFILTER_RULETYPE_DYNAMIC_QUARTER_4,
124  self::AUTOFILTER_RULETYPE_DYNAMIC_ABOVEAVERAGE,
125  self::AUTOFILTER_RULETYPE_DYNAMIC_BELOWAVERAGE,
126  ];
127 
128  /*
129  * The only valid filter rule operators for filter and customFilter types are:
130  * <xsd:enumeration value="equal"/>
131  * <xsd:enumeration value="lessThan"/>
132  * <xsd:enumeration value="lessThanOrEqual"/>
133  * <xsd:enumeration value="notEqual"/>
134  * <xsd:enumeration value="greaterThanOrEqual"/>
135  * <xsd:enumeration value="greaterThan"/>
136  */
139  const AUTOFILTER_COLUMN_RULE_GREATERTHAN = 'greaterThan';
140  const AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL = 'greaterThanOrEqual';
142  const AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL = 'lessThanOrEqual';
143 
144  private static $operators = [
145  self::AUTOFILTER_COLUMN_RULE_EQUAL,
146  self::AUTOFILTER_COLUMN_RULE_NOTEQUAL,
147  self::AUTOFILTER_COLUMN_RULE_GREATERTHAN,
148  self::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL,
149  self::AUTOFILTER_COLUMN_RULE_LESSTHAN,
150  self::AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL,
151  ];
152 
155 
156  private static $topTenValue = [
157  self::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE,
158  self::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT,
159  ];
160 
163 
164  private static $topTenType = [
165  self::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP,
166  self::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM,
167  ];
168 
169  // Rule Operators (Numeric, Boolean etc)
170 // const AUTOFILTER_COLUMN_RULE_BETWEEN = 'between'; // greaterThanOrEqual 1 && lessThanOrEqual 2
171  // Rule Operators (Numeric Special) which are translated to standard numeric operators with calculated values
172 // const AUTOFILTER_COLUMN_RULE_TOPTEN = 'topTen'; // greaterThan calculated value
173 // const AUTOFILTER_COLUMN_RULE_TOPTENPERCENT = 'topTenPercent'; // greaterThan calculated value
174 // const AUTOFILTER_COLUMN_RULE_ABOVEAVERAGE = 'aboveAverage'; // Value is calculated as the average
175 // const AUTOFILTER_COLUMN_RULE_BELOWAVERAGE = 'belowAverage'; // Value is calculated as the average
176  // Rule Operators (String) which are set as wild-carded values
177 // const AUTOFILTER_COLUMN_RULE_BEGINSWITH = 'beginsWith'; // A*
178 // const AUTOFILTER_COLUMN_RULE_ENDSWITH = 'endsWith'; // *Z
179 // const AUTOFILTER_COLUMN_RULE_CONTAINS = 'contains'; // *B*
180 // const AUTOFILTER_COLUMN_RULE_DOESNTCONTAIN = 'notEqual'; // notEqual *B*
181  // Rule Operators (Date Special) which are translated to standard numeric operators with calculated values
182 // const AUTOFILTER_COLUMN_RULE_BEFORE = 'lessThan';
183 // const AUTOFILTER_COLUMN_RULE_AFTER = 'greaterThan';
184 // const AUTOFILTER_COLUMN_RULE_YESTERDAY = 'yesterday';
185 // const AUTOFILTER_COLUMN_RULE_TODAY = 'today';
186 // const AUTOFILTER_COLUMN_RULE_TOMORROW = 'tomorrow';
187 // const AUTOFILTER_COLUMN_RULE_LASTWEEK = 'lastWeek';
188 // const AUTOFILTER_COLUMN_RULE_THISWEEK = 'thisWeek';
189 // const AUTOFILTER_COLUMN_RULE_NEXTWEEK = 'nextWeek';
190 // const AUTOFILTER_COLUMN_RULE_LASTMONTH = 'lastMonth';
191 // const AUTOFILTER_COLUMN_RULE_THISMONTH = 'thisMonth';
192 // const AUTOFILTER_COLUMN_RULE_NEXTMONTH = 'nextMonth';
193 // const AUTOFILTER_COLUMN_RULE_LASTQUARTER = 'lastQuarter';
194 // const AUTOFILTER_COLUMN_RULE_THISQUARTER = 'thisQuarter';
195 // const AUTOFILTER_COLUMN_RULE_NEXTQUARTER = 'nextQuarter';
196 // const AUTOFILTER_COLUMN_RULE_LASTYEAR = 'lastYear';
197 // const AUTOFILTER_COLUMN_RULE_THISYEAR = 'thisYear';
198 // const AUTOFILTER_COLUMN_RULE_NEXTYEAR = 'nextYear';
199 // const AUTOFILTER_COLUMN_RULE_YEARTODATE = 'yearToDate'; // <dynamicFilter val="40909" type="yearToDate" maxVal="41113"/>
200 // const AUTOFILTER_COLUMN_RULE_ALLDATESINMONTH = 'allDatesInMonth'; // <dynamicFilter type="M2"/> for Month/February
201 // const AUTOFILTER_COLUMN_RULE_ALLDATESINQUARTER = 'allDatesInQuarter'; // <dynamicFilter type="Q2"/> for Quarter 2
202 
208  private $parent;
209 
215  private $ruleType = self::AUTOFILTER_RULETYPE_FILTER;
216 
222  private $value = '';
223 
229  private $operator = self::AUTOFILTER_COLUMN_RULE_EQUAL;
230 
236  private $grouping = '';
237 
243  public function __construct(?Column $pParent = null)
244  {
245  $this->parent = $pParent;
246  }
247 
253  public function getRuleType()
254  {
255  return $this->ruleType;
256  }
257 
265  public function setRuleType($pRuleType)
266  {
267  if (!in_array($pRuleType, self::$ruleTypes)) {
268  throw new PhpSpreadsheetException('Invalid rule type for column AutoFilter Rule.');
269  }
270 
271  $this->ruleType = $pRuleType;
272 
273  return $this;
274  }
275 
281  public function getValue()
282  {
283  return $this->value;
284  }
285 
293  public function setValue($pValue)
294  {
295  if (is_array($pValue)) {
296  $grouping = -1;
297  foreach ($pValue as $key => $value) {
298  // Validate array entries
299  if (!in_array($key, self::$dateTimeGroups)) {
300  // Remove any invalid entries from the value array
301  unset($pValue[$key]);
302  } else {
303  // Work out what the dateTime grouping will be
304  $grouping = max($grouping, array_search($key, self::$dateTimeGroups));
305  }
306  }
307  if (count($pValue) == 0) {
308  throw new PhpSpreadsheetException('Invalid rule value for column AutoFilter Rule.');
309  }
310  // Set the dateTime grouping that we've anticipated
311  $this->setGrouping(self::$dateTimeGroups[$grouping]);
312  }
313  $this->value = $pValue;
314 
315  return $this;
316  }
317 
323  public function getOperator()
324  {
325  return $this->operator;
326  }
327 
335  public function setOperator($pOperator)
336  {
337  if (empty($pOperator)) {
338  $pOperator = self::AUTOFILTER_COLUMN_RULE_EQUAL;
339  }
340  if (
341  (!in_array($pOperator, self::$operators)) &&
342  (!in_array($pOperator, self::$topTenValue))
343  ) {
344  throw new PhpSpreadsheetException('Invalid operator for column AutoFilter Rule.');
345  }
346  $this->operator = $pOperator;
347 
348  return $this;
349  }
350 
356  public function getGrouping()
357  {
358  return $this->grouping;
359  }
360 
368  public function setGrouping($pGrouping)
369  {
370  if (
371  ($pGrouping !== null) &&
372  (!in_array($pGrouping, self::$dateTimeGroups)) &&
373  (!in_array($pGrouping, self::$dynamicTypes)) &&
374  (!in_array($pGrouping, self::$topTenType))
375  ) {
376  throw new PhpSpreadsheetException('Invalid rule type for column AutoFilter Rule.');
377  }
378  $this->grouping = $pGrouping;
379 
380  return $this;
381  }
382 
392  public function setRule($pOperator, $pValue, $pGrouping = null)
393  {
394  $this->setOperator($pOperator);
395  $this->setValue($pValue);
396  // Only set grouping if it's been passed in as a user-supplied argument,
397  // otherwise we're calculating it when we setValue() and don't want to overwrite that
398  // If the user supplies an argumnet for grouping, then on their own head be it
399  if ($pGrouping !== null) {
400  $this->setGrouping($pGrouping);
401  }
402 
403  return $this;
404  }
405 
411  public function getParent()
412  {
413  return $this->parent;
414  }
415 
423  public function setParent(?Column $pParent = null)
424  {
425  $this->parent = $pParent;
426 
427  return $this;
428  }
429 
433  public function __clone()
434  {
435  $vars = get_object_vars($this);
436  foreach ($vars as $key => $value) {
437  if (is_object($value)) {
438  if ($key == 'parent') {
439  // Detach from autofilter column parent
440  $this->$key = null;
441  } else {
442  $this->$key = clone $value;
443  }
444  } else {
445  $this->$key = $value;
446  }
447  }
448  }
449 }
getParent()
Get this Rule&#39;s AutoFilter Column Parent.
Definition: Rule.php:411
setParent(?Column $pParent=null)
Set this Rule&#39;s AutoFilter Column Parent.
Definition: Rule.php:423
setRule($pOperator, $pValue, $pGrouping=null)
Set AutoFilter Rule.
Definition: Rule.php:392
getGrouping()
Get AutoFilter Rule Grouping.
Definition: Rule.php:356
setGrouping($pGrouping)
Set AutoFilter Rule Grouping.
Definition: Rule.php:368
setRuleType($pRuleType)
Set AutoFilter Rule Type.
Definition: Rule.php:265
setOperator($pOperator)
Set AutoFilter Rule Operator.
Definition: Rule.php:335
setValue($pValue)
Set AutoFilter Rule Value.
Definition: Rule.php:293
getOperator()
Get AutoFilter Rule Operator.
Definition: Rule.php:323
__construct(?Column $pParent=null)
Create a new Rule.
Definition: Rule.php:243
$key
Definition: croninfo.php:18
__clone()
Implement PHP __clone to create a deep clone, not just a shallow copy.
Definition: Rule.php:433