Completed
Push — master ( af77e8...7d9d07 )
by Zack
11s
created

Field   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 169
ccs 27
cts 27
cp 1
rs 10
c 1
b 0
f 0
wmc 11
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B as_configuration() 0 13 5
B from_configuration() 0 25 4
A __get() 0 5 2
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 14 and the first side effect is on line 6.

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
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The default GravityView Field class.
11
 *
12
 * Houses all base Field functionality.
13
 */
14
class Field {
15
16
	/**
17
	 * @var array The custom View configuration for this field.
18
	 *
19
	 * Everything else is in the properties.
20
	 */
21
	private $configuration = array();
22
23
	/**
24
	 * @var string The field position in the view.
25
	 * @api
26
	 * @since future
27
	 */
28
	public $position = '';
29
30
	/**
31
	 * @var string UID for this field.
32
	 *
33
	 * A unique relation identifier between this field and a view.
34
	 *
35
	 * @api
36
	 * @since future
37
	 */
38
	public $UID = '';
39
40
	/**
41
	 * @var string The form field ID for this field.
42
	 * @api
43
	 * @since future
44
	 */
45
	public $ID = '';
46
47
	/**
48
	 * @var string The form label for this field.
49
	 * @api
50
	 * @since future
51
	 */
52
	public $label = '';
53
54
	/**
55
	 * @var string The custom label for this field.
56
	 * @api
57
	 * @since future
58
	 */
59
	public $custom_label = '';
60
61
	/**
62
	 * @var bool Whether to show the label or not for this field.
63
	 * @api
64
	 * @since future
65
	 */
66
	public $show_label = true;
67
68
	/**
69
	 * @var string The custom class for this field.
70
	 * @api
71
	 * @since future
72
	 */
73
	public $custom_class = '';
74
75
	/**
76
	 * @var string The capability required to view this field.
77
	 *
78
	 * If empty, anyone can view it, including non-logged in users.
79
	 *
80
	 * @api
81
	 * @since future
82
	 */
83
	public $cap = '';
84
85
	/**
86
	 * @var bool Show as a link to entry.
87
	 *
88
	 * @api
89
	 * @since future
90
	 */
91
	public $show_as_link = false;
92
93
	/**
94
	 * @var bool Filter this field from searching.
95
	 *
96
	 * @api
97
	 * @since future
98
	 */
99
	public $search_filter = false;
100
101
	/**
102
	 * Return an array of the old format as used by callers of `GVCommon:get_directory_fields()` for example.
103
	 *
104
	 *  		'id' => string '9' (length=1)
105
	 *  		'label' => string 'Screenshots' (length=11)
106
	 *			'show_label' => string '1' (length=1)
107
	 *			'custom_label' => string '' (length=0)
108
	 *			'custom_class' => string 'gv-gallery' (length=10)
109
	 * 			'only_loggedin' => string '0' (length=1)
110
	 *			'only_loggedin_cap' => string 'read' (length=4)
111
	 *			'search_filter' => string '0'
112
	 *			'show_as_link' => string '0'
113
	 *
114
	 *			+ whatever else specific field types may have
115
	 *
116
	 * @internal
117
	 * @since future
118
	 *
119
	 * @return array
120
	 */
121 3
	public function as_configuration() {
122 3
		return array_merge( array(
123 3
			'id' => $this->ID,
124 3
			'label' => $this->label,
125 3
			'show_label' => $this->show_label ? '1' : '0',
126 3
			'custom_label' => $this->custom_label,
127 3
			'custom_class' => $this->custom_class,
128 3
			'only_loggedin' => $this->cap ? '1' : '0',
129 3
			'only_loggedin_cap' => $this->cap,
130 3
			'search_filter' => $this->search_filter ? '1' : '0',
131 3
			'show_as_link' => $this->show_as_link ? '1' : '0',
132 3
		), $this->configuration );
133
	}
134
135
	/**
136
	 * Update self from a configuration array.
137
	 *
138
	 * @see \GV\Field::as_configuration()
139
	 * @internal
140
	 * @since future
141
	 *
142
	 * @return void
143
	 */
144 3
	public function from_configuration( $configuration ) {
145 3
		$configuration = wp_parse_args( $configuration, $this->as_configuration() );
146
147 3
		$this->ID = $configuration['id'];
148 3
		$this->label = $configuration['label'];
149 3
		$this->show_label = $configuration['show_label'] == '1';
150 3
		$this->custom_label = $configuration['custom_label'];
151 3
		$this->custom_class = $configuration['custom_class'];
152 3
		$this->cap = $configuration['only_loggedin'] == '1' ? $configuration['only_loggedin_cap'] : '';
153 3
		$this->search_filter = $configuration['search_filter'] == '1';
154 3
		$this->show_as_link = $configuration['show_as_link'] == '1';
155
156
		/** Shared among all field types (sort of). */
157
		$shared_configuration_keys = array(
158 3
			'id', 'label', 'show_label', 'custom_label', 'custom_class',
159
			'only_loggedin' ,'only_loggedin_cap', 'search_filter', 'show_as_link',
0 ignored issues
show
introduced by
Expected 0 spaces between "'only_loggedin'" and comma; 1 found
Loading history...
160
		);
161
162
		/** Everything else goes into the properties for now. @todo subclasses! */
163 3
		foreach ( $configuration as $key => $value ) {
164 3
			if ( ! in_array( $key, $shared_configuration_keys ) ) {
165 3
				$this->configuration[ $key ] = $value;
166
			}
167
		}
168 3
	}
169
170
	/**
171
	 * Get one of the extra configuration keys via property accessors.
172
	 *
173
	 * @param string $key The key to get.
174
	 *
175
	 * @return mixed|null The value for the given configuration key, null if doesn't exist.
176
	 */
177
	public function __get( $key ) {
178
		if ( isset( $this->configuration[ $key ] ) ) {
179
			return $this->configuration[ $key ];
180
		}
181
	}
182
}
183