<?php
/**
 * CGBetterUploads API
 *
 * @package CGBetterUploads
 */
namespace CGBetterUploads;

/**
 * This class is used to hold parameters representing a filter.
 * The filter object is then used to build a database query for various types of objects.
 *
 * @property-read int $limit
 * @property-read int $offset
 */
abstract class Filter
{
    /**
     * @ignore
     */
    private $_data = [ 'limit'=>1000, 'offset'=> 0 ];

    /**
     * Constructor
     *
     * @param array $in Array of properties and values
     */
    protected function __construct( array $in )
    {
        foreach( $in as $key => $val ) {
            switch( $key ) {
            case 'limit':
                $this->_data[$key] = max(1,(int)$val);
                break;
            case 'offset':
                $this->_data[$key] = max(0,(int)$val);
                break;
            }
        }
    }

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

        case 'offset':
            return (int) $this->_data[$key];

        default:
            throw new \LogicException("$key is not a gettable property of ".get_class($this));
        }
    }

    /**
     * @ignore
     */
    public function __set( string $key, $val )
    {
        throw new \LogicException("$key is not a settable property of ".get_class($this));
    }

} // class
