Completed
Push — develop ( 1b9b3e...5c91dc )
by Zack
04:18
created

GravityView_Entry_Approval_Status::is_approved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @file class-gravityview-entry-approval-status.php
4
 * @package   GravityView
5
 * @license   GPL2+
6
 * @author    Katz Web Services, Inc.
7
 * @link      https://gravityview.co
8
 * @copyright Copyright 2016, Katz Web Services, Inc.
9
 *
10
 * @since 1.18
11
 */
12
13
/** If this file is called directly, abort. */
14
if ( ! defined( 'ABSPATH' ) ) {
15
	die;
16
}
17
18
/**
19
 * There are specific values of entry approval that are valid. This class holds them and manages access to them.
20
 *
21
 * @since 1.18
22
 */
23
final class GravityView_Entry_Approval_Status {
24
25
	/**
26
	 * @todo Convert to integer, convert meta value in GravityView_Migrate
27
	 * @var string The value of the "Approved" status
28
	 */
29
	const Approved = 'Approved';
30
31
	/**
32
	 * @var int The value of the "Disapproved" status
33
	 */
34
	const Disapproved = 0;
35
36
	/**
37
	 * GravityView_Entry_Approval_Status constructor.
38
	 */
39
	private function __construct() {}
40
41
	/**
42
	 * Match values to the labels
43
	 *
44
	 * @return array
45
	 */
46
	private static function get_choices() {
47
		return array(
48
			self::Disapproved => esc_html__( 'Disapproved', 'gravityview' ),
49
			self::Approved    => esc_html__( 'Approved', 'gravityview' ),
50
		);
51
	}
52
53
	/**
54
	 * Get the status values as an array
55
	 *
56
	 * @return array Array of values for approval status choices
57
	 */
58
	public static function get_values() {
59
60
		$values = self::get_choices();
61
62
		return array_keys( $values );
63
	}
64
65
	/**
66
	 * Convert previously-used values to the current values, for backward compatibility
67
	 *
68
	 * @param string $old_value The status
69
	 *
70
	 * @return int|string Current value, possibly converted from old value
71
	 */
72
	private static function maybe_convert_status( $old_value = '' ) {
73
74
		$new_value = $old_value;
75
76
		switch ( $old_value ) {
77
78
			// Approved values
79
			case 'Approved':
80
			case '1':
81
				$new_value = self::Approved;
82
				break;
83
84
			//Disapproved values
85
			case '0':
86
				$new_value = self::Disapproved;
87
				break;
88
		}
89
90
		return $new_value;
91
	}
92
93
	/**
94
	 * Check whether the passed value is one of the defined values for entry approval
95
	 *
96
	 * @param mixed $value
97
	 *
98
	 * @return bool True: value is valid; false: value is not valid
99
	 */
100
	public static function is_valid( $value ) {
101
102
		$value = self::maybe_convert_status( $value );
103
104
		return in_array( $value, self::get_values(), true );
105
	}
106
107
	/**
108
	 * @param mixed $status Value to check approval of
109
	 *
110
	 * @return bool True: passed $status matches approved value
111
	 */
112
	public static function is_approved( $status ) {
113
114
		$status = self::maybe_convert_status( $status );
115
116
		return ( self::Approved === $status );
117
	}
118
119
	/**
120
	 * @param mixed $status Value to check approval of
121
	 *
122
	 * @return bool True: passed $status matches disapproved value
123
	 */
124
	public static function is_disapproved( $status ) {
125
126
		$status = self::maybe_convert_status( $status );
127
128
		return ( self::Disapproved === $status );
129
	}
130
131
	/**
132
	 * Get the labels for the status choices
133
	 *
134
	 * @return array Array of labels for the status choices ("Approved", "Disapproved")
135
	 */
136
	public static function get_labels() {
137
138
		$values = self::get_choices();
139
140
		return array_values( $values );
141
	}
142
143
	/**
144
	 * Get the label for a specific approval value
145
	 *
146
	 * @param string $value Valid approval value
147
	 *
148
	 * @return string|false Label of value ("Approved"). If invalid value, return false.
149
	 */
150
	public static function get_label( $value ) {
151
152
		$values = self::get_choices();
153
154
		return rgar( $values, $value, false );
155
	}
156
}
157