forked from drymek/GoogleBundle
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MapsManager.php
117 lines (98 loc) · 2.5 KB
/
MapsManager.php
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
<?php
namespace AntiMattr\GoogleBundle;
use AntiMattr\GoogleBundle\Maps\MapInterface;
use Doctrine\Common\Collections\Collection;
class MapsManager
{
const MAP_JAVASCRIPT = 'map_javascript';
const MAP_STATIC = 'map_static';
private $config = array();
private $maps = array();
private $templating;
public function __construct(array $config = array())
{
$this->config = $config;
}
public function setKey($key)
{
$this->config['key'] = $key;
}
public function getKey()
{
if (array_key_exists('key', $this->config)) {
return $this->config['key'];
}
}
public function hasMaps()
{
if (!empty($this->maps)) {
return true;
}
return false;
}
public function hasMap(MapInterface $map)
{
if ($this->maps instanceof Collection) {
return $this->maps->contains($map);
} else {
return in_array($map, $this->maps, true);
}
}
public function addMap(MapInterface $map)
{
$this->maps[] = $map;
}
public function removeMap(MapInterface $map)
{
if (!$this->hasMap($map)) {
return null;
}
if ($this->maps instanceof Collection) {
return $this->maps->removeElement($map);
} else {
unset($this->maps[array_search($map, $this->maps, true)]);
return $map;
}
}
public function setMaps($maps)
{
$this->maps = $maps;
}
public function getMaps()
{
return $this->maps;
}
public function getMapById($id)
{
foreach ($this->maps as $map) {
if ($id == $map->getId()) {
return $map;
}
}
}
/**
* create Google Map instance
* available options:
* - MapsManager::MAP_JAVASCRIPT
* - MapsManager::MAP_STATIC
*
* @param mixed $type
* @return AbstractMap instance
*/
public function create($type, $id) {
switch ($type)
{
case self::MAP_JAVASCRIPT:
$map = new Maps\JavascriptMap();
break;
case self::MAP_STATIC;
$map = new Maps\StaticMap();
break;
default:
throw \InvalidArgumentException(sprintf('Google Map\'s type: %s is not supported', $type));
}
$map->setId($id);
$map->setApiKey($this->getKey('api_key'));
return $map;
}
}