Location: PHPKode > projects > GnomePHP > peec-GnomePHP-b5a360b/gnomephp/mvc/router/Rule.php
<?php
namespace gnomephp\mvc\router;

class Rule{
	const R_GET = 0;
	const R_POST = 1;
	const R_ALL = 2;

	/**
	 * This is the request type of the rule.
	 * It can be one of the R_ constants in this class.
	 *
	 * @var int
	 */
	private $requestType;
	/**
	 * From field parser.
	 * @var gnomephp\mvc\router\FromField
	 */
	private $from;
	/**
	 * To Field parser
	 * @var gnomephp\mvc\router\FromField
	 */
	private $to;


	private $validated = false;


	/**
	 * Constructs a new router rule.
	 *
	 * @param int $type One of R_GET, R_POST or R_ALL
	 * @param unknown_type $from From a specific pattern.
	 * @param unknown_type $to
	 */
	public function __construct($type, $from, $to){
		$this->requestType = $type;
		$this->from = new FromField($from);
		$this->to = new ToField($to);
	}

	/**
	 * Tries to reverse controller, method and argument to a uri.
	 * 
	 */
	public function reverse($controller, $method, $args){
		// Validate the To pattern.
		$additionalArgs = $this->to->reverseValidate($controller, $method, $args);

		if (is_array($additionalArgs)){
			$args = array_merge($additionalArgs, $args);
			// Okay, this is what we are looking for.
				
				
			$op = $this->from->reverse($args)->setActive($this->validated);
			if (substr($op, -2) == '/?'){
				$op = substr($op, 0, -2);
			}
			return $op;
		}else{
			return false;
		}
	}

	public function validate($request, $url){

		// If the request type is OK
		if ($this->requestType == Rule::R_ALL || $request === $this->requestType){
				
			$regxp =  $this->from->toRegexp();

			// Match from, is it matching ?
			if (preg_match($regxp, $url, $values)){


				
				// Get the var -> values
				$vars = $this->from->getVars($values);

				$this->validated = true;
				// Return the components
				return $this->to->getComponent($vars);
			}
		}
		return null;
	}
}
Return current item: GnomePHP