<?php

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

namespace CGContacts;

/**
 * This class defines a physical address.
 * Multiple physical addresses can be associated with a contact.
 */
class contact_address extends contact_sub
{
    /**
     * @ignore
     */
    private $_data;

    /**
     * @ignore
     */
    public function __tostring()
    {
        return $this->format();
    }

    /**
     * Format the address as a single string.
     *
     * @return string
     */
    public function format()
    {
        $tmp = array_values($this->_data);
        return implode("\n",$tmp);
    }

    /**
     * Get the street address portion of the address.
     *
     * @return string
     */
    public function get_street()
    {
        return $this->street;
    }

    /**
     * Set the string address portion of the address.
     *
     * @param string $val
     */
    public function set_street($val)
    {
        $this->_data['street'] = trim((string) $val);
    }

    /**
     * Get the post office box portion of the address.
     *
     * @return string
     */
    public function get_pobox()
    {
        return $this->pobox;
    }

    /**
     * Set the post office box portion of the address.
     *
     * @param string $val
     */
    public function set_pobox($val)
    {
        $this->_data['pobox'] = trim((string) $val);
    }

    /**
     * Get the city portion of the address.
     *
     * @return string
     */
    public function get_city()
    {
        return $this->city;
    }

    /**
     * Set the city portion of the address.
     *
     * @param string $val
     */
    public function set_city($val)
    {
        $this->_data['city'] = trim((string) $val);
    }

    /**
     * Get the zip/postal code portion of the address.
     *
     * @return string
     */
    public function get_zip()
    {
        return $this->zip;
    }

    /**
     * Set the zip/postal code portion of the address.
     *
     * @param string $val
     */
    public function set_zip($val)
    {
        $this->_data['zip'] = trim((string) $val);
    }

    /**
     * Get the state/province code portion of the address.
     *
     * @return string
     */
    public function get_state()
    {
        return $this->state;
    }

    /**
     * Set the state/province portion of the address.
     *
     * @param string $val
     */
    public function set_state($val)
    {
        $this->_data['state'] = trim((string) $val);
    }

    /**
     * Get the country portion of the address.
     *
     * @return string
     */
    public function get_country()
    {
        return $this->country;
    }

    /**
     * Set the country portion of the address.
     *
     * @param string $val
     */
    public function set_country($val)
    {
        $this->_data['country'] = trim((string) $val);
    }

    /**
     * @ignore
     */
    public function __get($key)
    {
        $key = trim((string) $key);
        switch( $key ) {
        case 'street':
        case 'pobox':
        case 'city':
        case 'state':
        case 'zip':
        case 'country':
            return \cge_utils::get_param($this->_data,$key);

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

    /**
     * Convert the object to a flat array.
     *
     * @internal
     * @return array
     */
    public function to_data()
    {
        $out = parent::to_data();
        $out = array_merge($out,$this->_data);
        return $out;
    }

    /**
     * Validate the contents of the object prior to saving.
     * Although the format of an address is loose, at least one of the fields must be specified.
     * This method throws an exception on error.
     */
    public function validate()
    {
        parent::validate();
        $fnd = 0;
        foreach( $this->_data as $key => $value ) {
            switch( $key ) {
            case 'street':
            case 'pobox':
            case 'city':
            case 'state':
            case 'zip':
            case 'country':
                if( $value != '' ) $fnd++;
                break;
            }
        }
        if( $fnd == 0 ) throw new \CmsInvalidDataException('At least some data must be specified for an address object');
    }

    /**
     * Get the raw content of this object suitable for saving.
     *
     * @return string
     */
    public function get_raw_value()
    {
        return serialize($this->_data);
    }

    /**
     * Given an array of data, fill the object.
     *
     * @internal
     */
    public function set_data($data)
    {
        parent::set_data($data);
        if( isset($data['value']) ) {
            $this->_data = unserialize($data['value']);
        }
        else {
          foreach( $data as $key => $val ) {
              switch( $key ) {
              case 'street':
              case 'pobox':
              case 'city':
              case 'state':
              case 'zip':
              case 'country':
                  $this->_data[$key] = trim($val);
                  break;
              }
          }
        }
    }
}


?>
