<?php

/**
 * The definition for the contact_coords class.
 *
 * @package CGContacts
 * @category Productivity
 * @author  calguy1000 <calguy1000@cmsmadesimple.org>
 * @copyright Copyright 2014 by Robert Campbell
 */

namespace CGContacts;

/**
 * A class to represent a latitude and longitude pair.
 */
class contact_coords extends contact_sub
{
    /**
     * @ignore
     */
    private $_data;

    /**
     * Get the latitude of this object.
     *
     * @return float
     */
    public function get_lat()
    {
        return $this->lat;
    }

    /**
     * Get the longitude of this object.
     *
     * @return float
     */
    public function get_long()
    {
        return $this->long;
    }

    /**
     * Set the latitude of this object.
     *
     * @param float $val
     */
    public function set_lat($val)
    {
        $val = (float)$val;
        if( $val < -180.0 || $val > 180.0 ) throw new \Exception('Invalid latitude value for '.__CLASS__.' object');
        $this->_data['lat'] = $val;
    }

    /**
     * Set the longitude of this object.
     *
     * @param float $val
     */
    public function set_long($val)
    {
        $val = (float)$val;
        if( $val < -180.0 || $val > 180.0 ) throw new \Exception('Invalid longitude value for '.__CLASS__.' object');
        $this->_data['long'] = $val;
    }

    /**
     * @ignore
     */
    public function __get($key)
    {
        switch( $key ) {
        case 'lat':
            return cge_utils::get_param($this->_data,'lat');

        case 'long':
            return cge_utils::get_param($this->_data,'long');

        default:
            return parent::__get($key);
            break;
        }
    }

    /**
     * Validate this object.
     * Throws an exception on error.
     */
    public function validate()
    {
        parent::validate();
        if( $this->lat == '' || $this->long == '' ) throw new \CmsInvalidDataException('Invalid coordinates specified');
    }

    /**
     * Fill this object from an array.
     *
     * @param array $data
     * @internal
     */
    public function set_data($data)
    {
        parent::set_data($data);
        $this->_data = unserialize($data['value']);
    }
} // end of class

?>
