ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
DateComparator.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use Exception;
25 
31 class DateComparator extends BaseComparator
32 {
33  public function __construct(string $test)
34  {
35  if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {
36  throw new InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test));
37  }
38 
39  try {
40  $date = new \DateTime($matches[2]);
41  $target = $date->format('U');
42  } catch (Exception) {
43  throw new InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2]));
44  }
45 
46  $operator = $matches[1] ?? '==';
47 
48  if ('since' === $operator || 'after' === $operator) {
49  $operator = '>';
50  }
51 
52  if ('until' === $operator || 'before' === $operator) {
53  $operator = '<';
54  }
55 
56  $this->setOperator($operator);
57  $this->setTarget($target);
58  }
59 }