<?php

namespace CGClientTickets\Repository;

/**
 * This class generates a ticket list based on various criteria.
 * It is used for the tickets tab in the admin interface, and possibly the
 * frontend user ticket list interface.
 */
class MessageListQuery extends \CGExtensions\query\query
{
    private $_data = array('limit'=>100,'offset'=>0,'ticket_id'=>null);

    public function __construct($parms = array()) {
        // @todo: check the params
        foreach( $parms as $key => $val ) {
            $this[$key] = $val;
        }
    }

    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__);
        }
    }

    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__);
        }
    }


    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__);
        }
    }

    public function &execute()
    {
        $obj = new MessageListResultset($this);
        return $obj;
    }
} // end of class

?>
