Completed
Push — master ( cf8268...397f5b )
by Zack
24:22 queued 11:06
created

GravityView_Fields::get_associated_field()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 6
nop 1
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
crap 4.074
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @file class-gravityview-fields.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 */
7
8
/**
9
 * Wanted to extend GF_Fields, but couldn't because static variables are inherited,
10
 * so $_fields would always be GF results
11
 *
12
 * @see GF_Fields
13
 */
14
final class GravityView_Fields {
15
16
	/* @var GravityView_Field[] */
17
	protected static $_fields = array();
18
19
	/**
20
	 * @param GravityView_Field $field Field to register
21
	 *
22
	 * @throws Exception If requirements aren't met
23
	 *
24
	 * @return void
25
	 */
26
	public static function register( $field ) {
27
		if ( ! is_subclass_of( $field, 'GravityView_Field' ) ) {
28
			throw new Exception( 'Must be a subclass of GravityView_Field' );
29
		}
30
		if ( empty( $field->name ) ) {
31
			throw new Exception( 'The name must be set' );
32
		}
33
		if ( isset( self::$_fields[ $field->name ] ) && ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) {
34
			throw new Exception( 'Field type already registered: ' . $field->name );
35
		}
36
		self::$_fields[ $field->name ] = $field;
37
	}
38
39
	/**
40
	 * @param array $properties
41
	 *
42
	 * @return GravityView_Field | bool
43
	 */
44
	public static function create( $properties ) {
45
		$type = isset( $properties['type'] ) ? $properties['type'] : '';
46
		$type = empty( $properties['inputType'] ) ? $type : $properties['inputType'];
47
		if ( empty( $type ) || ! isset( self::$_fields[ $type ] ) ) {
48
			return new GravityView_Field( $properties );
0 ignored issues
show
Unused Code introduced by
The call to GravityView_Field::__construct() has too many arguments starting with $properties.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
49
		}
50
		$class      = self::$_fields[ $type ];
51
		$class_name = get_class( $class );
52
		$field      = new $class_name( $properties );
53
54
		return $field;
55
	}
56
57
	/**
58
	 * Does the field exist (has it been registered)?
59
	 *
60
	 * @param string $field_name
61
	 *
62
	 * @return bool True: yes, it exists; False: nope
63
	 */
64
	public static function exists( $field_name ) {
65
		return isset( self::$_fields["{$field_name}"] );
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
66
	}
67
68
	/**
69
	 * @param string $field_name
70
	 *
71
	 * @return GravityView_Field|false
72
	 */
73 19
	public static function get_instance( $field_name ) {
74 19
		return isset( self::$_fields[ $field_name ] ) ? self::$_fields[ $field_name ] : false;
75
	}
76
77
	/**
78
	 * Alias for get_instance()
79
	 *
80
	 * @param $field_name
81
	 *
82
	 * @return GravityView_Field|false
83
	 */
84
	public static function get( $field_name ) {
85
		return self::get_instance( $field_name );
86
	}
87
88
	/**
89
	 * Alias for get_instance()
90
	 *
91
	 * @param string|GF_Field $gf_field Gravity Forms field class or the class name type
92
	 *
93
	 * @return GravityView_Field|false Returns false if no matching fields found
94
	 */
95 3
	public static function get_associated_field( $gf_field ) {
96
97 3
		$field_type = is_a( $gf_field, 'GF_Field' ) ? get_class( $gf_field ) : $gf_field;
98
99 3
		foreach( self::$_fields as $field ) {
100 3
			if( $field_type === $field->_gf_field_class_name ) {
101 3
				return $field;
102
			}
103
		}
104
105
		return false;
106
	}
107
108
	/**
109
	 * Get all fields
110
	 *
111
	 * @since 1.16 Added $group parameter
112
	 *
113
	 * @param string $group Optional. If defined, fetch all fields in a group
114
	 *
115
	 * @return GravityView_Field[]
116
	 */
117 5
	public static function get_all( $group = '' ) {
118
119 5
		if( '' !== $group ) {
120
			$return_fields = self::$_fields;
121
			foreach ( $return_fields as $key => $field ) {
122
				if( $group !== $field->group ) {
123
					unset( $return_fields[ $key ] );
124
				}
125
			}
126
			return $return_fields;
127
		} else {
128 5
			return self::$_fields;
129
		}
130
	}
131
132
}