1: <?php
2: namespace TIC\TownBundle\Traits;
3:
4: /**
5: * Gestion des champs latitude & longitude pour les coordonnées géographiques.
6: */
7: trait Geolocatable
8: {
9:
10: // --------------------------------------------------------------------- Properties
11:
12: /**
13: * @ORM\Column(type="decimal", precision=10, scale=6, nullable=true)
14: */
15: protected $geoLat;
16:
17: /**
18: * @ORM\Column(type="decimal", precision=10, scale=6, nullable=true)
19: */
20: protected $geoLng;
21:
22:
23: // --------------------------------------------------------------------- Mutators/Accessors
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: // --------------------------------------------------------------------- Static methods
49:
50: /**
51: * Retourne un tableau {'lat','lng'} à partir de différents formats d'entrée.
52: * @param array|string $latLng Coordonnées à analyser (chaine concaténée ou tableau lat/lng)
53: * @param string $sep Caractère(s) séparateur si $latLng est une chaine (par défaut ';' et ',')
54: * @return array|null Tableau avec les clés 'lat' & 'lng' (ou null si données non reconnues)
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: // TODO
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: // --------------------------------------------------------------------- Custom methods
84:
85: /**
86: * Enregistre les coordonnées latitude;longitude pour le formulaire.
87: * @param array|string $latLng Coordonnées à enregistrer (chaine concaténée ou tableau lat/lng)
88: * @param string $sep Caractère(s) séparateur si $latLng est une chaine (par défaut ';' et ',')
89: * @return Entity
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: * Retourne les coordonnées latitude;longitude pour le formulaire.
103: * @param bool|string $toString
104: * @return array|string
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: