Completed
Push — master ( 397f5b...27d97b )
by Zack
27:24 queued 14:56
created

Internal_Field   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 76%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 107
ccs 19
cts 25
cp 0.76
rs 10
c 1
b 0
f 0
wmc 11
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A by_id() 0 13 1
A get_value() 0 21 3
A get_label() 0 11 4
A from_configuration() 0 13 3
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 14 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 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 19
	public static function from_configuration( $configuration ) {
31
32 19
		if ( empty( $configuration['id'] ) || ! is_string( $configuration['id'] ) ) {
33
			gravityview()->log->error( 'Invalid configuration[id] supplied.' );
34
			return null;
35
		}
36
37 19
		$field = self::by_id( $configuration['id'] );
38
39 19
		$field->update_configuration( $configuration );
40
41 19
		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 33
	public static function by_id( $field_id ) {
52 33
		$field = new self();
53 33
		$field->ID = $field_id;
54 33
		$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 33
		$field->field = \GravityView_Fields::get_instance( $field_id );
61
62 33
		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. Nothing here.
74
	 */
75 12
	public function get_label( View $view = null, Source $source = null, Entry $entry = null, Request $request = null ) {
76 12
		if ( $label = parent::get_label( $view, $source, $entry, $request ) ) {
77
			return $label;
78
		}
79
80 12
		if ( ! $this->show_label ) {
81
			return '';
82
		}
83
84 12
		return $this->field ? $this->field->label : $this->label;
85
	}
86
87
	/**
88
	 * Retrieve the value for this field.
89
	 *
90
	 * Requires the \GV\Entry in this implementation.
91
	 *
92
	 * @param \GV\View $view The view for this context if applicable.
93
	 * @param \GV\Source $source The source (form) for this context if applicable.
94
	 * @param \GV\Entry $entry The entry for this context if applicable.
95
	 * @param \GV\Request $request The request for this context if applicable.
96
	 *
97
	 * @return mixed The value for this field.
98
	 */
99 26
	public function get_value( View $view = null, Source $source = null, Entry $entry = null, Request $request = null ) {
100 26
		if ( ! $entry || ! is_a( $entry, '\GV\Entry' ) ) {
101
			gravityview()->log->error( '$entry is not a valid \GV\Entry instance' );
102
			return null;
103
		}
104
105
		/**
106
		 * @todo Implement in subclasses, once available.
107
		 *
108
		 * For example the "content" field will be empty here. It's
109
		 *  value is actually currently retrieved inside ...
110
		 *
111
		 * *drumroll*
112
		 *
113
		 * A TEMPLATE :)
114
		 */
115 26
		$value = Utils::get( $entry->as_entry(), $this->ID );
116
		
117
		/** Apply parent filters. */
118 26
		return $this->get_value_filters( $value, $view, $source, $entry, $request );
119
	}
120
}
121