Completed
Push — develop ( 24c11c...caac29 )
by Zack
15:31
created

Field_Collection::by_position()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 9.8666
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 12 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
 * A collection of \GV\Field objects.
11
 */
12
class Field_Collection extends Collection {
13
14
	/**
15
	 * Returns all the objects in this collection as an an array. Here for docBlock purposes only.
16
	 *
17
	 * @since 2.0.13.1
18
	 *
19
	 * @return \GV\Field[]
20
	 */
21 31
	public function all() {
22 31
		return parent::all();
23
	}
24
25
	/**
26
	 * Add a \GV\Field to this collection.
27
	 *
28
	 * @param \GV\Field $field The field to add to the internal array.
29
	 *
30
	 * @api
31
	 * @since 2.0
32
	 * @return void
33
	 */
34 72
	public function add( $field ) {
35 72
		if ( ! $field instanceof Field ) {
36 1
			gravityview()->log->error( 'Field_Collections can only contain objects of type \GV\Field.' );
37 1
			return;
38
		}
39 72
		parent::add( $field );
40 72
	}
41
42
	/**
43
	 * Get a \GV\Field from this list by UID.
44
	 *
45
	 * @param int $field_uid The UID of the field in the field to get.
46
	 *
47
	 * @api
48
	 * @since 2.0
49
	 *
50
	 * @return \GV\Field|null The \GV\Field with the $field_uid as the UID, or null if not found.
51
	 */
52 1
	public function get( $field_uid ) {
53 1
		foreach ( $this->all() as $field ) {
54 1
			if ( $field->UID == $field_uid ) {
55 1
				return $field;
56
			}
57
		}
58 1
		return null;
59
	}
60
61
	/**
62
	 * Get a copy of this \GV\Field_Collection filtered by position.
63
	 *
64
	 * @param string $position The position to get the fields for.
65
	 *  Can be a wildcard *
66
	 *
67
	 * @api
68
	 * @since
69
	 *
70
	 * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by position.
71
	 */
72 25
	public function by_position( $position ) {
73 25
		$fields = new self();
74
75 25
		$search = implode( '.*', array_map( 'preg_quote', explode( '*', $position ) ) );
76
77 25
		foreach ( $this->all() as $field ) {
78 25
			if ( preg_match( "#^{$search}$#", $field->position ) ) {
79 25
				$fields->add( $field );
80
			}
81
		}
82 25
		return $fields;
83
	}
84
85
	/**
86
	 * Get a copy of this \GV\Field_Collection filtered by visibility to current user context.
87
	 *
88
	 * @api
89
	 * @since
90
	 *
91
	 * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by visibility.
92
	 */
93 29
	public function by_visible() {
94 29
		$fields = new self();
95
96
		/** @var \GV\Field $field */
97 29
		foreach ( $this->all() as $field ) {
98 27
			if ( $field->is_visible() ) {
99 27
				$fields->add( $field );
100
			}
101
		}
102 29
		return $fields;
103
	}
104
105
	/**
106
	 * Parse a configuration array into a Field_Collection.
107
	 *
108
	 * @param array $configuration The configuration, structured like so:
109
	 *
110
	 * array(
111
	 *
112
	 * 	[other zones]
113
	 *
114
	 * 	'directory_list-title' => array(
115
	 *
116
	 *   	[other fields]
117
	 *
118
	 *  	'5372653f25d44' => array(
119
	 *			@see \GV\Field::as_configuration() for structure
120
	 *  	)
121
	 *
122
	 * 		[other fields]
123
	 *  )
124
	 *
125
	 * 	[other zones]
126
	 * )
127
	 *
128
	 * @return \GV\Field_Collection A collection of fields.
129
	 */
130 72
	public static function from_configuration( $configuration ) {
131 72
		$fields = new self();
132 72
		foreach ( $configuration as $position => $_fields ) {
133
134 72
			if ( empty( $_fields ) || ! is_array( $_fields ) ) {
135
				continue;
136
			}
137
138 72
			foreach ( $_fields as $uid => $_configuration ) {
139 72
				$field = Field::from_configuration( $_configuration );
140 72
				$field->UID = $uid;
141 72
				$field->position = $position;
142
143 72
				$fields->add( $field );
144
			}
145
		}
146 72
		return $fields;
147
	}
148
149
	/**
150
	 * Return a configuration array for this field collection.
151
	 *
152
	 * @return array See \GV\Field_Collection::from_configuration() for structure.
153
	 */
154 23
	public function as_configuration() {
155 23
		$configuration = array();
156
157
		/**
158
		 * @var \GV\Field $field
159
		 */
160 23
		foreach ( $this->all() as $field ) {
161 23
			if ( empty( $configuration[ $field->position ] ) ) {
162 23
				$configuration[ $field->position ] = array();
163
			}
164
165 23
			$configuration[ $field->position ][ $field->UID ] = $field->as_configuration();
166
		}
167 23
		return $configuration;
168
	}
169
}
170