Map_View::_make_marker_args()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
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
		$args = wp_parse_args( $args, array(
41 1
			'template' => Google_Maps::source_dir() . '/templates/map-view.php',
42
		) );
43
44 1
		require $args['template'];
45
		
46 1
	}
47
48 1
	public function the_map_params() {
49
50 1
		echo json_encode( $this->_map_params() );
51
52 1
	}
53
54
	/**
55
	 * @return array
56
	 */
57 2
	protected function _map_params() {
58
59 2
		$model  = $this->_model;
60
		$params = array(
61 2
			'center'                   => $model->center(),
62 2
			'height'                   => $model->height(),
63 2
			'styles'                   => $model->styles(),
64 2
			'zoom'                     => $model->zoom(),
65
		);
66
67 2
		return array_filter( $params );
68
69
	}
70
71
	/**
72
	 * @return array
73
	 */
74 1
	protected function _make_markers_args() {
75
76 1
		$marker_args = array();
77
78 1
		foreach ( $this->_model->markers() as $marker ) {
79 1
			$marker_args[] = $this->_make_marker_args( $marker );
80
		}
81
82 1
		return array_filter( $marker_args );
83
84
	}
85
86
	/**
87
	 * @param  Marker $marker
88
	 *
89
	 * @return array
90
	 */
91 1
	protected function _make_marker_args( $marker ) {
92
93 1
		$args = array();
94
95 1
		foreach ( $marker->marker_args() as $key => $value ) {
96 1
			$args[ $this->_camel_case( $key ) ] = $value;
97
		}
98
99 1
		if ( ! empty( $label = $this->_make_label_args( $marker->label() ) ) ) {
100 1
			$args['label'] =  $label;
101
		}
102
103 1
		return $args;
104
105
	}
106
107
	/**
108
	 * @param  Marker_Label $label
109
	 *
110
	 * @return array
111
	 */
112 1
	protected function _make_label_args( $label ) {
113
114 1
		$args = array();
115
116 1
		if ( ! empty( $label->text() ) ) {
117
			$args = array(
118 1
				'color'      => $label->color(),
119 1
				'fontFamily' => $label->font_family(),
120 1
				'fontSize'   => $label->font_size(),
121 1
				'fontWeight' => $label->font_weight(),
122 1
				'text'       => $label->text(),
123
			);
124
		}
125
126 1
		return $args;
127
128
	}
129
130
	/**
131
	 * @return array
132
	 */
133 1
	protected function _make_info_windows() {
134
135 1
		$windows = array();
136
137
		/**
138
		 * @var Marker $marker
139
		 */
140 1
		foreach( $this->_model->markers() as $marker ) {
141 1
			$info_window = $marker->info_window();
142 1
			$windows[]   = array(
143 1
				'content'      => $info_window->content(),
144 1
				'pixel_offset' => $info_window->pixel_offset(),
145 1
				'position'     => $info_window->position(),
146 1
				'max_width'    => $info_window->max_width(),
147
			);
148
		}
149
150 1
		return $windows;
151
152
	}
153
154 1
	protected function _camel_case( $text, $delimiter = '_' ) {
155
156 1
		$parts = explode( $delimiter, $text );
157
158 1
		if ( count( $parts ) ) {
159 1
			$text = '';
160 1
			foreach( $parts as $key => $value ) {
161 1
				$text .= 0 < $key ? ucfirst( $value ) : $value;
162
			}
163
		}
164
165 1
		return $text;
166
167
	}
168
}