<?php

/**
 * CGBetterUploads API
 *
 * @package CGBetterUploads
 */

namespace CGBetterUploads;

/**
 * A class to represent file type information.
 * This module uses this object to calculate an icon and displayable 'type' for a file.
 * The file type information is only used for display purposes.
 *
 * @property-read int $id The internal id of this filetype
 * @property-read string $name The displayable name of the filetype
 * @property-read string $icon The URL to the icon for this filetype
 * @property-read string $extensions A comma separated list of file extensions that can be used to identify a file of this type.
 * @property-read string $mimetypes A comma separated list of mime type strings that can be used to identify a file of this type.
 * @property-read i9nt $iorder The order of this filetype object to be used in processing, and admin display.
 */
class FileType
{
    /**
     * @ignore
     */
    private $_data = [ 'id'=>null, 'name'=>null, 'icon'=>null, 'extensions'=>null, 'mimetypes'=>null, 'iorder'=>null ];

    /**
     * @ignore
     */
    public function __get( string $key )
    {
        switch( $key ) {
        case 'name':
        case 'icon':
        case 'extensions':
        case 'mimetypes':
            if( !is_null($this->_data[$key]) ) return trim($this->_data[$key]);
            return;
        case 'id':
        case 'iorder':
            if( !is_null($this->_data[$key]) ) return (int) $this->_data[$key];
            return;
        default:
            throw new \LogicException("$key is not a gettable property of ".__CLASS__);
        }
    }

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

    /**
     * Adjust a property of this object.
     *
     * @param string $key The property name.  One of name, icon, extensions or mimetypes.
     * @param string $val
     * @return FileType
     */
    public function with( string $key, string $val ) : FileType
    {
        switch( $key ) {
        case 'name':
        case 'icon':
        case 'extensions':
        case 'mimetypes':
            $obj = clone $this;
            if( !is_null($val) ) $val = trim($val);
            $obj->_data[$key] = $val;
            return $obj;

        default:
            throw new \InvalidArgumentException("invalid data passed to ".__METHOD__);
        }
    }

    /**
     * Represent this object as an array
     *
     * @internal
     */
    public function to_array() : array
    {
        return $this->_data;
    }

    /**
     * Create a filetype object from an array
     *
     * @internal
     * @param array $in
     * @return FileType
     */
    public static function from_array( array $in ) : FileType
    {
        $obj = new self;
        foreach( $in as $key => $val ) {
            switch( $key ) {
            case 'name':
            case 'icon':
            case 'extensions':
            case 'mimetypes':
                $obj->_data[$key] = trim($val);
                break;
            case 'id':
            case 'iorder':
                $obj->_data[$key] = (int) $val;
                break;
            }
        }
        return $obj;
    }
} // clss
