ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DateComparator.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Exception;
24use InvalidArgumentException;
25
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}