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

Marker::info_window()   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
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Clubdeuce\WPGoogleMaps;
4
5
/**
6
 * Class Marker
7
 * @package Clubdeuce\WPGoogleMaps
8
 *
9
 * @link   https://developers.google.com/maps/documentation/javascript/reference#Marker
10
 *
11
 * @method string      address()
12
 * @method bool        clickable()
13
 * @method bool        cross_on_drag()
14
 * @method string      cursor()
15
 * @method bool        draggable()
16
 * @method array       extra_args()
17
 * @method Geocoder    geocoder()
18
 * @method array       icon()
19
 * @method Info_Window info_window()
20
 * @method string      title()
21
 * @method bool        visible()
22
 * @method int         z_index()
23
 */
24
class Marker extends Model_Base {
25
26
	/**
27
	 * @var string
28
	 */
29
	protected $_address;
30
31
	/**
32
	 * @var Geocoder
33
	 */
34
	protected $_geocoder;
35
36
	/**
37
	 * @var array
38
	 */
39
	protected $_icon;
40
41
	/**
42
	 * @var Info_Window
43
	 */
44
	protected $_info_window;
45
46
	/**
47
	 * @var Marker_Label
48
	 */
49
	protected $_label;
50
51
	/**
52
	 * @var double
53
	 */
54
	protected $_latitude;
55
56
	/**
57
	 * @var double
58
	 */
59
	protected $_longitude;
60
61
	/**
62
	 * @var Location|\WP_Error
63
	 */
64
	protected $_location;
65
66
	/**
67
	 * This appears as a tooltip for the marker.
68
	 *
69
	 * @var string
70
	 */
71
	protected $_title;
72
73
	/**
74
	 * @var array
75
	 */
76
	protected $_extra_args = array();
77
78
	/**
79
	 * Marker_Model constructor.
80
	 * @param array $args
81
	 */
82 2
	public function __construct( $args = array() ) {
83
84 2
		$args = wp_parse_args( $args, array(
85 2
			'address'     => '',
86
			'icon'        => null,
87 2
			'info_window' => '',
88 2
			'label'       => new Marker_Label(),
89 2
			'title'       => '',
90
			'latitude'    => null,
91
			'longitude'   => null,
92
		) );
93
94 2
		if ( empty( $args['info_window'] ) ) {
95 2
			$args['info_window'] = new Info_Window( array(
96 2
				'position' => array( 'lat' => $args['latitude'], 'lng' => $args['longitude'] ),
97
			) );
98
		}
99
100 2
		parent::__construct( $args );
101
102 2
	}
103
104
	/**
105
	 * @return Marker_Label
106
	 */
107 1
	public function label() {
108
109 1
		if ( is_string( $this->_label ) ) {
110 1
			$this->_label = new Marker_Label( array( 'text' => $this->_label ) );
111
		}
112
113 1
		return $this->_label;
114
115
	}
116
117
	/**
118
	 * @return double
119
	 */
120 1 View Code Duplication
	public function latitude() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
122 1
		if ( is_null( $this->_latitude ) && ! is_wp_error( $this->location() ) ) {
123 1
			$this->_latitude = $this->location()->latitude();
124
		}
125 1
		return doubleval( $this->_latitude );
126
	}
127
128
	/**
129
	 * @return Location|\WP_Error
130
	 */
131 2
	public function location() {
132
133 2
		if ( ! is_object( $this->_location ) ) {
134 2
			$this->_location = $this->_geocoder()->geocode( $this->_address );
135
		}
136
137 2
		return $this->_location;
138
139
	}
140
141
	/**
142
	 * @return double
143
	 */
144 1 View Code Duplication
	public function longitude() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
146 1
		if ( is_null( $this->_longitude ) && ! is_wp_error( $this->location() ) ) {
147 1
			$this->_longitude = doubleval( $this->location()->longitude() );
148
		}
149
150 1
		return doubleval( $this->_longitude );
151
	}
152
153
	/**
154
	 * Get the position of this marker. An array with key/value pairs of lat and lng.
155
	 *
156
	 * @return array
157
	 */
158 1
	public function position() {
159
160 1
		return array( 'lat' => $this->latitude(), 'lng' => $this->longitude() );
161
162
	}
163
164
	/**
165
	 * @param string|array $icon
166
	 */
167 1
	public function set_icon( $icon ) {
168
169 1
		if ( is_string( $icon ) ) {
170
			//assume $icon is an URL
171
			$icon = array(
172 1
				'url' => (string)$icon,
173
			);
174
		}
175
176 1
		$this->_icon = $icon;
177
178 1
	}
179
180
	/**
181
	 * @param  array $args
182
	 * @return array
183
	 */
184 3
	public function marker_args( $args = array() ) {
185
186 3
		$args = array_merge( $args, $this->extra_args() );
187
188 3
		$args = wp_parse_args( $args, array(
189 3
			'position'  => $this->position(),
190 3
			'icon'      => $this->icon(),
191 3
			'label'     => $this->label()->options(),
192 3
			'title'     => $this->title(),
193
		) );
194
195 3
		return array_filter( $args );
196
197
	}
198
199
	/**
200
	 * @return Geocoder
201
	 */
202 2
	protected function _geocoder() {
203
204 2
		if (! is_a( $this->_geocoder, Geocoder::class ) ) {
205 1
			$this->_geocoder = new Geocoder();
206
		}
207
208 2
		return $this->_geocoder;
209
210
	}
211
212
}
213