<?php

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

namespace CGContacts;

/**
 * A phone number has a type, as well as a label and value.
 * The type is based on VCARD 3.0 specifications.
 */
class contact_phone extends contact_sub
{
    const TYPE_VOICE  = 'voice';
    const TYPE_MSG    = 'msg';
    const TYPE_FAX    = 'fax';
    const TYPE_MOBILE = 'mobile';
    const TYPE_PAGER  = 'pager';
    const TYPE_VIDEO  = 'video';
    const TYPE_SAT    = 'satellite';

    /**
     * @ignore
     */
    private $_data = array();

    /**
     * @ignore
     */
    public function __tostring()
    {
        $mod = \cms_utils::get_module('CGContacts');
        $key = 'phone_'.$this->get_type();
        return $this->get_value().' ('.$mod->Lang($key).')';
    }

    /**
     * Constructor
     *
     * @param contact $parent The contact that this object is associated with.
     */
    public function __construct(contact &$parent) {
        parent::__construct($parent);
        $this->_data['type'] = self::TYPE_VOICE;
    }

    /**
     * Get the type of this phone object.
     *
     * @return string
     */
    public function get_type()
    {
        return \cge_utils::get_param($this->_data,'type');
    }

    /**
     * Set the type of this phone object.
     *
     * @param string $val
     */
    public function set_type($val)
    {
        $val = trim((string)$val);
        if( !$val ) throw new \Exception('invalid type for '.__CLASS__.' object');
        $this->_data['type'] = $val;
    }

    /**
     * Get the value (phone number) for this object.
     *
     * @return string
     */
    public function get_value()
    {
        return \cge_utils::get_param($this->_data,'value');
    }

    /**
     * Set the value (phone number) for this object.
     *
     * @param string $val
     */
    public function set_value($val)
    {
        $val = trim((string)$val);
        $this->_data['value'] = $val;
    }

    /**
     * Validate this object.
     * If an error occurs, this method throws exceptions.
     */
    public function validate()
    {
        parent::validate();
        if( $this->get_type() == '' ) throw new \CmsInvalidDataException('No type for phone object');
        if( $this->get_value() == '' ) throw new \CmsInvalidDataException('No value for phone object');
    }

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

    /**
     * Get this object representad as an array
     *
     * @return array
     */
    public function to_data()
    {
        $out = parent::to_data();
        $out['value'] = $this->get_value();
        $out['type'] = $this->get_type();
        return $out;
    }

    /**
     * Fill this object from an array.
     *
     * @param array $data
     */
    public function set_data($data)
    {
        parent::set_data($data);
        if( isset($data['value']) ) {
            $tmp = @unserialize($data['value']);
            if( is_array($tmp) ) {
                $this->_data = $tmp;
            }
            else {
                $this->_data['value'] = $data['value'];
                $this->_data['type'] = \cge_utils::get_param($data,'type');
            }
        }
    }
} // end of class

?>
