Completed
Pull Request — develop (#1487)
by Zack
07:56
created

Field_Collection::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
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 111
	public function all() {
22 111
		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 183
	public function add( $field ) {
35 183
		if ( ! $field instanceof Field ) {
36 1
			gravityview()->log->error( 'Field_Collections can only contain objects of type \GV\Field.' );
37 1
			return;
38
		}
39 183
		parent::add( $field );
40 183
	}
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 2
	public function get( $field_uid ) {
53 2
		foreach ( $this->all() as $field ) {
54 2
			if ( $field->UID == $field_uid ) {
55 2
				return $field;
56
			}
57
		}
58 1
		return null;
59
	}
60
61
	/**
62
	 * Get a copy of this \GV\Field_Collection filtered by type.
63
	 *
64
	 * @param string $type The type of the field to get.
65
	 *  Can be a wildcard *
66
	 *
67
	 * @api
68
	 * @since develop
69
	 *
70
	 * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by type.
71
	 */
72 75
	public function by_type( $type ) {
73 75
		$fields = new self();
74
75
		// Turn a wildcard into a regular expression.
76 75
		$search = implode( '.*', array_map( 'preg_quote', explode( '*', $type ) ) );
77
78 75
		foreach ( $this->all() as $field ) {
79
			// Only add fields for which the type matches the regular expression.
80 75
			if ( preg_match( "#^{$search}$#", $field->type ) ) {
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<GV\Field>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
81 9
				$fields->add( $field );
82
			}
83
		}
84 75
		return $fields;
85
	}
86
87
	/**
88
	 * Get a copy of this \GV\Field_Collection filtered by position.
89
	 *
90
	 * @param string $position The position to get the fields for.
91
	 *  Can be a wildcard *
92
	 *
93
	 * @api
94
	 * @since
95
	 *
96
	 * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by position.
97
	 */
98 49
	public function by_position( $position ) {
99 49
		$fields = new self();
100
101
		// Turn a wildcard into a regular expression.
102 49
		$search = implode( '.*', array_map( 'preg_quote', explode( '*', $position ) ) );
103
104 49
		foreach ( $this->all() as $field ) {
105
			// Only add fields for which the position matches the regular expression.
106 49
			if ( preg_match( "#^{$search}$#", $field->position ) ) {
107 47
				$fields->add( $field );
108
			}
109
		}
110 49
		return $fields;
111
	}
112
113
	/**
114
	 * Get a copy of this \GV\Field_Collection filtered by visibility to current user context.
115
	 *
116
	 * @api
117
	 * @since
118
	 *
119
	 * @param $view \GV\View The view!
120
	 *
121
	 * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by visibility.
122
	 */
123 75
	public function by_visible( $view = null ) {
124 75
		$fields = new self();
125
126
		/** @type \GV\Field $field */
127 75
		foreach ( $this->all() as $field ) {
128 72
			if ( $field->is_visible( $view ) ) {
129 72
				$fields->add( $field );
130
			}
131
		}
132 75
		return $fields;
133
	}
134
135
	/**
136
	 * Parse a configuration array into a Field_Collection.
137
	 *
138
	 * @param array $configuration The configuration, structured like so:
139
	 *
140
	 * array(
141
	 *
142
	 * 	[other zones]
143
	 *
144
	 * 	'directory_list-title' => array(
145
	 *
146
	 *   	[other fields]
147
	 *
148
	 *  	'5372653f25d44' => array(
149
	 *			@see \GV\Field::as_configuration() for structure
150
	 *  	)
151
	 *
152
	 * 		[other fields]
153
	 *  )
154
	 *
155
	 * 	[other zones]
156
	 * )
157
	 *
158
	 * @return \GV\Field_Collection A collection of fields.
159
	 */
160 184
	public static function from_configuration( $configuration ) {
161 184
		$fields = new self();
162 184
		foreach ( $configuration as $position => $_fields ) {
163
164 183
			if ( empty( $_fields ) || ! is_array( $_fields ) ) {
165
				continue;
166
			}
167
168 183
			foreach ( $_fields as $uid => $_configuration ) {
169 183
				$field = Field::from_configuration( $_configuration );
170 183
				$field->UID = $uid;
171 183
				$field->position = $position;
172
173 183
				$fields->add( $field );
174
			}
175
		}
176 184
		return $fields;
177
	}
178
179
	/**
180
	 * Return a configuration array for this field collection.
181
	 *
182
	 * @return array See \GV\Field_Collection::from_configuration() for structure.
183
	 */
184 55
	public function as_configuration() {
185 55
		$configuration = array();
186
187
		/**
188
		 * @var \GV\Field $field
189
		 */
190 55
		foreach ( $this->all() as $field ) {
191 54
			if ( empty( $configuration[ $field->position ] ) ) {
192 54
				$configuration[ $field->position ] = array();
193
			}
194
195 54
			$configuration[ $field->position ][ $field->UID ] = $field->as_configuration();
196
		}
197 55
		return $configuration;
198
	}
199
}
200