Completed
Push — 717-fix/add-tests-for-posts-de... ( 41a87a...780d20 )
by Sudar
09:45 queued 06:22
created

OperatorHelpers::resolve_operators()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 0
cts 8
cp 0
crap 12
rs 10
1
<?php
2
/**
3
 * From Bulk Delete v6.1.0, the minimum required PHP version is v5.6
4
 * This opens the possibility of using Traits.
5
 */
6
namespace BulkWP\BulkDelete\Core\Base\Mixin;
7
8
/**
9
 * Operator related helpers.
10
 *
11
 * Used in Renderer.
12
 *
13
 * @since 6.1.0
14
 */
15
trait OperatorHelpers {
16
	/**
17
	 * Get the master list of operators.
18
	 *
19
	 * The operators are cached using static variable for performance.
20
	 *
21
	 * @return array Master list of operators.
22
	 */
23
	protected function get_operator_master_list() {
24
		static $operator_with_labels = [];
25
26
		if ( empty( $operator_with_labels ) ) {
27
			$operator_with_labels = [
28
				'='           => __( 'equal to', 'bulk-delete' ),
29
				'!='          => __( 'not equal to', 'bulk-delete' ),
30
				'<'           => __( 'less than', 'bulk-delete' ),
31
				'<='          => __( 'less than or equal to', 'bulk-delete' ),
32
				'>'           => __( 'greater than', 'bulk-delete' ),
33
				'>='          => __( 'greater than or equal to', 'bulk-delete' ),
34
				'IN'          => __( 'in', 'bulk-delete' ),
35
				'NOT IN'      => __( 'not in', 'bulk-delete' ),
36
				'BETWEEN'     => __( 'between', 'bulk-delete' ),
37
				'NOT BETWEEN' => __( 'not between', 'bulk-delete' ),
38
				'EXISTS'      => __( 'exists', 'bulk-delete' ),
39
				'NOT EXISTS'  => __( 'not exists', 'bulk-delete' ),
40
				'LIKE'        => __( 'contains', 'bulk-delete' ),
41
				'NOT LIKE'    => __( 'not contains', 'bulk-delete' ),
42
				'STARTS WITH' => __( 'starts with', 'bulk-delete' ),
43
				'ENDS WITH'   => __( 'ends with', 'bulk-delete' ),
44
			];
45
		}
46
47
		return $operator_with_labels;
48
	}
49
50
	/**
51
	 * Resolve the operators array.
52
	 *
53
	 * @param array $operators Operators array.
54
	 *
55
	 * @return array Resolved operators.
56
	 */
57
	protected function resolve_operators( $operators ) {
58
		$operator_list_to_render = [];
59
60
		$operator_master_list = $this->get_operator_master_list();
61
62
		foreach ( $operators as $operator ) {
63
			if ( array_key_exists( $operator, $operator_master_list ) ) {
64
				$operator_list_to_render[] = $operator;
65
			} else {
66
				$operator_list_to_render = array_merge( $operator_list_to_render, $this->resolve_operator( $operator ) );
67
			}
68
		}
69
70
		return $operator_list_to_render;
71
	}
72
73
	/**
74
	 * Resolve the operator.
75
	 *
76
	 * Users can specify placeholders for operators.
77
	 * The following placeholders are currently supported
78
	 * - equals
79
	 * - numeric
80
	 * - ins
81
	 * - betweens
82
	 * - exists-all
83
	 * - likes
84
	 * - string-start-end
85
	 * - string-all
86
	 *
87
	 * @param string $operator Operator.
88
	 *
89
	 * @return array Resolved operators.
90
	 */
91
	protected function resolve_operator( $operator ) {
92
		switch ( $operator ) {
93
			case 'equals':
94
				return [ '=', '!=' ];
95
			case 'numeric':
96
				return [ '<', '<=', '>', '>=' ];
97
			case 'ins':
98
				return [ 'IN', 'NOT IN' ];
99
			case 'betweens':
100
				return [ 'BETWEEN', 'NOT BETWEEN' ];
101
			case 'exist-all':
102
				return [ 'EXISTS', 'NOT EXISTS' ];
103
			case 'likes':
104
				return [ 'LIKE', 'NOT LIKE' ];
105
			case 'string-start-end':
106
				return [ 'STARTS WITH', 'ENDS WITH' ];
107
			case 'string-all':
108
				return [ 'STARTS WITH', 'ENDS WITH', 'LIKE', 'NOT LIKE' ];
109
		}
110
	}
111
112
	/**
113
	 * Get the label for an operator.
114
	 *
115
	 * @param string $operator Operator.
116
	 *
117
	 * @return string Operator label.
118
	 */
119
	protected function get_operator_label( $operator ) {
120
		$operator_with_labels = $this->get_operator_master_list();
121
122
		if ( ! array_key_exists( $operator, $operator_with_labels ) ) {
123
			return '';
124
		}
125
126
		return $operator_with_labels[ $operator ];
127
	}
128
}
129