<?php
/*
    Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301  USA.
*/

namespace SiWeather\Webservice;

/**
  * @brief Describes a city in a region and a country.
  *
  * The codes (region and country) are Alpha 2 codes from ISO 3166-1 (country) and ISO 3166-2 (subdivision).
  *
  */
class Location
{
	public function __construct($city, $postalCode = "",
								$regionName = "", $regionCode = "",
								$countryName = "", $countryCode = "",
								$latitude = "", $longitude = "", 
								$timeZone = ""){

		$this->_city = "";
		$this->_postalCode = "";
		$this->_regionName = "";
		$this->_regionCode = "";
		$this->_countryName = "";
		$this->_countryCode = "";
		$this->_longitude = 0;
		$this->_latitude = 0;
		$this->_timeZone = "";


		$this->setCity($city);
		$this->setPostalCode($postalCode);
		$this->setRegion($regionName, $regionCode);
		$this->setCountry($countryName, $countryCode);
		$this->setLocation($latitude, $longitude);
		$this->setTimezone($timeZone);
	}

	public function setCity($name){
		$this->_city = $name;
	}
	
	public function city(){
		return $this->_city;
	}
	
	public function setPostalCode($code){
		$this->_postalCode = $code;
	}
	
	public function postalCode(){
		return $this->_postalCode;
	}
	
	public function setRegion($name, $code){
		$this->_regionName = $name;
		if(strlen($code) == 2)
			$this->_regionCode = $code;
	}
	
	public function regionName(){
		return $this->_regionName;
	}
	
	public function regionCode(){
		return $this->_regionCode;
	}

	public function setCountry($name, $code){
		$this->_countryName = $name;
		if(strlen($code) == 2)
			$this->_countryCode = $code;
	}
	
	public function countryName(){
		return $this->_countryName;
	}
	
	public function countryCode(){
		return $this->_countryCode;
	}
	
	/**
	  * @brief Sets the location to @p $latitude and @p $longitude
	  *
	  * @note Please note that this functions arguments are in the typical order of 
	  *       city coordinates, whereas the WKT format turned around those values
	  *       (WKT(Longitude Latitude)).
	  *
	  * @see latitude()
	  * @see longitude()
	  * @see locationWkt()
	  */
	public function setLocation($latitude, $longitude){
		if(self::validLatitude($latitude))
			$this->_latitude = $latitude;
		if(self::validLongitude($longitude))
			$this->_longitude = $longitude;
	}
	
	public function latitude(){
		return $this->_latitude;
	}
	
	public function longitude(){
		return $this->_longitude;
	}
	
	public function locationWkt(){
		return $this->longitude() ." ". $this->latitude();
	}

	public static function validLatitude($latitude){
		if(is_numeric($latitude) && (-90.0 <= $latitude && $latitude <= 90.0))
			return true;
		return false;
	}

	public static function validLongitude($longitude){
		if(is_numeric($longitude) && (-180.0 <= $longitude && $longitude <= 180.0))
			return true;
		return false;
	}

	public function setTimeZone($zone){
		if(preg_match("/^[\D]+\/[\D]+$/", $zone) > 0)
			$this->_timeZone = $zone;
	}
	
	public function timeZone(){
		return $this->_timeZone;
	}

	private $_countryCode;
	private $_countryName;
	private $_regionCode;
	private $_regionName;
	private $_city;
	private $_postalCode;
	private $_timeZone;
	private $_latitude;
	private $_longitude;
}

?>
