Completed
Push — develop ( 87b48c...882b48 )
by Gennady
23:47 queued 03:49
created

Internal_Field::get_label()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 4
dl 0
loc 16
ccs 7
cts 8
cp 0.875
crap 5.0488
rs 9.4222
c 0
b 0
f 0
1
<?php
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The internal \GF_Field field object wrapper.
11
 *
12
 * Used for custom content fields, etc.
13
 */
14
class Internal_Field extends Field {
15
	/**
16
	 * @var \GravityView_Field|false The backing GravityView field (old). False if none exists.
17
	 */
18
	public $field;
19
20
	/**
21
	 * Create self from a configuration array.
22
	 *
23
	 * @param array $configuration The configuration array.
24
	 * @see \GV\Field::as_configuration()
25
	 * @internal
26
	 * @since 2.0
27
	 *
28
	 * @return \GV\Internal_Field|null The field implementation or null on error.
29
	 */
30 59
	public static function from_configuration( $configuration ) {
31
32 59
		if ( empty( $configuration['id'] ) || ! is_string( $configuration['id'] ) ) {
33
			gravityview()->log->error( 'Invalid configuration[id] supplied.' );
34
			return null;
35
		}
36
37 59
		$field = self::by_id( $configuration['id'] );
38
39 59
		$field->update_configuration( $configuration );
40
41 59
		return $field;
42
	}
43
44
	/**
45
	 * Get a \GV\GF_Field from an internal Gravity Forms field ID.
46
	 *
47
	 * @param int $field_id The internal Gravity Forms field ID.
48
	 *
49
	 * @return \GV\Internal_Field|null The requested field or null if not found.
50
	 */
51 78
	public static function by_id( $field_id ) {
52 78
		$field = new self();
53 78
		$field->ID = $field_id;
54 78
		$field->type = $field->ID;
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
56
		/**
57
		 * Retrieve the internal backing field (old for now)
58
		 * @todo switch to future subclasses
59
		 */
60 78
		$field->field = \GravityView_Fields::get_instance( $field_id );
61
62 78
		return $field;
63
	}
64
65
	/**
66
	 * Retrieve the label for this field.
67
	 *
68
	 * @param \GV\View $view The view for this context if applicable.
69
	 * @param \GV\Source $source The source (form) for this context if applicable.
70
	 * @param \GV\Entry $entry The entry for this context if applicable.
71
	 * @param \GV\Request $request The request for this context if applicable.
72
	 *
73
	 * @return string The label for this field.
74
	 */
75 27
	public function get_label( View $view = null, Source $source = null, Entry $entry = null, Request $request = null ) {
76
77 27
		if ( ! $this->show_label ) {
78
			return '';
79
		}
80
81 27
		if ( $label = parent::get_label( $view, $source, $entry, $request ) ) {
82 1
			return $label;
83
		}
84
85 26
		if ( $this->label ) {
86 22
			return $this->label;
87
		}
88
89 6
		return $this->field ? $this->field->label : '';
90
	}
91
92
	/**
93
	 * Retrieve the value for this field.
94
	 *
95
	 * Requires the \GV\Entry in this implementation.
96
	 *
97
	 * @param \GV\View $view The view for this context if applicable.
98
	 * @param \GV\Source $source The source (form) for this context if applicable.
99
	 * @param \GV\Entry $entry The entry for this context if applicable.
100
	 * @param \GV\Request $request The request for this context if applicable.
101
	 *
102
	 * @return mixed The value for this field.
103
	 */
104 60
	public function get_value( View $view = null, Source $source = null, Entry $entry = null, Request $request = null ) {
105 60
		if ( ! $entry || ! is_a( $entry, '\GV\Entry' ) ) {
106
			gravityview()->log->error( '$entry is not a valid \GV\Entry instance' );
107
			return null;
108
		}
109
110
		/**
111
		 * @todo Implement in subclasses, once available.
112
		 *
113
		 * For example the "content" field will be empty here. It's
114
		 *  value is actually currently retrieved inside ...
115
		 *
116
		 * *drumroll*
117
		 *
118
		 * A TEMPLATE :)
119
		 */
120 60
		$value = Utils::get( $entry->as_entry(), $this->ID );
121
		
122
		/** Apply parent filters. */
123 60
		return $this->get_value_filters( $value, $view, $source, $entry, $request );
124
	}
125
}
126