Completed
Pull Request — master (#193)
by
unknown
09:54
created

Data_Manager::should_load_field()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php 
2
namespace Carbon_Fields\REST;
3
4
use Carbon_Fields\Container\Container;
5
6
/**
7
 * Class for retrieving relative data for REST responses
8
 */
9
class Data_Manager {
10
	
11
	/**
12
	 * Special field types, that require 
13
	 * different data loading
14
	 * 
15
	 * @var array
16
	 */
17
	public $special_field_types = array(
18
		'complex',
19
		'relationship',
20
		'association',
21
		'map'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
22
	); 
23
24
	/**
25
	 * Field types that should be excluded
26
	 * from the REST response
27
	 * 
28
	 * @var array
29
	 */
30
	protected $exclude_field_types = array(
31
		'html',
32
		'separator',
33
	);
34
35
	/**
36
	 * Instance of the Container_Validator class
37
	 * 
38
	 * @var object
39
	 */
40
	public $container_validator;
41
42
	public function __construct( $validator ) {
43
		$this->container_validator = $validator;
44
	}
45
46
	/**
47
	 * Returns the Carbon Fields data based
48
	 * on $type and $id
49
	 * 
50
	 * @param  string $type 
51
	 * @param  string $id 
52
	 * @return array
53
	 */
54
	public function get_data( $type, $id  = '' ) {
55
		$response   = array();
56
		$containers = $this->filter_containers( $type, $id );
57
58
		foreach ( $containers as $container ) {
59
			$fields = $this->filter_fields( $container->get_fields() );
60
61
			foreach ( $fields as $field ) {
62
				
63
				if ( $id ) {
64
					$field->get_datastore()->set_id( $id );
65
				}
66
67
				$field->load();
68
69
				$field_type = in_array( strtolower( $field->type ), $this->special_field_types ) ? strtolower( $field->type ) : 'generic';
70
71
				$response[ $field->get_name() ] = call_user_func( array( $this, "load_{$field_type}_field_value" ), $field );
72
			}
73
		}
74
75
		return $response;
76
	}
77
78
	/**
79
	 * Filters all available containers 
80
	 * based on $type
81
	 * 
82
	 * @param  string $type 
83
	 * @param  string $id
84
	 * @return array
85
	 */
86
	public function filter_containers( $type, $id = '' ) {
87
		return array_filter( Container::$active_containers, function( $container ) use ( $type, $id ) {
88
			return $this->container_validator->is_valid_container( $container, $type, $id );
89
		} );
90
	}
91
92
	/**
93
	 * Checks if fields should be excluded from the response
94
	 * 
95
	 * @param  array $fields 
96
	 * @return array
97
	 */
98
	public function filter_fields( $fields ) {
99
		return array_filter( $fields, array( $this, 'should_load_field' ) );
100
	}
101
102
	/**
103
	 * Checks if a field should be excluded from the response
104
	 * 
105
	 * @param  object $field
106
	 * @return array       
107
	 */
108
	public function should_load_field( $field ) {
109
		return $field->get_rest_visibility() && ! in_array( strtolower( $field->type ), $this->exclude_field_types );
110
	}
111
112
	/**
113
	 * Loads field value
114
	 * 
115
	 * @param  object $field
116
	 * @return array
117
	 */
118
	public function load_generic_field_value( $field ) {
119
		return $field->get_value();
120
	}
121
122
	/**
123
	 * Loads the value of a complex field
124
	 * 
125
	 * @param  object $field 
126
	 * @return array
127
	 */
128
	public function load_complex_field_value( $field ) {
129
		return $field->to_json( false )['value'];
130
	}
131
132
	/**
133
	 * Load the value of a map field
134
	 * 
135
	 * @param  object $field 
136
	 * @return array
137
	 */
138
	public function load_map_field_value( $field ) {
139
		$map_data = $field->to_json( false );
140
141
		return array(
142
			'lat'     => $map_data['lat'],
143
			'lng'     => $map_data['lng'],
144
			'zoom'    => $map_data['zoom'],
145
			'address' => $map_data['address'],
146
		);
147
	}
148
149
	/**
150
	 * Loads the value of a relationship field
151
	 * 
152
	 * @param  object $field 
153
	 * @return array
154
	 */
155
	public function load_relationship_field_value( $field ) {
156
		return maybe_unserialize( $field->get_value() );
157
	}
158
159
	/**
160
	 * Loads the value of an association field
161
	 * 
162
	 * @param object $field 
163
	 * @return array
164
	 */
165
	public function load_association_field_value( $field ) {
166
		$field->process_value();
167
		return $field->get_value();
168
	}
169
}