Completed
Push — develop ( d6e54c...488d60 )
by Zack
17:10
created

Widget_Collection::from_configuration()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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