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

GF_Form::from_form()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 7
cp 0
crap 6
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 Gravity Forms Form class implementation.
11
 *
12
 * Accessible as an array for back-compatibility.
13
 */
14
class GF_Form extends Form implements \ArrayAccess {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
15
16
	/**
17
	 * @var string The identifier of the backend used for this form.
18
	 * @api
19
	 * @since 2.0
20
	 */
21
	public static $backend = self::BACKEND_GRAVITYFORMS;
22
23
	/**
24
	 * Initialization.
25
	 */
26 71
	private function __construct() {
27 71
		if ( ! class_exists( 'GFAPI' ) ) {
28
			gravityview()->log->error( 'Gravity Forms plugin is not active.' );
29
		}
30 71
	}
31
32
	/**
33
	 * Construct a \GV\GF_Form instance by ID.
34
	 *
35
	 * @param int|string $form_id The internal form ID.
36
	 *
37
	 * @api
38
	 * @since 2.0
39
	 * @return \GV\GF_Form|null An instance of this form or null if not found.
40
	 */
41 72
	public static function by_id( $form_id ) {
42
43 72
		$form = wp_cache_get( 'gf_form_' . $form_id, 'gravityview' );
44
45 72
		if ( ! $form ) {
46 72
			$form = \GFAPI::get_form( $form_id );
47
		}
48
49 72
		if ( ! $form ) {
50 1
			return null;
51
		}
52
53 72
		wp_cache_set( 'gf_form_' . $form_id, $form, 'gravityview' );
54
55 72
		$self = new self();
56 72
		$self->form = $form;
0 ignored issues
show
Bug introduced by
The property form cannot be accessed from this context as it is declared private in class GV\Form.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
57
58 72
		$self->ID = $self->form['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...
59
60 72
		return $self;
61
	}
62
63
	/**
64
	 * Construct a \GV\Form instance from a Gravity Forms form array.
65
	 *
66
	 * @since 2.0.7
67
	 *
68
	 * @param array $form The form array
69
	 *
70
	 * @return \GV\GF_Form|null An instance of this form or null if not found.
71
	 */
72
	public static function from_form( $form ) {
73
		if ( empty( $form['id'] ) ) {
74
			return null;
75
		}
76
77
		$self = new self();
78
		$self->form = $form;
0 ignored issues
show
Bug introduced by
The property form cannot be accessed from this context as it is declared private in class GV\Form.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
79
		$self->ID = $self->form['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...
80
81
		return $self;
82
	}
83
84
	/**
85
	 * Get all entries for this form.
86
	 *
87
	 * @api
88
	 * @since 2.0
89
	 *
90
	 * @return \GV\Entry_Collection The \GV\Entry_Collection
91
	 */
92 1
	public function get_entries() {
93 1
		$entries = new \GV\Entry_Collection();
94
95 1
		$form = &$this;
96
97
		/** Add the fetcher lazy callback. */
98 1
		$entries->add_fetch_callback( function( $filters, $sorts, $offset ) use ( $form ) {
99 1
			$entries = new \GV\Entry_Collection();
100
101 1
			$search_criteria = array();
102 1
			$sorting = array();
103 1
			$paging = array();
104
105
			/** Apply the filters */
106 1
			foreach ( $filters as $filter ) {
107 1
				$search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() );
108
			}
109
110
			/** Apply the sorts */
111 1
			foreach ( $sorts as $sort ) {
112
				/** Gravity Forms does not have multi-sorting, so just overwrite. */
113
				$sorting = array(
114
					'key' => $sort->field->ID,
115
					'direction' => $sort->direction,
116
					'is_numeric' => $sort->mode == Entry_Sort::NUMERIC,
117
				);
118
			}
119
120
			/** The offset and limit */
121 1
			if ( ! empty( $offset->limit ) ) {
122 1
				$paging['page_size'] = $offset->limit;
123
			}
124
125 1
			if ( ! empty( $offset->offset ) ) {
126
				$paging['offset'] = $offset->offset;
127
			}
128
129 1
			foreach ( \GFAPI::get_entries( $form->ID, $search_criteria, $sorting, $paging ) as $entry ) {
130 1
				$entries->add( \GV\GF_Entry::from_entry( $entry ) );
131
			}
132
133 1
			return $entries;
134 1
		} );
135
136
		/** Add the counter lazy callback. */
137 1
		$entries->add_count_callback( function( $filters ) use ( $form ) {
138
			$search_criteria = array();
139
			$sorting = array();
140
141
			/** Apply the filters */
142
			/** @var \GV\GF_Entry_Filter|\GV\Entry_Filter $filter */
143
			foreach ( $filters as $filter ) {
144
				$search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() );
145
			}
146
147
			return \GFAPI::count_entries( $form->ID, $search_criteria );
148 1
		} );
149
150 1
		return $entries;
151
	}
152
153
	/**
154
	 * Get a \GV\Field by Form and Field ID for this data source.
155
	 *
156
	 * @param \GV\GF_Form $form The Gravity Form form ID.
0 ignored issues
show
Bug introduced by
There is no parameter named $form. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
157
	 * @param int $field_id The Gravity Form field ID for the $form_id.
0 ignored issues
show
Bug introduced by
There is no parameter named $field_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
158
	 *
159
	 * @return \GV\Field|null The requested field or null if not found.
160
	 */
161 3
	public static function get_field( /** varargs */ ) {
162 3
		$args = func_get_args();
163
164 3
		if ( ! is_array( $args ) || count( $args ) != 2 ) {
0 ignored issues
show
introduced by
Found "!= 2". Use Yoda Condition checks, you must
Loading history...
165 1
			gravityview()->log->error( '{source} expects 2 arguments for ::get_field ($form, $field_id)', array( 'source' => __CLASS__ ) );
166 1
			return null;
167
		}
168
169
		/** Unwrap the arguments. */
170 3
		list( $form, $field_id ) = $args;
171
172
		/** Wrap it up into a \GV\Field. */
173 3
		return GF_Field::by_id( $form, $field_id );
174
	}
175
176
	/**
177
	 * Get an array of GV Fields for this data source
178
	 *
179
	 * @return \GV\Field[]|array Empty array if no fields
180
	 */
181
	public function get_fields() {
182
		$fields = array();
183
		foreach ( $this['fields'] as $field ) {
184
			foreach ( empty( $field['inputs'] ) ? array( $field['id'] ) : wp_list_pluck( $field['inputs'], 'id' ) as $id ) {
185
				if ( is_numeric( $id ) ) {
186
					$fields[ $id ] = self::get_field( $this, $id );
187
				} else {
188
					$fields[ $id ] = Internal_Field::by_id( $id );
189
				}
190
			}
191
		}
192
193
		return array_filter( $fields );
194
	}
195
196
	/**
197
	 * Proxies.
198
	 *
199
	 * @param string $key The property to get.
200
	 *
201
	 * @return mixed
202
	 */
203 1
	public function __get( $key ) {
204 1
		switch ( $key ) {
205 1
			case 'fields':
206
				return $this->get_fields();
207
			default:
208 1
				return parent::__get( $key );
209
		}
210
	}
211
212
	/**
213
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
214
	 *
215
	 * @internal
216
	 * @deprecated
217
	 * @since 2.0
218
	 * @return bool Whether the offset exists or not.
219
	 */
220 15
	public function offsetExists( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetExists is in camel caps, but expected offset_exists instead as per the coding standard.
Loading history...
221 15
		return isset( $this->form[$offset] );
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...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
222
	}
223
224
	/**
225
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
226
	 *
227
	 * Maps the old keys to the new data;
228
	 *
229
	 * @internal
230
	 * @deprecated
231
	 * @since 2.0
232
	 *
233
	 * @return mixed The value of the requested form data.
234
	 */
235 11
	public function offsetGet( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetGet is in camel caps, but expected offset_get instead as per the coding standard.
Loading history...
236 11
		return $this->form[$offset];
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...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
237
	}
238
239
	/**
240
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
241
	 *
242
	 * @internal
243
	 * @deprecated
244
	 * @since 2.0
245
	 *
246
	 * @return void
247
	 */
248
	public function offsetSet( $offset, $value ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetSet is in camel caps, but expected offset_set instead as per the coding standard.
Loading history...
249
		gravityview()->log->error( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' );
250
	}
251
252
	/**
253
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
254
	 *
255
	 * @internal
256
	 * @deprecated
257
	 * @since 2.0
258
	 * @return void
259
	 */
260
	public function offsetUnset( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetUnset is in camel caps, but expected offset_unset instead as per the coding standard.
Loading history...
261
		gravityview()->log->error( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' );
262
	}
263
}
264