<?php
/**
 * Request class
 * @author Jérôme Brilland
 * @version 2.0
 * @copyright (c) 2009-2021, Jérôme Brilland
 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License 
 */
class Request {

    /**
     * @var array $request Request
     * @access protected
     */
    protected $request;

    /**
     * @var string $method Request method
     * @access protected
     */
    protected $method;

    /**
     * @var string $uri Request URI
     * @access protected
     */
    protected $uri;

    /**
     * @var array $attributes Attributes derived from the request
     * @access protected
     */
    protected $attributes = [];

    /**
     * Class constructor
     * @access public
     */
    public function __construct() {
        $this->request = array_merge( $_POST, $_GET );
        $this->method = $_SERVER['REQUEST_METHOD'];
        $this->uri = $_SERVER['REQUEST_URI'];
    }

    /**
     * Retrieve the HTTP method of the request.
     * @return string the request method
     * @access public
     */
    public function getMethod() {
        return $this->method;
    }

    /**
     * @return string
     * @access public
     */
    public function getUri() {
        return $this->uri;
    }

    /**
     * @access public
     */
    public function getParameter( $key, $default = null ) {
        return ( isset( $this->request[$key] ) ) ? $this->request[$key] : $default;
    }

    /**
     * @return boolean
     * @access public
     */
    public function hasParameter( $key ) {
        return array_key_exists( $key, $this->request );
    }

    /**
     * @return integer
     * @access public
     */
    public function getInt( $key, $default = null ) {
        return filter_var( $this->getParameter( $key, $default ), FILTER_SANITIZE_NUMBER_INT );
    }

    /**
     * @return float
     * @access public
     */
    public function getFloat( $key ) {
        return filter_var( $this->getParameter( $key, $default ), FILTER_SANITIZE_NUMBER_FLOAT );
    }

    /**
     * @return string
     * @access public
     */
    public function getAlpha( $key, $default = null ) {
        return preg_replace( '/[^[:alpha:]]/', '', $this->getParameter( $key, $default ) );
    }

    /**
     * @return string
     * @access public
     */
    public function getAlnum( $key, $default = null ) {
        return preg_replace( '/[^[:alnum:]]/', '', $this->getParameter( $key, $default ) );
    }

    /**
     * @return string
     * @access public
     */
    public function getString( $key, $default = null ) {
        return filter_var( $this->getParameter( $key, $default ), FILTER_SANITIZE_STRING );
    }

    /**
     * @return string
     * @access public
     */
    public function getWord( $key, $default = null ) {
        return preg_replace( '/[\W]/', '', $this->getParameter( $key, $default ) );
    }

    /**
     * @return string
     * @access public
     */
    public function getURL( $key, $default = null ) {
        return filter_var( $this->getParameter( $key, $default ), FILTER_SANITIZE_URL );
    }

    /**
     * @return boolean
     * @access public
     */
    public function getBoolean( $key, $default = false ) {
        return (boolean) $this->getParameter( $key, $default );
    }

    /**
     * @return array
     * @access public
     */
    public function getArray( $key, $default = null ) {
        return ( is_array( $this->getParameter( $key, $default ) ) ) ? $this->getParameter( $key ) : null;
    }

    /**
     * Set single derived request attribute
     * @param string $key the attribute key
     * @param mixed $value the attribute value
     * @access public
     */
    public function setAttribute( $key, $value ) {
        $this->attributes[$key] = $value;
    }

    /**
     * Retrieve a single derived request attribute
     * @param string $key the attribute key
     * @access public
     */
    public function getAttribute( $key, $default = null ) {
        return ( isset( $this->attributes[$key] ) ) ? $this->attributes[$key] : $default;
    }

    /**
     * Retrieve attributes derived from the request
     * @return array Attributes derived from the request
     * @access public
     */
    public function getAttributes() {
        return $this->attributes;
    }

}

