| 1: | <?php |
| 2: | namespace TIC\TownBundle\Traits; |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | trait Geolocatable |
| 8: | { |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | protected $geoLat; |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | protected $geoLng; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | public function setGeoLat(?float $geoLat): self |
| 26: | { |
| 27: | $this->geoLat = $geoLat; |
| 28: | return $this; |
| 29: | } |
| 30: | |
| 31: | public function getGeoLat(): ?float |
| 32: | { |
| 33: | return $this->geoLat; |
| 34: | } |
| 35: | |
| 36: | public function setGeoLng(?float $geoLng): self |
| 37: | { |
| 38: | $this->geoLng = $geoLng; |
| 39: | return $this; |
| 40: | } |
| 41: | |
| 42: | public function getGeoLng(): ?float |
| 43: | { |
| 44: | return $this->geoLng; |
| 45: | } |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public static function parseGeomap($latLng, $sep = null) { |
| 57: | if (is_string($latLng)) { |
| 58: | if (is_string($sep) && strpos($latLng, $sep)) $latLng = explode($sep, $latLng); |
| 59: | elseif (strpos($latLng, ';')) $latLng = explode(';', $latLng); |
| 60: | elseif (strpos($latLng, ',')) $latLng = explode(',', $latLng); |
| 61: | else return; |
| 62: | } |
| 63: | if (is_array($latLng)) { |
| 64: | if (array_key_exists('lat', $latLng) && array_key_exists('lng', $latLng)) { |
| 65: | $lat = $latLng['lat']; |
| 66: | $lng = $latLng['lng']; |
| 67: | } elseif (count($latLng) == 2) { |
| 68: | list($lat, $lng) = array_values($latLng); |
| 69: | } |
| 70: | } |
| 71: | elseif (is_object($latLng)) { |
| 72: | |
| 73: | } |
| 74: | if (! isset($lat) || ! is_numeric($lat)) return; |
| 75: | if (! isset($lng) || ! is_numeric($lng)) return; |
| 76: | return array( |
| 77: | 'lat' => $lat, |
| 78: | 'lng' => $lng |
| 79: | ); |
| 80: | } |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | public function setGeomap($latLng, $sep = null) |
| 92: | { |
| 93: | $this->geoLat = $this->geoLng = null; |
| 94: | if ($res = static::parseGeomap($latLng, $sep)) { |
| 95: | $this->geoLat = $res['lat']; |
| 96: | $this->geoLng = $res['lng']; |
| 97: | } |
| 98: | return $this; |
| 99: | } |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | public function getGeomap($toString = false) |
| 107: | { |
| 108: | if ($this->geoLat === null) return null; |
| 109: | if ($this->geoLng === null) return null; |
| 110: | if ($toString) { |
| 111: | $sep = is_string($toString) ? $toString : ','; |
| 112: | return sprintf('%f%s%f', $this->geoLat, $sep, $this->geoLng); |
| 113: | } |
| 114: | return array( |
| 115: | 'lat' => $this->geoLat, |
| 116: | 'lng' => $this->geoLng |
| 117: | ); |
| 118: | } |
| 119: | |
| 120: | } |
| 121: | |