1 | <?php |
||
14 | class GF_Form extends Form implements \ArrayAccess { |
||
15 | |||
16 | /** |
||
17 | * @var string The identifier of the backend used for this form. |
||
18 | * @api |
||
19 | * @since future |
||
20 | */ |
||
21 | public static $backend = self::BACKEND_GRAVITYFORMS; |
||
22 | |||
23 | /** |
||
24 | * Initialization. |
||
25 | */ |
||
26 | 3 | private function __construct() { |
|
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 future |
||
39 | * @return \GV\GF_Form|null An instance of this form or null if not found. |
||
40 | */ |
||
41 | 4 | public static function by_id( $form_id ) { |
|
54 | |||
55 | /** |
||
56 | * Get all entries for this form. |
||
57 | * |
||
58 | * @api |
||
59 | * @since future |
||
60 | * |
||
61 | * @return \GV\Entry_Collection The \GV\Entry_Collection |
||
62 | */ |
||
63 | public function get_entries() { |
||
64 | $entries = new \GV\Entry_Collection(); |
||
65 | |||
66 | $form = &$this; |
||
67 | |||
68 | /** Add the fetcher lazy callback. */ |
||
69 | $entries->add_fetch_callback( function( $filters, $sorts, $offset ) use ( $form ) { |
||
70 | $entries = new \GV\Entry_Collection(); |
||
71 | |||
72 | $search_criteria = array(); |
||
73 | $sorting = array(); |
||
74 | $paging = array(); |
||
75 | |||
76 | /** Apply the filters */ |
||
77 | foreach ( $filters as $filter ) { |
||
78 | $search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() ); |
||
79 | } |
||
80 | |||
81 | /** Apply the sorts */ |
||
82 | foreach ( $sorts as $sort ) { |
||
83 | /** Gravity Forms does not have multi-sorting, so just overwrite. */ |
||
84 | $sorting = array( |
||
85 | 'key' => $sort->field->ID, |
||
86 | 'direction' => $sort->direction, |
||
87 | 'is_numeric' => $sort->mode == Entry_Sort::NUMERIC, |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | /** The offset and limit */ |
||
92 | if ( ! empty( $offset->limit ) ) { |
||
93 | $paging['page_size'] = $offset->limit; |
||
94 | } |
||
95 | |||
96 | if ( ! empty( $offset->offset ) ) { |
||
97 | $paging['offset'] = $offset->offset; |
||
98 | } |
||
99 | |||
100 | foreach ( \GFAPI::get_entries( $form->ID, $search_criteria, $sorting, $paging ) as $entry ) { |
||
101 | $entries->add( \GV\GF_Entry::from_entry( $entry ) ); |
||
102 | } |
||
103 | |||
104 | return $entries; |
||
105 | } ); |
||
106 | |||
107 | /** Add the counter lazy callback. */ |
||
108 | $entries->add_count_callback( function( $filters ) use ( $form ) { |
||
109 | |||
110 | $search_criteria = array(); |
||
111 | $sorting = array(); |
||
112 | |||
113 | /** Apply the filters */ |
||
114 | foreach ( $filters as $filter ) { |
||
115 | $search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() ); |
||
116 | } |
||
117 | |||
118 | return \GFAPI::count_entries( $form->ID, $search_criteria ); |
||
119 | } ); |
||
120 | |||
121 | return $entries; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * ArrayAccess compatibility layer with a Gravity Forms form array. |
||
126 | * |
||
127 | * @internal |
||
128 | * @deprecated |
||
129 | * @since future |
||
130 | * @return bool Whether the offset exists or not. |
||
131 | */ |
||
132 | public function offsetExists( $offset ) { |
||
135 | |||
136 | /** |
||
137 | * ArrayAccess compatibility layer with a Gravity Forms form array. |
||
138 | * |
||
139 | * Maps the old keys to the new data; |
||
140 | * |
||
141 | * @internal |
||
142 | * @deprecated |
||
143 | * @since future |
||
144 | * |
||
145 | * @return mixed The value of the requested form data. |
||
146 | */ |
||
147 | public function offsetGet( $offset ) { |
||
150 | |||
151 | /** |
||
152 | * ArrayAccess compatibility layer with a Gravity Forms form array. |
||
153 | * |
||
154 | * @internal |
||
155 | * @deprecated |
||
156 | * @since future |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | public function offsetSet( $offset, $value ) { |
||
163 | |||
164 | /** |
||
165 | * ArrayAccess compatibility layer with a Gravity Forms form array. |
||
166 | * |
||
167 | * @internal |
||
168 | * @deprecated |
||
169 | * @since future |
||
170 | * @return void |
||
171 | */ |
||
172 | public function offsetUnset( $offset ) { |
||
175 | } |
||
176 |
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.