Completed
Push — master ( af77e8...7d9d07 )
by Zack
11s
created

Field_Collection::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
c 1
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
	 * Add a \GV\Field to this collection.
15
	 *
16
	 * @param \GV\Field $field The field to add to the internal array.
17
	 *
18
	 * @throws \InvalidArgumentException if $field is not of type \GV\Field.
19
	 *
20
	 * @api
21
	 * @since future
22
	 * @return void
23
	 */
24 3
	public function add( $field ) {
25 3
		if ( ! $field instanceof Field ) {
26 1
			throw new \InvalidArgumentException( 'Field_Collections can only contain objects of type \GV\Field.' );
27
		}
28 3
		parent::add( $field );
29 3
	}
30
31
	/**
32
	 * Get a \GV\Field from this list by UID.
33
	 *
34
	 * @param int $field_uid The UID of the field in the field to get.
35
	 *
36
	 * @api
37
	 * @since future
38
	 *
39
	 * @return \GV\Field|null The \GV\Field with the $field_uid as the UID, or null if not found.
40
	 */
41 1
	public function get( $field_uid ) {
42 1
		foreach ( $this->all() as $field ) {
43 1
			if ( $field->UID == $field_uid ) {
44 1
				return $field;
45
			}
46
		}
47 1
		return null;
48
	}
49
50
	/**
51
	 * Get a copy of this \GV\Field_Collection filtered by position.
52
	 *
53
	 * @param string $position The position to get the fields for.
54
	 *
55
	 * @api
56
	 * @since
57
	 *
58
	 * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by position.
59
	 */
60 1
	public function by_position( $position ) {
61 1
		$fields = new self();
62 1
		foreach ( $this->all() as $field ) {
63 1
			if ( $field->position == $position ) {
64 1
				$fields->add( $field );
65
			}
66
		}
67 1
		return $fields;
68
	}
69
70
	/**
71
	 * Get a copy of this \GV\Field_Collection filtered by visibility to current user context.
72
	 *
73
	 * @api
74
	 * @since
75
	 *
76
	 * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by visibility.
77
	 */
78 1
	public function by_visible() {
79 1
		$fields = new self();
80
81 1
		foreach ( $this->all() as $field ) {
82 1
			if ( ! $field->cap || \GVCommon::has_cap( $field->cap ) ) {
83 1
				$fields->add( $field );
84
			}
85
		}
86 1
		return $fields;
87
	}
88
89
	/**
90
	 * Parse a configuration array into a Field_Collection.
91
	 *
92
	 * @param array $configuration The configuration, structured like so:
93
	 *
94
	 * array(
95
	 *
96
	 * 	[other zones]
97
	 *
98
	 * 	'directory_list-title' => array(
99
	 *
100
	 *   	[other fields]
101
	 *
102
	 *  	'5372653f25d44' => array(
103
	 *			@see \GV\Field::as_configuration() for structure
104
	 *  	)
105
	 *
106
	 * 		[other fields]
107
	 *  )
108
	 *
109
	 * 	[other zones]
110
	 * )
111
	 *
112
	 * @return \GV\Field_Collection A collection of fields.
113
	 */
114 3
	public static function from_configuration( $configuration ) {
115 3
		$fields = new self();
116 3
		foreach ( $configuration as $position => $_fields ) {
117 3
			foreach ( $_fields as $uid => $_field ) {
118 3
				$field = new \GV\Field();
119 3
				$field->UID = $uid;
120 3
				$field->position = $position;
121 3
				$field->from_configuration( $_field );
122
123 3
				$fields->add( $field );
124
			}
125
		}
126 3
		return $fields;
127
	}
128
129
	/**
130
	 * Return a configuration array for this field collection.
131
	 *
132
	 * @return array See \GV\Field_Collection::from_configuration() for structure.
133
	 */
134 3
	public function as_configuration() {
135 3
		$configuration = array();
136 3
		foreach ( $this->all() as $field ) {
137 3
			if ( empty( $configuration[ $field->position ] ) ) {
138 3
				$configuration[ $field->position ] = array();
139
			}
140
141 3
			$configuration[ $field->position ][ $field->UID ] = $field->as_configuration();
142
		}
143 3
		return $configuration;
144
	}
145
}
146