<?php

/**
 * This file defines the MessageListQuery class.
 *
 * @package CGClientTickets
 * @category Application
 * @author  calguy1000 <calguy1000@cmsmadesimple.org>
 * @copyright Copyright 2010 by Robert Campbell
 */

namespace CGClientTickets\Repository;

/**
 * This class manages the critera for a MessageListResultset.
 *
 * It is used for the tickets tab in the admin interface, and possibly the
 * frontend user ticket list interface.
 *
 * @property int $ticket_id The ticket to retrieve messages for.
 * @property int $limit The number of messages to return, default value is 100.
 * @property int $offset The offset within all applicable matches of messages to return.
 */
class MessageListQuery extends \CGExtensions\query\query
{
    /**
     * @ignore
     */
    private $_data = array('limit'=>100,'offset'=>0,'ticket_id'=>null);

    /**
     * Constructor
     *
     * @param array $parms The initial properties for this object.
     */
    public function __construct($parms = array()) {
        // @todo: check the params
        foreach( $parms as $key => $val ) {
            $this[$key] = $val;
        }
    }

    /**
     * @ignore
     */
    public function OffsetGet($key)
    {
        switch( $key ) {
        case 'offset':
        case 'limit':
        case 'ticket_id':
            return $this->_data[$key];

        default:
            throw new \RuntimeException($key.' is an invalid parameter for a '.__CLASS__);
        }
    }

    /**
     * @ignore
     */
    public function OffsetSet($key,$val)
    {
        switch( $key ) {
        case 'offset':
            $val = (int) $val;
            if( $val < 0 ) throw new \RuntimeException("Invalid value for offset in a ".__CLASS__);
            $this->_data[$key] = $val;
            break;

        case 'limit':
            $val = (int) $val;
            if( $val < 0 || $val > 1000 ) throw new \RuntimeException("Invalid value for limit in a ".__CLASS__);
            $this->_data[$key] = $val;
            break;

        case 'ticket_id':
            $val = (int) $val;
            if( $val < 1 ) throw new \RuntimeException("Invalid value for ticket_id in a ".__CLASS__);
            $this->_data[$key] = $val;
            break;

        default:
            throw new \RuntimeException($key.' is an invalid parameter for a '.__CLASS__);
        }
    }

    /**
     * @ignore
     */
    public function OffsetExists($key)
    {
        switch( $key ) {
        case 'offset':
        case 'limit':
        case 'ticket_id':
            return isset($this->_data[$key]);

        default:
            throw new \RuntimeException($key.' is an invalid parameter for a '.__CLASS__);
        }
    }

    /**
     * Execute the query and generate a result set.
     *
     * @return MessageListResultset
     */
    public function &execute()
    {
        $obj = new MessageListResultset($this);
        return $obj;
    }
} // end of class

?>
