Completed
Branch milestone/2_0/react-ui (57d10c)
by htmlBurger
02:47
created

Group_Field::verify_unique_field_name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 7
loc 7
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
use Carbon_Fields\Datastore\Datastore_Interface;
6
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
7
8
class Group_Field {
9
10
	const DEFAULT_GROUP_NAME = '_';
11
12
	/**
13
	 * Unique group identificator. Generated randomly.
14
	 *
15
	 * @var string
16
	 */
17
	protected $group_id;
18
19
	/**
20
	 * Sanitized group name.
21
	 *
22
	 * @var string
23
	 */
24
	protected $name;
25
26
	/**
27
	 * Group label, used during rendering.
28
	 *
29
	 * @var string
30
	 */
31
	protected $label;
32
33
	/**
34
	 * Group label template.
35
	 *
36
	 * @var string
37
	 */
38
	protected $label_template;
39
40
	/**
41
	 * Group fields.
42
	 *
43
	 * @var array
44
	 */
45
	protected $fields = array();
46
47
	/**
48
	 * List of registered unique field names
49
	 *
50
	 * @see register_field_name()
51
	 * @var array
52
	 */
53
	protected $registered_field_names = array();
54
55
	/**
56
	 * Create a group field with the specified name and label.
57
	 *
58
	 * @param string $name
59
	 * @param string $label
60
	 * @param array  $fields
61
	 */
62
	public function __construct( $name, $label, $fields ) {
63
		$this->set_name( $name );
64
		$this->set_label( $label );
65
		$this->add_fields( $fields );
66
67
		// Pick random ID
68
		$random_string = md5( mt_rand() . $this->get_name() . $this->get_label() );
69
		$random_string = substr( $random_string, 0, 5 ); // 5 chars should be enough
70
		$this->group_id = 'carbon-group-' . $random_string;
71
	}
72
73
	/**
74
	 * Add a group of fields.
75
	 *
76
	 * @param array $fields
77
	 */
78
	public function add_fields( $fields ) {
79
		foreach ( $fields as $field ) {
80
			if ( ! is_a( $field, __NAMESPACE__ . '\\Field' ) ) {
81
				Incorrect_Syntax_Exception::raise( 'Object must be of type ' . __NAMESPACE__ . '\\Field' );
82
			}
83
84
			$this->register_field_name( $field->get_name() );
85
		}
86
87
		$this->fields = array_merge( $this->fields, $fields );
88
	}
89
90
	/**
91
	 * Fields attribute getter.
92
	 *
93
	 * @return array
94
	 */
95
	public function get_fields() {
96
		return $this->fields;
97
	}
98
99
	/**
100
	 * Return the names of all fields.
101
	 *
102
	 * @return array
103
	 */
104
	public function get_field_names() {
105
		$names = array();
106
107
		foreach ( $this->fields as $field ) {
108
			$names[] = $field->get_name();
109
		}
110
111
		return $names;
112
	}
113
114
	/**
115
	 * Returns an array that holds the field data, suitable for JSON representation.
116
	 *
117
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
118
	 * @return array
119
	 */
120
	public function to_json( $load ) {
121
		$fields_data = array();
122
123
		foreach ( $this->get_fields() as $field ) {
124
			$fields_data[] = $field->to_json( $load );
125
		}
126
127
		$group_data = array(
128
			'group_id' => $this->get_group_id(),
129
			'name' => $this->get_name(),
130
			'label' => $this->get_label(),
131
			'label_template' => $this->get_label_template(),
132
			'fields' => $fields_data,
133
		);
134
135
		return $group_data;
136
	}
137
138
	/**
139
	 * Group ID attribute getter.
140
	 *
141
	 * @return string
142
	 */
143
	public function get_group_id() {
144
		return $this->group_id;
145
	}
146
147
	/**
148
	 * Set the group label.
149
	 *
150
	 * @param string $label If null, the label will be generated from the group name
151
	 */
152 View Code Duplication
	public function set_label( $label ) {
153
		// Try to guess field label from it's name
154
		if ( is_null( $label ) ) {
155
			// remove the leading underscore(if it's there)
156
			$label = preg_replace( '~^_~', '', $this->name );
157
158
			// remove the leading "crb_"(if it's there)
159
			$label = preg_replace( '~^crb_~', '', $label );
160
161
			// split the name into words and make them capitalized
162
			$label = ucwords( str_replace( '_', ' ', $label ) );
163
		}
164
165
		$this->label = $label;
166
	}
167
168
	/**
169
	 * Label attribute getter.
170
	 *
171
	 * @return string
172
	 */
173
	public function get_label() {
174
		return $this->label;
175
	}
176
177
	/**
178
	 * Set the label template.
179
	 *
180
	 * @param string $template
181
	 */
182
	public function set_label_template( $template ) {
183
		$this->label_template = $template;
184
	}
185
186
	/**
187
	 * Get the label template.
188
	 *
189
	 * @return string
190
	 */
191
	public function get_label_template() {
192
		return $this->label_template;
193
	}
194
195
	/**
196
	 * Print the label template.
197
	 */
198
	public function template_label() {
199
		echo $this->label_template;
200
	}
201
202
	/**
203
	 * Set the group name.
204
	 *
205
	 * @param string $name  Group name, either sanitized or not
206
	 */
207
	public function set_name( $name ) {
208
		if ( ! $name ) {
209
			$name = static::DEFAULT_GROUP_NAME;
210
		}
211
212
		$regex = '/\A[a-z0-9_]+\z/';
213
		if ( ! preg_match( $regex, $name ) ) {
214
			Incorrect_Syntax_Exception::raise( 'Group name can only contain lowercase alphanumeric characters and underscores.' );
215
		}
216
217
		$this->name = $name;
218
	}
219
220
	/**
221
	 * Return the group name.
222
	 *
223
	 * @return string
224
	 */
225
	public function get_name() {
226
		return $this->name;
227
	}
228
229
	/**
230
	 * Assign a DataStore instance for all group fields.
231
	 *
232
	 * @param object $datastore
233
	 */
234
	public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) {
235
		foreach ( $this->fields as $field ) {
236
			$field->set_datastore( $datastore, $set_as_default );
237
		}
238
	}
239
240
	/**
241
	 * Perform checks whether there is a field registered with the name $name.
242
	 * If not, the field name is recorded.
243
	 *
244
	 * @param string $name
245
	 */
246 View Code Duplication
	public function register_field_name( $name ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
247
		if ( in_array( $name, $this->registered_field_names ) ) {
248
			Incorrect_Syntax_Exception::raise( 'Field name "' . $name . '" already registered' );
249
			return false;
250
		}
251
252
		$this->registered_field_names[] = $name;
253
		return true;
254
	}
255
}
256