ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
BaseComparator.php
Go to the documentation of this file.
1<?php
2declare(strict_types=1);
3
5
11abstract class BaseComparator
12{
14 private $target = '';
15
17 private $operator = '==';
18
22 public function getTarget() : string
23 {
24 return $this->target;
25 }
26
30 public function setTarget(string $target)
31 {
32 $this->target = $target;
33 }
34
38 public function getOperator() : string
39 {
40 return $this->operator;
41 }
42
46 public function setOperator(string $operator)
47 {
48 if (0 === strlen($operator)) {
49 $operator = '==';
50 }
51
52 if (!in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
53 throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
54 }
55
56 $this->operator = $operator;
57 }
58
63 public function test(string $test) : bool
64 {
65 switch ($this->operator) {
66 case '>':
67 return $test > $this->target;
68
69 case '>=':
70 return $test >= $this->target;
71
72 case '<':
73 return $test < $this->target;
74
75 case '<=':
76 return $test <= $this->target;
77
78 case '!=':
79 return $test != $this->target;
80 }
81
82 return $test == $this->target;
83 }
84}
$test
Definition: Utf8Test.php:84
An exception for terminatinating execution or to throw for unit testing.