Completed
Push — master ( e57dae...fd214a )
by Zack
14s
created

GF_Entry::from_entry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
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 12
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
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 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 Entry class implementation.
11
 *
12
 * Accessible as an array for back-compatibility.
13
 */
14
class GF_Entry extends Entry 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 entry.
18
	 * @api
19
	 * @since future
20
	 */
21
	public static $backend = 'gravityforms';
22
23
	/**
24
	 * Initialization.
25
	 */
26
	private function __construct() {
27
		if ( ! class_exists( 'GFAPI' ) ) {
28
			gravityview()->log->error( 'Gravity Forms plugin not active.' );
29
		}
30
	}
31
32
	/**
33
	 * Construct a \GV\Entry instance by ID.
34
	 *
35
	 * @param int|string $entry_id The internal entry ID.
36
	 *
37
	 * @api
38
	 * @since future
39
	 * @return \GV\Entry|null An instance of this entry or null if not found.
40
	 */
41
	public static function by_id( $entry_id ) {
42
		$entry = \GFAPI::get_entry( $entry_id );
43
		if ( !$entry ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
44
			return null;
45
		}
46
47
		return self::from_entry( $entry );
48
	}
49
50
	/**
51
	 * Construct a \GV\Entry instance from a Gravity Forms entry array.
52
	 *
53
	 * @param array $entry The array ID.
54
	 *
55
	 * @return \GV\Entry|null An instance of this entry or null if not found.
56
	 */
57
	public static function from_entry( $entry ) {
58
		if ( empty( $entry['id'] ) ) {
59
			return null;
60
		}
61
62
		$self = new self();
63
		$self->entry = $entry;
64
65
		$self->ID = $self->entry['id'];
66
67
		return $self;
68
	}
69
70
	/**
71
	 * ArrayAccess compatibility layer with a Gravity Forms entry array.
72
	 *
73
	 * @internal
74
	 * @deprecated
75
	 * @since future
76
	 * @return bool Whether the offset exists or not.
77
	 */
78
	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...
79
		return isset( $this->entry[$offset] );
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
80
	}
81
82
	/**
83
	 * ArrayAccess compatibility layer with a Gravity Forms entry array.
84
	 *
85
	 * Maps the old keys to the new data;
86
	 *
87
	 * @internal
88
	 * @deprecated
89
	 * @since future
90
	 *
91
	 * @return mixed The value of the requested entry data.
92
	 */
93
	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...
94
		return $this->entry[$offset];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
95
	}
96
97
	/**
98
	 * ArrayAccess compatibility layer with a Gravity Forms entry array.
99
	 *
100
	 * @internal
101
	 * @deprecated
102
	 * @since future
103
	 *
104
	 * @return void
105
	 */
106
	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...
107
		gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' );
108
	}
109
110
	/**
111
	 * ArrayAccess compatibility layer with a Gravity Forms entry array.
112
	 *
113
	 * @internal
114
	 * @deprecated
115
	 * @since future
116
	 * @return void
117
	 */
118
	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...
119
		gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' );
120
	}
121
}
122