Completed
Pull Request — master (#16)
by Daryl
02:34
created

Map_Model::add_marker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Clubdeuce\WPGoogleMaps;
4
5
/**
6
 * Class Map
7
 * @package Clubdeuce\WPGoogleMaps
8
 * @method array       styles()
9
 */
10
class Map_Model extends Model_Base {
11
12
	/**
13
	 * @var null|string
14
	 */
15
	protected $_background_color = null;
16
17
	/**
18
	 * @var array
19
	 */
20
	protected $_center;
21
22
	/**
23
	 * The Map element height (default: 400px).
24
	 * @var string
25
	 */
26
	protected $_height = '400px';
27
28
	/**
29
	 * The string to use for the map HTML element id property
30
	 *
31
	 * @var string
32
	 */
33
	protected $_html_id;
34
35
	/**
36
	 * @var Marker[]
37
	 */
38
	protected $_markers = array();
39
40
	/**
41
	 * @var array
42
	 */
43
	protected $_styles = array();
44
45
	/**
46
	 * The Map element width (default: 100%).
47
	 * @var string
48
	 */
49
	protected $_width = '100%';
50
51
	/**
52
	 * @var int
53
	 */
54
	protected $_zoom = 5;
55
56
	/**
57
	 * @param Marker $marker
58
	 */
59 1
	public function add_marker( $marker ) {
60
61 1
		$this->_markers[] = $marker;
62
63 1
	}
64
65
	/**
66
	 * @param Marker[] $markers
67
	 */
68 1
	public function add_markers( $markers ) {
69
70 1
		$this->_markers = array_merge( $this->_markers, $markers );
71
72 1
	}
73
74
	/**
75
	 * @return array
76
	 */
77 2
	public function center() {
78
79 2
		return $this->_center;
80
81
	}
82
83
	/**
84
	 * @return string
85
	 */
86 2
	public function height() {
87
88 2
		return $this->_height;
89
90
	}
91
92
	/**
93
	 * @return string
94
	 */
95 2
	public function html_id() {
96
97 2
		if ( ! isset( $this->_html_id ) ) {
98 1
			$this->_html_id = sprintf( 'map-%1$s', md5( serialize( array( $this->center(), $this->markers() ) ) ) );
99
		}
100
101 2
		return $this->_html_id;
102
103
	}
104
105
	/**
106
	 * @return Marker[]
107
	 */
108 1
	public function markers() {
109
110 1
		return $this->_markers;
111
112
	}
113
114
	/**
115
	 * @return string
116
	 */
117 1
	public function width() {
118
119 1
		return $this->_width;
120
121
	}
122
123
	/**
124
	 * @return int
125
	 */
126 2
	public function zoom() {
127
128 2
		return $this->_zoom;
129
130
	}
131
132
	/**
133
	 * @param array $center
134
	 */
135 1
	public function set_center( $center ) {
136
137 1
		$center = wp_parse_args( $center, array(
138 1
			'lat' => null,
139
			'lng' => null,
140
		) );
141
142 1
		$this->_center = $center;
143
144 1
	}
145
146
	/**
147
	 * @param int $zoom
148
	 */
149 1
	public function set_zoom( $zoom ) {
150
151 1
		$this->_zoom = (int)$zoom;
152
153 1
	}
154
155
	/**
156
	 * @param string $height
157
	 */
158 1
	public function set_height( $height ) {
159
160 1
		$this->_height = $height;
161
162 1
	}
163
164
	/**
165
	 * @param string $html_id
166
	 */
167 1
	public function set_html_id( $html_id ) {
168
169 1
		$this->_html_id = $html_id;
170
171 1
	}
172
173
	/**
174
	 * @param array|string $styles
175
	 */
176
	public function set_styles( $styles ) {
177
178
		do {
179
			if ( is_string( $styles ) ) {
180
				$styles = json_decode( $styles, true );
181
			}
182
183
			if ( ! is_array( $styles ) ) {
184
				trigger_error( __( 'The style property must be an array' ) );
185
				break;
186
			}
187
188
			$this->_styles = $styles;
189
		} while ( false );
190
191
	}
192
193
	/**
194
	 * @return array
195
	 *
196
	 * @todo Refactor to make_params
197
	 */
198 1
	public function make_args() {
199
200
		return array(
201 1
			'center'          => $this->center(),
202 1
			'styles'          => $this->styles(),
203 1
			'zoom'            => (int)$this->zoom(),
204
		);
205
206
	}
207
208
}
209