Completed
Push — master ( 5e9bea...ff34a2 )
by Zack
46:44 queued 39:47
created

GF_Field::get_label()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 24.1114

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 7
nop 4
dl 0
loc 28
ccs 6
cts 14
cp 0.4286
crap 24.1114
rs 4.909
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 12 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 Gravity Forms \GF_Field field object wrapper.
11
 */
12
class GF_Field extends Field {
13
	
14
	/**
15
	 * @var \GF_Field The backing Gravity Forms field.
16
	 */
17
	public $field;
18
19
	/**
20
	 * Create self from a configuration array.
21
	 *
22
	 * @param array $configuration The configuration array.
23
	 * @see \GV\Field::as_configuration()
24
	 * @internal
25
	 * @since 2.0
26
	 *
27
	 * @return \GV\GF_Field|null The field implementation or null on error.
28
	 */
29 67
	public static function from_configuration( $configuration ) {
30 67
		if ( empty( $configuration['id'] ) || ! is_numeric( $configuration['id'] ) ) {
31
			gravityview()->log->error( 'Invalid configuration[id] supplied.' );
32
			return null;
33
		}
34
35 67
		if ( empty( $configuration['form_id'] ) || ! $form = \GV\GF_Form::by_id( $configuration['form_id'] )  ) {
36 1
			gravityview()->log->error( 'Invalid configuration[form_id] supplied.' );
37 1
			return null;
38
		}
39
40 67
		$field = self::by_id( $form, $configuration['id'] );
41
42 67
		if ( ! $field ) {
43 4
			gravityview()->log->error( 'Invalid configuration[id] supplied.' );
44 4
			return null;
45
		}
46
47 67
		$field->update_configuration( $configuration );
48 67
		return $field;
49
	}
50
51
	/**
52
	 * Get a \GV\GF_Field by \GV\GF_Form and Field ID.
53
	 *
54
	 * @param \GV\GF_Form $form The Gravity Form form.
55
	 * @param int $field_id The Gravity Form field ID for the $form.
56
	 *
57
	 * @return \GV\Field|null The requested field or null if not found.
58
	 */
59 67
	public static function by_id( $form, $field_id ) {
60
61 67
		if ( ! $form || ! is_object( $form ) || ! is_a( $form, '\GV\GF_Form' ) ) {
62
			gravityview()->log->error( '$form is not a \GV\GF_Form instance' );
63
			return null;
64
		}
65
66 67
		if ( empty( $form->form ) ) {
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67
			gravityview()->log->error( '$form is not initialized with a backing Gravity Forms form' );
68
			return null;
69
		}
70
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
71
72 67
		$gv_field = \GFFormsModel::get_field( $form->form, $field_id );
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
73
74 67
		if ( ! $gv_field ) {
75 6
			gravityview()->log->error( 'Invalid $field_id #{field_id} for current source', array( 'field_id' => $field_id ) );
76 6
			return null;
77
		}
78
79 67
		$field = new self();
80 67
		$field->ID = $field_id;
81 67
		$field->field = $gv_field;
82
83 67
		return $field;
84
	}
85
86
	/**
87
	 * Retrieve the label for this field.
88
	 *
89
	 * Requires a \GV\GF_Form in this implementation.
90
	 *
91
	 * @param \GV\View $view The view for this context if applicable.
92
	 * @param \GV\Source $source The source (form) for this context if applicable.
93
	 * @param \GV\Entry $entry The entry for this context if applicable.
94
	 * @param \GV\Request $request The request for this context if applicable.
95
	 *
96
	 * @return string The label for this Gravity Forms field.
97
	 */
98 17
	public function get_label( View $view = null, Source $source = null, Entry $entry = null, Request $request = null ) {
99 17
		if ( $label = parent::get_label( $view, $source, $entry, $request ) ) {
100
			return $label;
101
		}
102
103 17
		if ( ! $this->show_label ) {
104
			return '';
105
		}
106
107 17
		if ( ! $source || ! is_object( $source ) || ! is_a( $source, '\GV\GF_Form' ) ) {
108
			gravityview()->log->error( '$source is not a valid \GV\GF_Form instance' );
109
			return null;
110
		}
111
112 17
		if ( $this->label ) {
113 17
			return $this->label;
114
		}
115
116
		/** This is a complex Gravity Forms input. */
117
		if ( $input = \GFFormsModel::get_input( $this->field, $this->ID ) ) {
118
			$label = ! empty( $input['customLabel'] ) ? $input['customLabel'] : $input['label'];
119
		} else {
120
			/** This is a field with one label. */
121
			$label = $this->field->get_field_label( true, $this->label );
122
		}
123
124
		return $label;
125
	}
126
127
	/**
128
	 * Retrieve the value for this field.
129
	 *
130
	 * Requires a \GV\GF_Entry in this implementation.
131
	 *
132
	 * @param \GV\View $view The view for this context if applicable.
133
	 * @param \GV\Source $source The source (form) for this context if applicable.
134
	 * @param \GV\Entry $entry The entry for this context if applicable.
135
	 * @param \GV\Request $request The request for this context if applicable.
136
	 *
137
	 * @return mixed The value for this field.
138
	 */
139 40
	public function get_value( View $view = null, Source $source = null, Entry $entry = null, Request $request = null ) {
140 40
		if ( ! $entry || ! is_object( $entry ) || ! is_a( $entry, '\GV\GF_Entry' ) ) {
141
			gravityview()->log->error( '$entry is not a valid \GV\GF_Entry instance' );
142
			return null;
143
		}
144
145 40
		$value = \RGFormsModel::get_lead_field_value( $entry->as_entry(), $this->field );
146
		
147
		/** Apply parent filters. */
148 40
		return $this->get_value_filters( $value, $view, $source, $entry, $request );
149
	}
150
151
	/**
152
	 * A proxy getter for the backing GravityView field.
153
	 *
154
	 * The view field configuration is checked first, though.
155
	 *
156
	 * @param string $key The property to get.
157
	 *
158
	 * @return mixed The value of the Gravity View field property, or null if not exists.
159
	 */
160 53
	public function __get( $key ) {
161 53
		if ( $value = parent::__get( $key ) ) {
162 11
			return $value;
163
		}
164
165 51
		if ( $this->field ) {
166 51
			return $this->field->$key;
167
		}
168
	}
169
}
170