Passed
Push — master ( 34b29b...0daf4f )
by Daryl
01:42
created

Map_View::the_map()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 7
cts 9
cp 0.7778
rs 9.4285
cc 3
eloc 8
nc 4
nop 1
crap 3.0987
1
<?php
2
3
namespace Clubdeuce\WPGoogleMaps;
4
5
/**
6
 * Class Map_View
7
 * @package Clubdeuce\WPGoogleMaps
8
 */
9
class Map_View {
10
11
	/**
12
	 * @var Map_Model
13
	 */
14
	protected $_model;
15
16
	/**
17
	 * Map_View constructor.
18
	 *
19
	 * @param Map_Model $model
20
	 */
21 3
	public function __construct( $model ) {
22
		
23 3
		$this->_model = $model;
24
		
25 3
	}
26
27
	/**
28
	 * @param array $args
29
	 */
30 1
	public function the_map( $args = array() ) { 
31
32 1
		if ( $this->_model->use_clusters() ) {
33
			$args['useClusters'] = true;
34
		}
35
36 1
		if ( $this->_model->fit_bounds() ) {
37
			$args['fitBounds'] = true;
38
		}
39
40 1
		wp_localize_script( 'google-marker-clusterer', 'gmMaps', $args );
41
42 1
		$args = wp_parse_args( $args, array(
43 1
			'template' => Google_Maps::source_dir() . '/templates/map-view.php',
44
		) );
45
46 1
		require $args['template'];
47
		
48 1
	}
49
50 1
	public function the_map_params() {
51
52 1
		echo json_encode( $this->_map_params() );
53
54 1
	}
55
56
	/**
57
	 * @return array
58
	 */
59 2
	protected function _map_params() {
60
61 2
		$model  = $this->_model;
62
		$params = array(
63 2
			'center'                   => $model->center(),
64 2
			'height'                   => $model->height(),
65 2
			'styles'                   => $model->styles(),
66 2
			'zoom'                     => $model->zoom(),
67
		);
68
69 2
		return array_filter( $params );
70
71
	}
72
73
	/**
74
	 * @return array
75
	 */
76 1
	protected function _make_markers_args() {
77
78 1
		$marker_args = array();
79
80 1
		foreach ( $this->_model->markers() as $marker ) {
81 1
			$marker_args[] = $this->_make_marker_args( $marker );
82
		}
83
84 1
		return array_filter( $marker_args );
85
86
	}
87
88
	/**
89
	 * @param  Marker $marker
90
	 *
91
	 * @return array
92
	 */
93 1
	protected function _make_marker_args( $marker ) {
94
95 1
		$args = array();
96
97 1
		foreach ( $marker->marker_args() as $key => $value ) {
98 1
			$args[ $this->_camel_case( $key ) ] = $value;
99
		}
100
101 1
		if ( ! empty( $label = $this->_make_label_args( $marker->label() ) ) ) {
102 1
			$args['label'] =  $label;
103
		}
104
105 1
		return $args;
106
107
	}
108
109
	/**
110
	 * @param  Marker_Label $label
111
	 *
112
	 * @return array
113
	 */
114 1
	protected function _make_label_args( $label ) {
115
116 1
		$args = array();
117
118 1
		if ( ! empty( $label->text() ) ) {
119
			$args = array(
120 1
				'color'      => $label->color(),
121 1
				'fontFamily' => $label->font_family(),
122 1
				'fontSize'   => $label->font_size(),
123 1
				'fontWeight' => $label->font_weight(),
124 1
				'text'       => $label->text(),
125
			);
126
		}
127
128 1
		return $args;
129
130
	}
131
132
	/**
133
	 * @return array
134
	 */
135 1
	protected function _make_info_windows() {
136
137 1
		$windows = array();
138
139
		/**
140
		 * @var Marker $marker
141
		 */
142 1
		foreach( $this->_model->markers() as $marker ) {
143 1
			$info_window = $marker->info_window();
144 1
			$windows[]   = array(
145 1
				'content'      => $info_window->content(),
146 1
				'pixel_offset' => $info_window->pixel_offset(),
147 1
				'position'     => $info_window->position(),
148 1
				'max_width'    => $info_window->max_width(),
149
			);
150
		}
151
152 1
		return $windows;
153
154
	}
155
156 1
	protected function _camel_case( $text, $delimiter = '_' ) {
157
158 1
		$parts = explode( $delimiter, $text );
159
160 1
		if ( count( $parts ) ) {
161 1
			$text = '';
162 1
			foreach( $parts as $key => $value ) {
163 1
				$text .= 0 < $key ? ucfirst( $value ) : $value;
164
			}
165
		}
166
167 1
		return $text;
168
169
	}
170
}