-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathMap.php
More file actions
199 lines (164 loc) · 3.94 KB
/
Map.php
File metadata and controls
199 lines (164 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* Maps for Craft CMS
*
* @link https://ethercreative.co.uk
* @copyright Copyright (c) 2019 Ether Creative
*/
namespace ether\simplemap\models;
use craft\base\Model;
use craft\helpers\Json;
use ether\simplemap\SimpleMap;
use Exception;
use Twig\Markup;
use yii\base\InvalidConfigException;
/**
* Class Map
*
* @author Ether Creative
* @package ether\simplemap\models
*/
class Map extends BaseLocation
{
// Properties
// =========================================================================
/** @var int|null */
public ?int $id = null;
/** @var int|null */
public ?int $ownerId = null;
/** @var int|null */
public ?int $ownerSiteId = null;
/** @var int|null */
public ?int $fieldId = null;
/** @var int */
public ?int $zoom = 15;
/** @var int|float|null */
public int|null|float $distance = null;
// Constructor
// =========================================================================
public function __construct ($config = [])
{
foreach (['id', 'ownerId', 'ownerSiteId', 'fieldId'] as $key)
if (array_key_exists($key, $config))
unset($config[$key]);
parent::__construct($config);
$this->distance = SimpleMap::getInstance()->map->getDistance($this);
}
// Getters
// =========================================================================
public function __get ($name)
{
$isPart = (is_object($this->parts) && property_exists($this->parts, $name)) || $name === 'streetAddress';
if (in_array($name, PartsLegacy::$legacyKeys) && !$isPart)
return null;
else if ($isPart)
return $this->parts->$name;
return parent::__get($name);
}
public function canGetProperty ($name, $checkVars = true, $checkBehaviors = true): bool
{
try
{
if (
(is_object($this->parts) && property_exists($this->parts, $name)) ||
$name === 'streetAddress' ||
in_array($name, PartsLegacy::$legacyKeys)
) return true;
} catch (Exception $e) {
return false;
}
return parent::canGetProperty($name, $checkVars, $checkBehaviors);
}
// Methods
// =========================================================================
public function rules (): array
{
$rules = parent::rules();
$rules[] = [
['zoom'],
'required',
];
$rules[] = [
['lat'],
'double',
'min' => -90,
'max' => 90,
];
$rules[] = [
['lng'],
'double',
'min' => -180,
'max' => 180,
];
return $rules;
}
public function isValueEmpty (): bool
{
return empty($this->lat) && empty($this->lng);
}
// Render Map
// =========================================================================
// Render Map: Image
// -------------------------------------------------------------------------
/**
* Output the map field as a static image
*
* @param array $options
*
* @return string|void
* @throws Exception
*/
public function img (array $options = [])
{
return SimpleMap::getInstance()->static->generate(
$this->_getMapOptions($options)
);
}
/**
* Output the map ready for srcset
*
* @param array $options
*
* @return string
* @throws Exception
*/
public function imgSrcSet (array $options = []): string
{
$options = $this->_getMapOptions($options);
$x1 = $this->img(array_merge($options, ['scale' => 1]));
$x2 = $this->img(array_merge($options, ['scale' => 2]));
return $x1 . ' 1x, ' . $x2 . ' 2x';
}
/**
* Output an interactive map
*
* @param array $options
*
* @return string|void
* @throws InvalidConfigException
*/
public function embed (array $options = [])
{
$options = $this->_getMapOptions($options);
return SimpleMap::getInstance()->embed->embed($options);
}
// Helpers
// =========================================================================
/**
* Merge options w/ map properties
*
* @param array $options
*
* @return array
*/
private function _getMapOptions (array $options): array
{
return array_merge($options, [
'center' => [
$this->lat,
$this->lng,
],
'zoom' => $this->zoom,
]);
}
}