|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\UX\Map; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Simon André <smn.andre@gmail.com> |
| 16 | + * |
| 17 | + * @internal |
| 18 | + */ |
| 19 | +final class MapFactory |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @param array{ |
| 23 | + * center?: array{lat: float, lng: float}, |
| 24 | + * zoom?: float, |
| 25 | + * markers?: list<array{ |
| 26 | + * position: array{lat: float, lng: float}, |
| 27 | + * title?: string, |
| 28 | + * infoWindow?: array{ |
| 29 | + * content?: string, |
| 30 | + * headerContent?: string, |
| 31 | + * }, |
| 32 | + * }>, |
| 33 | + * } $array The array representation of the map |
| 34 | + */ |
| 35 | + public static function fromArray(array $array): Map |
| 36 | + { |
| 37 | + $map = new Map(); |
| 38 | + |
| 39 | + $map->fitBoundsToMarkers(); |
| 40 | + |
| 41 | + if (isset($array['center'])) { |
| 42 | + if (!\is_array($array['center'])) { |
| 43 | + throw new \InvalidArgumentException('The "center" parameter must be an array.'); |
| 44 | + } |
| 45 | + |
| 46 | + $map->center(self::createPoint($array['center'])); |
| 47 | + $map->fitBoundsToMarkers(false); |
| 48 | + unset($array['center']); |
| 49 | + } |
| 50 | + |
| 51 | + if (isset($array['zoom'])) { |
| 52 | + if (!is_numeric($array['zoom'])) { |
| 53 | + throw new \InvalidArgumentException('The "zoom" parameter must be numeric.'); |
| 54 | + } |
| 55 | + |
| 56 | + $map->zoom((float) $array['zoom']); |
| 57 | + $map->fitBoundsToMarkers(false); |
| 58 | + unset($array['zoom']); |
| 59 | + } |
| 60 | + |
| 61 | + if (isset($array['markers'])) { |
| 62 | + if (!\is_array($array['markers'])) { |
| 63 | + throw new \InvalidArgumentException('The "markers" parameter must be an array.'); |
| 64 | + } |
| 65 | + foreach ($array['markers'] as $marker) { |
| 66 | + if (!\is_array($marker)) { |
| 67 | + throw new \InvalidArgumentException('The "markers" parameter must be an array of arrays.'); |
| 68 | + } |
| 69 | + $marker = self::createMarker($marker); |
| 70 | + $map = $map->addMarker($marker); |
| 71 | + } |
| 72 | + unset($array['markers']); |
| 73 | + } |
| 74 | + |
| 75 | + if (\count($array) > 0) { |
| 76 | + throw new \InvalidArgumentException(\sprintf('Unknown map parameters: "%s"', implode(', ', array_keys($array)))); |
| 77 | + } |
| 78 | + |
| 79 | + return $map; |
| 80 | + } |
| 81 | + |
| 82 | + private static function createMarker(array $marker): Marker |
| 83 | + { |
| 84 | + if (!\array_key_exists('position', $marker)) { |
| 85 | + throw new \InvalidArgumentException('The "position" parameter is required.'); |
| 86 | + } |
| 87 | + if (!\is_array($marker['position'])) { |
| 88 | + throw new \InvalidArgumentException(\sprintf('The "position" parameter must be an array, "%s" given.', get_debug_type($marker['position']))); |
| 89 | + } |
| 90 | + $point = self::createPoint($marker['position']); |
| 91 | + unset($marker['position']); |
| 92 | + |
| 93 | + $infoWindow = null; |
| 94 | + if (\array_key_exists('infoWindow', $marker)) { |
| 95 | + if (!\is_array($marker['infoWindow'])) { |
| 96 | + throw new \InvalidArgumentException(\sprintf('The "infoWindow" parameter must be an array, "%s" given.', get_debug_type($marker['infoWindow']))); |
| 97 | + } |
| 98 | + |
| 99 | + $infoWindow = self::createInfoWindow($marker['infoWindow']); |
| 100 | + unset($marker['infoWindow']); |
| 101 | + } |
| 102 | + |
| 103 | + $title = null; |
| 104 | + if (\array_key_exists('title', $marker)) { |
| 105 | + if (!\is_string($marker['title'])) { |
| 106 | + throw new \InvalidArgumentException(\sprintf('The "title" parameter must be a string, "%s" given.', get_debug_type($marker['title']))); |
| 107 | + } |
| 108 | + $title = $marker['title']; |
| 109 | + unset($marker['title']); |
| 110 | + } |
| 111 | + |
| 112 | + $extra = []; |
| 113 | + if (\array_key_exists('extra', $marker)) { |
| 114 | + if (!\is_array($marker['extra'])) { |
| 115 | + throw new \InvalidArgumentException(\sprintf('The "extra" parameter must be an array, "%s" given.', get_debug_type($marker['extra']))); |
| 116 | + } |
| 117 | + $extra = $marker['extra']; |
| 118 | + unset($marker['extra']); |
| 119 | + } |
| 120 | + |
| 121 | + if (\count($marker) > 0) { |
| 122 | + throw new \InvalidArgumentException(\sprintf('Unknown marker parameters: "%s".', implode('", "', array_keys($marker)))); |
| 123 | + } |
| 124 | + |
| 125 | + return new Marker($point, $title, $infoWindow, $extra); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @param array{content?: string, headerContent?: string} $infoWindow |
| 130 | + */ |
| 131 | + private static function createInfoWindow(array $infoWindow): InfoWindow |
| 132 | + { |
| 133 | + $headerContent = null; |
| 134 | + if (\array_key_exists('headerContent', $infoWindow)) { |
| 135 | + if (!\is_string($infoWindow['headerContent'])) { |
| 136 | + throw new \InvalidArgumentException(\sprintf('The "header" parameter must be a string, "%s" given.', get_debug_type($infoWindow['headerContent']))); |
| 137 | + } |
| 138 | + |
| 139 | + $headerContent = $infoWindow['headerContent']; |
| 140 | + unset($infoWindow['headerContent']); |
| 141 | + } |
| 142 | + |
| 143 | + $content = null; |
| 144 | + if (\array_key_exists('content', $infoWindow)) { |
| 145 | + if (!\is_string($infoWindow['content'])) { |
| 146 | + throw new \InvalidArgumentException(\sprintf('The "content" parameter must be a string, "%s" given.', get_debug_type($infoWindow['content']))); |
| 147 | + } |
| 148 | + |
| 149 | + $content = $infoWindow['content']; |
| 150 | + unset($infoWindow['content']); |
| 151 | + } |
| 152 | + |
| 153 | + if (\count($infoWindow) > 0) { |
| 154 | + throw new \InvalidArgumentException(\sprintf('Unknown "infoWindow" parameters: "%s".', implode('", "', array_keys($infoWindow)))); |
| 155 | + } |
| 156 | + |
| 157 | + if (!$headerContent && !$content) { |
| 158 | + throw new \InvalidArgumentException('The "infoWindow" parameter must have at least one of "header" or "content" values.'); |
| 159 | + } |
| 160 | + |
| 161 | + return new InfoWindow($headerContent, $content); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @param array{lat?: float, lng?: float} $point |
| 166 | + */ |
| 167 | + private static function createPoint(array $point): Point |
| 168 | + { |
| 169 | + if (!isset($point['lat']) || !isset($point['lng'])) { |
| 170 | + throw new \InvalidArgumentException('The Point parameter must be an array with "lat" and "lng" keys.'); |
| 171 | + } |
| 172 | + if (!is_numeric($point['lat']) || !is_numeric($point['lng'])) { |
| 173 | + throw new \InvalidArgumentException('The "lat" and "lng" values must be numeric.'); |
| 174 | + } |
| 175 | + |
| 176 | + $lat = (float) $point['lat']; |
| 177 | + $lng = (float) $point['lng']; |
| 178 | + unset($point['lat'], $point['lng']); |
| 179 | + |
| 180 | + if (\count($point) > 2) { |
| 181 | + throw new \InvalidArgumentException(\sprintf('Unknown Point parameters: "%s".', implode('", "', array_keys($point)))); |
| 182 | + } |
| 183 | + |
| 184 | + return new Point($lat, $lng); |
| 185 | + } |
| 186 | +} |
0 commit comments