ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
BaseComparator.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use InvalidArgumentException;
24
30abstract class BaseComparator
31{
32 private string $target = '';
33 private string $operator = '==';
34
35 public function getTarget(): string
36 {
37 return $this->target;
38 }
39
40 public function setTarget(string $target): void
41 {
42 $this->target = $target;
43 }
44
45 public function getOperator(): string
46 {
47 return $this->operator;
48 }
49
50 public function setOperator(string $operator): void
51 {
52 if ($operator === '') {
53 $operator = '==';
54 }
55
56 if (!in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
57 throw new InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
58 }
59
60 $this->operator = $operator;
61 }
62
63 public function test(string $test): bool
64 {
65 return match ($this->operator) {
66 '>' => $test > $this->target,
67 '>=' => $test >= $this->target,
68 '<' => $test < $this->target,
69 '<=' => $test <= $this->target,
70 '!=' => $test !== $this->target,
71 default => $test === $this->target,
72 };
73 }
74}