Source of file Geolocatable.php

Size: 3,298 Bytes - Last Modified: 2023-11-16T22:56:03+01:00

/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/town-bundle/src/Traits/Geolocatable.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
<?php
namespace TIC\TownBundle\Traits;

/**
 * Gestion des champs latitude & longitude pour les coordonnées géographiques.
 */
trait Geolocatable
{

// --------------------------------------------------------------------- Properties

	/**
	 * @ORM\Column(type="decimal", precision=10, scale=6, nullable=true)
	 */
	protected $geoLat;

	/**
	 * @ORM\Column(type="decimal", precision=10, scale=6, nullable=true)
	 */
	protected $geoLng;


// --------------------------------------------------------------------- Mutators/Accessors

	public function setGeoLat(?float $geoLat): self
	{
		$this->geoLat = $geoLat;
		return $this;
	}

	public function getGeoLat(): ?float
	{
		return $this->geoLat;
	}

	public function setGeoLng(?float $geoLng): self
	{
		$this->geoLng = $geoLng;
		return $this;
	}

	public function getGeoLng(): ?float
	{
		return $this->geoLng;
	}


// --------------------------------------------------------------------- Static methods

	/**
	 * Retourne un tableau {'lat','lng'} à partir de différents formats d'entrée.
	 * @param  array|string    $latLng   Coordonnées à analyser (chaine concaténée ou tableau lat/lng)
	 * @param  string          $sep      Caractère(s) séparateur si $latLng est une chaine (par défaut ';' et ',')
	 * @return array|null                Tableau avec les clés 'lat' & 'lng' (ou null si données non reconnues)
	 */
	public static function parseGeomap($latLng, $sep = null) {
		if (is_string($latLng)) {
			if (is_string($sep) && strpos($latLng, $sep)) $latLng = explode($sep, $latLng);
			elseif (strpos($latLng, ';')) $latLng = explode(';', $latLng);
			elseif (strpos($latLng, ',')) $latLng = explode(',', $latLng);
			else return;
		}
		if (is_array($latLng)) {
			if (array_key_exists('lat', $latLng) && array_key_exists('lng', $latLng)) {
				$lat = $latLng['lat'];
				$lng = $latLng['lng'];
			} elseif (count($latLng) == 2) {
				list($lat, $lng) = array_values($latLng);
			}
		}
		elseif (is_object($latLng)) {
			// TODO
		}
		if (! isset($lat) || ! is_numeric($lat)) return;
		if (! isset($lng) || ! is_numeric($lng)) return;
		return array(
			'lat' => $lat,
			'lng' => $lng
		);
	}


// --------------------------------------------------------------------- Custom methods

	/**
	 * Enregistre les coordonnées latitude;longitude pour le formulaire.
	 * @param  array|string    $latLng   Coordonnées à enregistrer (chaine concaténée ou tableau lat/lng)
	 * @param  string          $sep      Caractère(s) séparateur si $latLng est une chaine (par défaut ';' et ',')
	 * @return Entity
	 */
	public function setGeomap($latLng, $sep = null)
	{
		$this->geoLat = $this->geoLng = null;
		if ($res = static::parseGeomap($latLng, $sep)) {
			$this->geoLat = $res['lat'];
			$this->geoLng = $res['lng'];
		}
		return $this;
	}

	/**
	 * Retourne les coordonnées latitude;longitude pour le formulaire.
	 * @param  bool|string    $toString
	 * @return array|string
	 */
	public function getGeomap($toString = false)
	{
		if ($this->geoLat === null) return null;
		if ($this->geoLng === null) return null;
		if ($toString) {
			$sep = is_string($toString) ? $toString : ',';
			return sprintf('%f%s%f', $this->geoLat, $sep, $this->geoLng);
		}
		return array(
			'lat' => $this->geoLat,
			'lng' => $this->geoLng
		);
	}

}