<?php

/**
 * The <strong>Request</strong> class is responsible for parsing the URI to detect the controller,
 * methods, and arguments.<br />
 * <strong>Note:</strong> the instantiation of this class must be wrapped ONLY in a Router object.
 * Usage is available in the Example section below
 *
 * @example <tt>$route = new Router(new Request)</tt>
 * @copyright Trusto Tech 2015-2016
 * @package System\Core
 */
class Request {

    /**
     * @var string Holds controller name
     */
    private $_controller;

    /**
     * @var string Holds method name
     */
    private $_method;

    /**
     * @var array Holds an array of arguments
     */
    private $_args;

    /**
     * Detect the controller, method, and arguments in the URI and defines them in their var. 
     * If the URI is empty, the class
     * will set the vars to the default controller, method, and an empty array of args.
     *
     * @return string Name of the controller
     */
    public function __construct() {
        $parts = array();
        if (empty($_GET['url'])) //we fill landing page in $_GET['url'], if it is empty since this variable some times used direclty
            $_GET['url'] = (empty(smc_session()['user_details']['landing']) ? "" : smc_session()['user_details']['landing']);
        $url = $_GET['url'];
        if (!empty($url)) {
            $parts = explode('/', $url);
            $parts = array_filter($parts);
        }
        //
        $this->_controller = ($current = array_shift($parts)) ? $current : 'smc';
        $this->_method = ($current = array_shift($parts)) ? $current : 'landing';
        $this->_args = (isset($parts[0])) ? $parts : array();
    }

    /**
     * Get the controller name
     *
     * @return string Name of the controller.
     */
    public function getController() {
        return $this->_controller;
    }

    /**
     * Get the method name
     *
     * @return string Name of the method.
     */
    public function getMethod() {
        return $this->_method;
    }

    /**
     * Get the arguments
     *
     * @return array Array of arguments.
     */
    public function getArgs() {
        return $this->_args;
    }

}
