ILIAS  release_8 Revision v8.24
BaseComparator.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
6
7use InvalidArgumentException;
8
9/******************************************************************************
10 *
11 * This file is part of ILIAS, a powerful learning management system.
12 *
13 * ILIAS is licensed with the GPL-3.0, you should have received a copy
14 * of said license along with the source code.
15 *
16 * If this is not the case or you just want to try ILIAS, you'll find
17 * us at:
18 * https://www.ilias.de
19 * https://github.com/ILIAS-eLearning
20 *
21 *****************************************************************************/
22
28abstract class BaseComparator
29{
30 private string $target = '';
31 private string $operator = '==';
32
33 public function getTarget(): string
34 {
35 return $this->target;
36 }
37
38 public function setTarget(string $target): void
39 {
40 $this->target = $target;
41 }
42
43 public function getOperator(): string
44 {
45 return $this->operator;
46 }
47
48 public function setOperator(string $operator): void
49 {
50 if ($operator === '') {
51 $operator = '==';
52 }
53
54 if (!in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
55 throw new InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
56 }
57
58 $this->operator = $operator;
59 }
60
61 public function test(string $test): bool
62 {
63 switch ($this->operator) {
64 case '>':
65 return $test > $this->target;
66
67 case '>=':
68 return $test >= $this->target;
69
70 case '<':
71 return $test < $this->target;
72
73 case '<=':
74 return $test <= $this->target;
75
76 case '!=':
77 return $test !== $this->target;
78 }
79
80 return $test === $this->target;
81 }
82}