ComparisonTrait.php
1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php declare( strict_types=1 );
namespace ACP\Expression;
use ACP\Expression\Exception\OperatorNotFoundException;
trait ComparisonTrait {
use OperatorTrait;
protected function get_operators(): array {
return [
ComparisonOperators::EQUAL,
ComparisonOperators::NOT_EQUAL,
ComparisonOperators::LESS_THAN,
ComparisonOperators::LESS_THAN_EQUAL,
ComparisonOperators::GREATER_THAN,
ComparisonOperators::GREATER_THAN_EQUAL,
];
}
protected function compare( string $operator, $fact, $value ): bool {
switch ( $operator ) {
case ComparisonOperators::EQUAL:
return $value === $fact;
case ComparisonOperators::NOT_EQUAL:
return $value !== $fact;
case ComparisonOperators::GREATER_THAN:
return $value > $fact;
case ComparisonOperators::GREATER_THAN_EQUAL:
return $value >= $fact;
case ComparisonOperators::LESS_THAN:
return $value < $fact;
case ComparisonOperators::LESS_THAN_EQUAL:
return $value <= $fact;
}
throw new OperatorNotFoundException( $operator );
}
}