Completed
Push — development ( dc38d6...a6cc3f )
by Atanas
23:32 queued 14:44
created

Repository::get_unique_container_id()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.054

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 8
nop 1
dl 0
loc 31
ccs 17
cts 20
cp 0.85
crap 4.054
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container;
4
5
use Carbon_Fields\Helper\Helper;
6
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
7
8
/**
9
 * Keeps track of all instantiated containers
10
 */
11
class Repository {
12
	/**
13
	 * List of registered unique container ids
14
	 *
15
	 * @see get_unique_container_id()
16
	 * @see register_unique_container_id()
17
	 * @see unregister_unique_container_id()
18
	 * @var array
19
	 */
20
	protected $registered_container_ids = array();
21
22
	/**
23
	 * List of registered containers that should be initialized
24
	 *
25
	 * @see initialize_containers()
26
	 * @var array
27
	 */
28
	protected $pending_containers = array();
29
30
	/**
31
	 * List of all containers
32
	 *
33
	 * @see _attach()
34
	 * @var array
35
	 */
36
	protected $containers = array();
37
38
	/**
39
	 * Container id prefix
40
	 *
41
	 * @var string
42
	 */
43
	protected $container_id_prefix = 'carbon_fields_container_';
44
45
	/**
46
	 * Container id prefix
47
	 *
48
	 * @var string
49
	 */
50
	protected $widget_id_wildcard_suffix = '-__i__';
51
52
	/**
53
	 * Register a container with the repository
54
	 *
55
	 * @return array
56
	 */
57 5
	public function register_container( Container $container ) {
58 5
		$this->register_unique_container_id( $container->get_id() );
59 5
		$this->containers[] = $container;
60 5
		$this->pending_containers[] = $container;
61 5
	}
62
63
	/**
64
	 * Initialize registered containers
65
	 *
66
	 * @return array
67
	 */
68 3
	public function initialize_containers() {
69 3
		$initialized_containers = array();
70 3
		while ( ( $container = array_shift( $this->pending_containers ) ) ) {
71 3
			$container->init();
72 3
			$initialized_containers[] = $container;
73
		}
74
75 3
		return $initialized_containers;
76
	}
77
78
	/**
79
	 * Return all containers
80
	 *
81
	 * @param string $type Container type to filter for
82
	 * @return array
83
	 */
84
	public function get_containers( $type = null ) {
85
		$raw_containers = $this->containers;
86
		$containers = array();
87
88
		if ( $type === null ) {
89
			$containers = $raw_containers;
90
		} else {
91
			$normalized_type = Helper::normalize_type( $type );
92
			foreach ( $raw_containers as $container ) {
93
				if ( $container->type === $normalized_type ) {
94
					$containers[] = $container;
95
				}
96
			}
97
		}
98
99
		return $containers;
100
	}
101
102
	/**
103
	 * Return field in a container with supplied id
104
	 *
105
	 * @param  string                    $field_name
106
	 * @param  string                    $container_id
107
	 * @param  bool                      $include_nested_fields
108
	 * @return Carbon_Fields\Field\Field
109
	 */
110 View Code Duplication
	public function get_field_in_container( $field_name, $container_id, $include_nested_fields = true ) {
111
		$containers = $this->get_containers();
112
		$field = null;
113
114
		foreach ( $containers as $container ) {
115
			if ( $container->get_id() !== $container_id ) {
116
				continue;
117
			}
118
119
			if ( $include_nested_fields ) {
120
				$field = $container->get_field_by_name( $field_name );
121
			} else {
122
				$field = $container->get_root_field_by_name( $field_name );
123
			}
124
			break;
125
		}
126
127
		return $field;
128
	}
129
130
	/**
131
	 * Return field in containers
132
	 *
133
	 * @param  string                    $field_name
134
	 * @param  string                    $container_type
135
	 * @param  bool                      $include_nested_fields
136
	 * @return Carbon_Fields\Field\Field
137
	 */
138 View Code Duplication
	public function get_field_in_containers( $field_name, $container_type = null, $include_nested_fields = true ) {
139
		$containers = $this->get_containers( $container_type );
140
		$field = null;
141
142
		foreach ( $containers as $container ) {
143
			if ( $include_nested_fields ) {
144
				$field = $container->get_field_by_name( $field_name );
145
			} else {
146
				$field = $container->get_root_field_by_name( $field_name );
147
			}
148
			if ( $field ) {
149
				break;
150
			}
151
		}
152
153
		return $field;
154
	}
155
156
	/**
157
	 * Return all currently active containers
158
	 *
159
	 * @return array
160
	 */
161
	public function get_active_containers() {
162 1
		return array_filter( $this->containers, function( $container ) {
163 1
			return $container->is_active();
164 1
		} );
165
	}
166
167
	/**
168
	 * Check if container identificator id is unique
169
	 *
170
	 * @param string $id
171
	 */
172
	public function is_unique_container_id( $id ) {
173
		return ! in_array( $id, $this->registered_container_ids );
174
	}
175
176
	/**
177
	 * Generate a unique container identificator id based on container title
178
	 *
179
	 * @param string $title
180
	 */
181 3
	public function get_unique_container_id( $title ) {
182 3
		$id = remove_accents( $title );
183 3
		$id = strtolower( $id );
184
185 3
		$id_prefix = $this->container_id_prefix;
186 3
		if ( substr( $id, 0, strlen( $id_prefix ) ) === $id_prefix ) {
187
			$id_prefix = '';
188
		}
189
190 3
		$wids = $this->widget_id_wildcard_suffix;
191 3
		$id_suffix = '';
192 3
		if ( substr( $id, -strlen( $wids ) ) === $wids ) {
193
			$id_suffix = $wids;
194
			$id = substr( $id, 0, -strlen( $wids ) );
195
		}
196
		
197 3
		$id = preg_replace( '~[\s]+~', '_', $id );
198 3
		$id = preg_replace( '~[^\w\-\_]+~', '', $id );
199
200 3
		$id = $id_prefix . $id . $id_suffix;
201
202 3
		$base = $id;
203 3
		$suffix = 0;
204
205 3
		while ( ! $this->is_unique_container_id( $id ) ) {
206 1
			$suffix++;
207 1
			$id = $base . strval( $suffix );
208
		}
209
210 3
		return $id;
211
	}
212
213
	/**
214
	 * Add container identificator id to the list of unique container ids
215
	 *
216
	 * @param string $id
217
	 */
218
	protected function register_unique_container_id( $id ) {
219
		if ( $this->is_unique_container_id( $id ) ) {
220
			$this->registered_container_ids[] = $id;
221
		}
222
	}
223
224
	/**
225
	 * Remove container identificator id from the list of unique container ids
226
	 *
227
	 * @param string $id
228
	 */
229
	protected function unregister_unique_container_id( $id ) {
230
		if ( ! $this->is_unique_container_id( $id ) ) {
231
			unset( $this->registered_container_ids[ array_search( $id, $this->registered_container_ids ) ] );
232
		}
233
	}
234
}
235