ILIAS  release_8 Revision v8.24
DateComparator.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
6
7use Exception;
8use InvalidArgumentException;
9
10/******************************************************************************
11 *
12 * This file is part of ILIAS, a powerful learning management system.
13 *
14 * ILIAS is licensed with the GPL-3.0, you should have received a copy
15 * of said license along with the source code.
16 *
17 * If this is not the case or you just want to try ILIAS, you'll find
18 * us at:
19 * https://www.ilias.de
20 * https://github.com/ILIAS-eLearning
21 *
22 *****************************************************************************/
23
30{
31 public function __construct(string $test)
32 {
33 if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {
34 throw new InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test));
35 }
36
37 try {
38 $date = new \DateTime($matches[2]);
39 $target = $date->format('U');
40 } catch (Exception $e) {
41 throw new InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2]));
42 }
43
44 $operator = $matches[1] ?? '==';
45
46 if ('since' === $operator || 'after' === $operator) {
47 $operator = '>';
48 }
49
50 if ('until' === $operator || 'before' === $operator) {
51 $operator = '<';
52 }
53
54 $this->setOperator($operator);
55 $this->setTarget($target);
56 }
57}