Completed
Push — test/legacy-storage-service ( 13c02e...9939d7 )
by
unknown
02:29
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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container;
4
5
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
6
7
/**
8
 * Keeps track of all instantiated containers
9
 */
10
class Repository {
11
	/**
12
	 * List of registered unique panel identificator ids
13
	 *
14
	 * @see get_unique_panel_id()
15
	 * @see register_unique_panel_id()
16
	 * @see unregister_unique_panel_id()
17
	 * @var array
18
	 */
19
	protected $registered_panel_ids = array();
20
21
	/**
22
	 * List of registered containers that should be initialized
23
	 *
24
	 * @see initialize_containers()
25
	 * @var array
26
	 */
27
	protected $pending_containers = array();
28
29
	/**
30
	 * List of all containers
31
	 *
32
	 * @see _attach()
33
	 * @var array
34
	 */
35
	protected $containers = array();
36
37
	/**
38
	 * Register a container with the repository
39
	 *
40
	 * @return array
41
	 **/
42 5
	public function register_container( Container $container ) {
43 5
		$this->register_unique_panel_id( $container->id );
44 5
		$this->containers[] = $container;
45 5
		$this->pending_containers[] = $container;
46 5
	}
47
48
	/**
49
	 * Initialize registered containers
50
	 *
51
	 * @return array
52
	 **/
53 3
	public function initialize_containers() {
54 3
		$initialized_containers = array();
55 3
		while ( ( $container = array_shift( $this->pending_containers ) ) ) {
56 3
			$container->init();
57 3
			$initialized_containers[] = $container;
58 3
		}
59
60 3
		return $initialized_containers;
61
	}
62
63
	/**
64
	 * Return all containers
65
	 *
66
	 * @param string $type Container type to filter for
67
	 * @return array
68
	 **/
69
	public function get_containers( $type = null ) {
70
		$raw_containers = $this->containers;
71
		$containers = array();
72
73
		if ( $type === null ) {
74
			$containers = $raw_containers;
75
		} else {
76
			foreach ( $raw_containers as $container ) {
77
				if ( $container->type === $type ) {
78
					$containers[] = $container;
79
				}
80
			}
81
		}
82
83
		return $containers;
84
	}
85
86
	/**
87
	 * Return field in containers
88
	 *
89
	 * @param string $field_name
90
	 * @param string $container_type Container type to filter for
91
	 * @param bool $include_nested_fields Search in nested fields as well
92
	 * @return Carbon_Fields\Field\Field
93
	 **/
94
	public function get_field_in_containers( $field_name, $container_type = null, $include_nested_fields = true ) {
95
		$containers = $this->get_containers( $container_type );
96
		$field = null;
97
98
		foreach ( $containers as $container ) {
99
			if ( $include_nested_fields ) {
100
				$field = $container->get_field_by_name( $field_name );
101
			} else {
102
				$field = $container->get_root_field_by_name( $field_name );
103
			}
104
			if ( $field ) {
105
				break;
106
			}
107
		}
108
109
		return $field;
110
	}
111
112
	/**
113
	 * Return all currently active containers
114
	 *
115
	 * @return array
116
	 **/
117
	public function get_active_containers() {
118 1
		return array_filter( $this->containers, function( $container ) {
119 1
			return $container->active();
120 1
		} );
121
	}
122
123
	/**
124
	 * Generate a unique container identificator id based on container title
125
	 * 
126
	 * @param string $title
127
	 */
128 3
	public function get_unique_panel_id( $title ) {
129 3
		$id = preg_replace( '~[^\w\-]*~', '', remove_accents( $title ) );
130 3
		$base = $id;
131 3
		$suffix = 0;
132
133 3
		while ( ! $this->is_unique_panel_id( $id ) ) {
134 2
			$suffix++;
135 2
			$id = $base . strval( $suffix );
136 2
		}
137
138 3
		return $id;
139
	}
140
141
	/**
142
	 * Check if container identificator id is unique
143
	 * 
144
	 * @param string $id
145
	 */
146
	protected function is_unique_panel_id( $id ) {
147
		return ! in_array( $id, $this->registered_panel_ids );
148
	}
149
150
	/**
151
	 * Add container identificator id to the list of unique container ids
152
	 *
153
	 * @param string $id
154
	 **/
155
	protected function register_unique_panel_id( $id ) {
156
		if ( $this->is_unique_panel_id( $id ) ) {
157
			$this->registered_panel_ids[] = $id;
158
		}
159
	}
160
161
	/**
162
	 * Remove container identificator id from the list of unique container ids
163
	 *
164
	 * @param string $id
165
	 **/
166
	protected function unregister_unique_panel_id( $id ) {
167
		if ( ! $this->is_unique_panel_id( $id ) ) {
168
			unset( $this->registered_panel_ids[ array_search( $id, $this->registered_panel_ids ) ] );
169
		}
170
	}
171
}
172