Completed
Pull Request — 2.x (#4569)
by Scott Kingsley
04:56
created

PodsField_Phone::pre_save()   C

Complexity

Conditions 12
Paths 98

Size

Total Lines 67
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 42
nc 98
nop 7
dl 0
loc 67
rs 5.8429
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @package Pods\Fields
5
 */
6
class PodsField_Phone extends PodsField {
7
8
	/**
9
	 * {@inheritdoc}
10
	 */
11
	public static $group = 'Text';
12
13
	/**
14
	 * {@inheritdoc}
15
	 */
16
	public static $type = 'phone';
17
18
	/**
19
	 * {@inheritdoc}
20
	 */
21
	public static $label = 'Phone';
22
23
	/**
24
	 * {@inheritdoc}
25
	 */
26
	public static $prepare = '%s';
27
28
	/**
29
	 * {@inheritdoc}
30
	 */
31
	public function setup() {
32
33
		self::$label = __( 'Phone', 'pods' );
34
	}
35
36
	/**
37
	 * {@inheritdoc}
38
	 */
39
	public function options() {
40
41
		$options = array(
42
			static::$type . '_repeatable'  => array(
43
				'label'             => __( 'Repeatable Field', 'pods' ),
44
				'default'           => 0,
45
				'type'              => 'boolean',
46
				'help'              => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),
47
				'boolean_yes_label' => '',
48
				'dependency'        => true,
49
				'developer_mode'    => true,
50
			),
51
			static::$type . '_format'      => array(
52
				'label'   => __( 'Format', 'pods' ),
53
				'default' => '999-999-9999 x999',
54
				'type'    => 'pick',
55
				'data'    => array(
56
					__( 'US', 'pods' )            => array(
57
						'999-999-9999 x999'   => '123-456-7890 x123',
58
						'(999) 999-9999 x999' => '(123) 456-7890 x123',
59
						'999.999.9999 x999'   => '123.456.7890 x123',
60
					),
61
					__( 'International', 'pods' ) => array(
62
						'international' => __( 'Any (no validation available)', 'pods' ),
63
					),
64
				),
65
			),
66
			static::$type . '_options'     => array(
67
				'label' => __( 'Phone Options', 'pods' ),
68
				'group' => array(
69
					static::$type . '_enable_phone_extension' => array(
70
						'label'   => __( 'Enable Phone Extension?', 'pods' ),
71
						'default' => 1,
72
						'type'    => 'boolean',
73
					),
74
				),
75
			),
76
			static::$type . '_max_length'  => array(
77
				'label'   => __( 'Maximum Length', 'pods' ),
78
				'default' => 25,
79
				'type'    => 'number',
80
				'help'    => __( 'Set to -1 for no limit', 'pods' ),
81
			),
82
			static::$type . '_html5'       => array(
83
				'label'   => __( 'Enable HTML5 Input Field?', 'pods' ),
84
				'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ),
85
				'type'    => 'boolean',
86
			),
87
			static::$type . '_placeholder' => array(
88
				'label'   => __( 'HTML Placeholder', 'pods' ),
89
				'default' => '',
90
				'type'    => 'text',
91
				'help'    => array(
92
					__( 'Placeholders can provide instructions or an example of the required data format for a field. Please note: It is not a replacement for labels or description text, and it is less accessible for people using screen readers.', 'pods' ),
93
					'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
94
				),
95
			),
96
		);
97
98
		return $options;
99
	}
100
101
	/**
102
	 * {@inheritdoc}
103
	 */
104 View Code Duplication
	public function schema( $options = null ) {
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...
105
106
		$length = (int) pods_v( static::$type . '_max_length', $options, 25, true );
107
108
		$schema = 'VARCHAR(' . $length . ')';
109
110
		if ( 255 < $length || $length < 1 ) {
111
			$schema = 'LONGTEXT';
112
		}
113
114
		return $schema;
115
	}
116
117
	/**
118
	 * {@inheritdoc}
119
	 */
120 View Code Duplication
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
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...
121
122
		$options         = (array) $options;
123
		$form_field_type = PodsForm::$field_type;
124
125
		if ( is_array( $value ) ) {
126
			$value = implode( ' ', $value );
127
		}
128
129
		$field_type = 'phone';
130
131
		if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) {
132
			if ( pods_v( 'read_only', $options, false ) ) {
133
				$options['readonly'] = true;
134
135
				$field_type = 'text';
136
			} else {
137
				return;
138
			}
139
		} elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
140
			$options['readonly'] = true;
141
142
			$field_type = 'text';
143
		}
144
145
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
146
	}
147
148
	/**
149
	 * {@inheritdoc}
150
	 */
151 View Code Duplication
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
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...
152
153
		$errors = array();
154
155
		$label = strip_tags( pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
156
157
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
158
159
		if ( is_array( $check ) ) {
160
			$errors = $check;
161
		} else {
162
			if ( 0 < strlen( $value ) && '' === $check ) {
163
				if ( 1 === (int) pods_v( 'required', $options ) ) {
164
					$errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
165
				} else {
166
					$errors[] = sprintf( __( 'Invalid phone number provided for the field %s.', 'pods' ), $label );
167
				}
168
			}
169
		}
170
171
		if ( ! empty( $errors ) ) {
172
			return $errors;
173
		}
174
175
		return true;
176
	}
177
178
	/**
179
	 * {@inheritdoc}
180
	 */
181
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
182
183
		$options = (array) $options;
184
185
		if ( 'international' !== pods_v( static::$type . '_format', $options ) ) {
186
			// Clean input
187
			$number = preg_replace( '/([^0-9ext])/', '', $value );
188
189
			$number = str_replace(
190
				array( '-', '.', 'ext', 'x', 't', 'e', '(', ')' ), array(
191
					'',
192
					'',
193
					'|',
194
					'|',
195
					'',
196
					'',
197
					'',
198
					'',
199
				), $number
200
			);
201
202
			// Get extension
203
			$extension = explode( '|', $number );
204
			if ( 1 < count( $extension ) ) {
205
				$number    = preg_replace( '/([^0-9])/', '', $extension[0] );
206
				$extension = preg_replace( '/([^0-9])/', '', $extension[1] );
207
			} else {
208
				$extension = '';
209
			}
210
211
			// Build number array
212
			$numbers = str_split( $number, 3 );
213
214
			if ( isset( $numbers[3] ) ) {
215
				$numbers[2] .= $numbers[3];
216
				$numbers     = array( $numbers[0], $numbers[1], $numbers[2] );
217
			} elseif ( isset( $numbers[1] ) ) {
218
				$numbers = array( $numbers[0], $numbers[1] );
219
			}
220
221
			// Format number
222
			if ( '(999) 999-9999 x999' === pods_v( static::$type . '_format', $options ) ) {
223
				if ( 2 === count( $numbers ) ) {
224
					$value = implode( '-', $numbers );
225
				} else {
226
					$value = '(' . $numbers[0] . ') ' . $numbers[1] . '-' . $numbers[2];
227
				}
228
			} elseif ( '999.999.9999 x999' === pods_v( static::$type . '_format', $options ) ) {
229
				$value = implode( '.', $numbers );
230
			} else {
231
				$value = implode( '-', $numbers );
232
			}
233
234
			// Add extension
235
			if ( 1 === (int) pods_v( static::$type . '_enable_phone_extension', $options ) && 0 < strlen( $extension ) ) {
236
				$value .= ' x' . $extension;
237
			}
238
		}//end if
239
240
		$length = (int) pods_v( static::$type . '_max_length', $options, 25 );
241
242
		if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
243
			$value = pods_mb_substr( $value, 0, $length );
244
		}
245
246
		return $value;
247
	}
248
}
249