Completed
Push — milestone/2_0/react-ui ( df5e70...90d344 )
by
unknown
03:02
created

Repository::get_active_containers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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
	 * Register a container with the repository
40
	 *
41
	 * @return array
42
	 */
43 5
	public function register_container( Container $container ) {
44 5
		$this->register_unique_container_id( $container->id );
45 5
		$this->containers[] = $container;
46 5
		$this->pending_containers[] = $container;
47 5
	}
48
49
	/**
50
	 * Initialize registered containers
51
	 *
52
	 * @return array
53
	 */
54 3
	public function initialize_containers() {
55 3
		$initialized_containers = array();
56 3
		while ( ( $container = array_shift( $this->pending_containers ) ) ) {
57 3
			$container->init();
58 3
			$initialized_containers[] = $container;
59 3
		}
60
61 3
		return $initialized_containers;
62
	}
63
64
	/**
65
	 * Return all containers
66
	 *
67
	 * @param string $type Container type to filter for
68
	 * @return array
69
	 */
70
	public function get_containers( $type = null ) {
71
		$raw_containers = $this->containers;
72
		$containers = array();
73
74
		if ( $type === null ) {
75
			$containers = $raw_containers;
76
		} else {
77
			$normalized_type = Helper::normalize_type( $type );
78
			foreach ( $raw_containers as $container ) {
79
				if ( $container->type === $normalized_type ) {
80
					$containers[] = $container;
81
				}
82
			}
83
		}
84
85
		return $containers;
86
	}
87
88
	/**
89
	 * Return field in a container with supplied id
90
	 *
91
	 * @param  string                    $field_name
92
	 * @param  string                    $container_id
93
	 * @param  bool                      $include_nested_fields
94
	 * @return Carbon_Fields\Field\Field
95
	 */
96 View Code Duplication
	public function get_field_in_container( $field_name, $container_id, $include_nested_fields = true ) {
97
		$containers = $this->get_containers();
98
		$field = null;
99
100
		foreach ( $containers as $container ) {
101
			if ( $container->get_id() !== $container_id ) {
102
				continue;
103
			}
104
105
			if ( $include_nested_fields ) {
106
				$field = $container->get_field_by_name( $field_name );
107
			} else {
108
				$field = $container->get_root_field_by_name( $field_name );
109
			}
110
			break;
111
		}
112
113
		return $field;
114
	}
115
116
	/**
117
	 * Return field in containers
118
	 *
119
	 * @param  string                    $field_name
120
	 * @param  string                    $container_type
121
	 * @param  bool                      $include_nested_fields
122
	 * @return Carbon_Fields\Field\Field
123
	 */
124 View Code Duplication
	public function get_field_in_containers( $field_name, $container_type = null, $include_nested_fields = true ) {
125
		$containers = $this->get_containers( $container_type );
126
		$field = null;
127
128
		foreach ( $containers as $container ) {
129
			if ( $include_nested_fields ) {
130
				$field = $container->get_field_by_name( $field_name );
131
			} else {
132
				$field = $container->get_root_field_by_name( $field_name );
133
			}
134
			if ( $field ) {
135
				break;
136
			}
137
		}
138
139
		return $field;
140
	}
141
142
	/**
143
	 * Return all currently active containers
144
	 *
145
	 * @return array
146
	 */
147
	public function get_active_containers() {
148 1
		return array_filter( $this->containers, function( $container ) {
149 1
			return $container->is_active();
150 1
		} );
151
	}
152
153
	/**
154
	 * Check if container identificator id is unique
155
	 * 
156
	 * @param string $id
157
	 */
158
	public function is_unique_container_id( $id ) {
159
		return ! in_array( $id, $this->registered_container_ids );
160
	}
161
162
	/**
163
	 * Generate a unique container identificator id based on container title
164
	 * 
165
	 * @param string $title
166
	 */
167 3
	public function get_unique_container_id( $title ) {
168 3
		$id = remove_accents( $title );
169 3
		$id = strtolower( $id );
170 3
		$id = preg_replace( '~[\-\s]+~', '_', $id );
171 3
		$id = preg_replace( '~[^\w\_]+~', '', $id );
172 3
		$id = preg_replace( '~_{2,}~', '_', $id );
173 3
		$id = 'carbon_fields_container_' . $id;
174 3
		$base = $id;
175 3
		$suffix = 0;
176
177 3
		while ( ! $this->is_unique_container_id( $id ) ) {
178 2
			$suffix++;
179 2
			$id = $base . strval( $suffix );
180 2
		}
181
182 3
		return $id;
183
	}
184
185
	/**
186
	 * Add container identificator id to the list of unique container ids
187
	 *
188
	 * @param string $id
189
	 */
190
	protected function register_unique_container_id( $id ) {
191
		if ( $this->is_unique_container_id( $id ) ) {
192
			$this->registered_container_ids[] = $id;
193
		}
194
	}
195
196
	/**
197
	 * Remove container identificator id from the list of unique container ids
198
	 *
199
	 * @param string $id
200
	 */
201
	protected function unregister_unique_container_id( $id ) {
202
		if ( ! $this->is_unique_container_id( $id ) ) {
203
			unset( $this->registered_container_ids[ array_search( $id, $this->registered_container_ids ) ] );
204
		}
205
	}
206
}
207