<?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 class to represent a name.
 * A name consists of a prefix, a first, middle, and last part, and a suffix.
 * Only one name object can be associated with a contact.
 */
class contact_name extends contact_sub
{
    /**
     * @ignore
     */
    private $_data;

    /**
     * Get the prefix portion of the name.
     *
     * @return string
     */
    public function get_prefix()
    {
        return $this->prefix;
    }

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

    /**
     * Get the firstname portion of the name.
     *
     * @return string
     */
    public function get_first()
    {
        return $this->first;
    }

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

    /**
     * Get the middle portion of the name.
     *
     * @return string
     */
    public function get_middle()
    {
        return $this->middle;
    }

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

    /**
     * Get the last portion of the name.
     *
     * @return string
     */
    public function get_last()
    {
        return $this->last;
    }

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

    /**
     * Get the name suffix.
     *
     * @return string
     */
    public function get_suffix()
    {
        return $this->suffix;
    }

    /**
     * Set the name suffix.
     *
     * @param string $val
     */
    public function set_suffix($val)
    {
        $this->_data['suffix'] = trim((string) $val);
    }

    /**
     * Get the name formatted in a displayable format.
     *
     * @return string
     */
    public function get_formatted()
    {
        $data = $this->_data;
        if( $data['middle'] && !endswith($data['middle'],'.') )  $data['middle'] .= '.';
        return implode(' ',$data);
    }

    /**
     * @ignore
     */
    public function __get($key)
    {
        $key = trim((string)$key);
        switch( $key ) {
        case 'prefix':
        case 'first':
        case 'middle':
        case 'last':
        case 'suffix':
            return \cge_utils::get_param($this->_data,$key);

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

    /**
     * @ignore
     */
    public function allows_multiple()
    {
        return FALSE;
    }

    /**
     * Represent this object as an array.
     *
     * @return array
     */
    public function to_data()
    {
        $out = parent::to_data();
        $out = array_merge($out,$this->_data);
        return $out;
    }

    /**
     * Validate this object.
     * Upon error, this method throws exceptions.
     */
    public function validate()
    {
        parent::validate();

        $fnd = 0;
        foreach( $this->_data as $key => $val ) {
            switch( $key ) {
            case 'prefix':
            case 'first':
            case 'middle':
            case 'last':
            case 'suffix':
                if( $val != '' ) $fnd++;
                break;
            }
        }
        if( $fnd == 0 ) throw new \CmsInvalidDataException('At least some data must be specified for a name object');
    }

    /**
     * Get the value of this object in a form that can be saved to the database.
     *
     * @return string
     */
    public function get_raw_value()
    {
        return serialize($this->_data);
    }

    /**
     * Fill this object from an array.
     *
     * @param array $data
     */
    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 'prefix':
                case 'first':
                case 'middle':
                case 'last':
                case 'suffix':
                    $this->_data[$key] = trim($val);
                    break;
                }
            }
        }
    }

} // end of class

?>
