ComparisonFactory.php
1.56 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace ACP\Search\Helper\Sql;
use ACP\Search\Operators;
use ACP\Search\Value;
use LogicException;
final class ComparisonFactory {
/**
* @param string $column
* @param string $operator
* @param Value $value
*
* @return Comparison
*/
public static function create( $column, $operator, Value $value ) {
$operators = [
Operators::EQ => '=',
Operators::NEQ => '!=',
Operators::LT => '<',
Operators::LTE => '<=',
Operators::GT => '>',
Operators::GTE => '>=',
Operators::IS_EMPTY => '=',
Operators::NOT_IS_EMPTY => '!=',
];
if ( array_key_exists( $operator, $operators ) ) {
return new Comparison( $column, $operators[ $operator ], $value );
}
$operators = [
Operators::CONTAINS => 'Contains',
Operators::NOT_CONTAINS => 'NotContains',
Operators::BEGINS_WITH => 'BeginsWith',
Operators::ENDS_WITH => 'EndsWith',
Operators::IN => 'In',
Operators::NOT_IN => 'NotIn',
Operators::BETWEEN => 'Between',
Operators::TODAY => 'Today',
Operators::FUTURE => 'Future',
Operators::PAST => 'Past',
Operators::LT_DAYS_AGO => 'LtDaysAgo',
Operators::GT_DAYS_AGO => 'GtDaysAgo',
Operators::WITHIN_DAYS => 'WithinDays',
Operators::CURRENT_USER => 'CurrentUser',
];
if ( ! array_key_exists( $operator, $operators ) ) {
throw new LogicException( 'Invalid operator found.' );
}
$class = __NAMESPACE__ . '\Comparison\\' . $operators[ $operator ];
return new $class( $column, $value );
}
}