src/Router.php
53e0de50
 <?php
 
 namespace elanpl\L3;
 
 class Router{
 
a2bd90a3
     protected $routes; // The defined routes collection
     protected $routeNameIndex; // An array with elements that reference to the routes ordered by a route names
8e695a75
     public $parsedParameters; // Parameters parsed from Request Path
     public $depth; // Number of nested nodes in Request Path
c6173b1c
     public $routeInfo; // The RouteInfo object if the route was matched
a2bd90a3
 
     public function __construct($routing)
53e0de50
     {
a2bd90a3
         $this->routes = array(); 
         $this->routeNameIndex = array();
         //Set the routing from configuration object
         $routing->set($this);
53e0de50
     }
 
c6173b1c
     public function add($method, $path, $result, $name = ''){
         $this->routes[] = new RouteInfo($method, $path, $result, $name);
         if(isset($name)&&$name!='') $this->routeNameIndex[$name] = &$this->routes[count($this->routes)-1];
a2bd90a3
         return $this;
53e0de50
     }
 
c6173b1c
     public function get($path, $result, $name = ''){
         return $this->add('GET', $path, $result, $name);
53e0de50
     }
 
c6173b1c
     public function post($path, $result, $name = ''){
         return $this->add('POST', $path, $result, $name);
53e0de50
     }
 
c6173b1c
     public function put($path, $result, $name = ''){
         return $this->add('PUT', $path, $result, $name);
53e0de50
     }
 
c6173b1c
     public function patch($path, $result, $name = ''){
         return $this->add('PATCH', $path, $result, $name);
53e0de50
     }
 
c6173b1c
     public function delete($path, $result, $name = ''){
         return $this->add('DELETE', $path, $result, $name);
53e0de50
     }
 
c6173b1c
     public function any($path, $result, $name = ''){
         return $this->add('ANY', $path, $result, $name);
53e0de50
     }
 
c6173b1c
     public function addBeforeAction($event_handler){
         $this->routes[count($this->routes)-1]->addBeforeAction($event_handler);
a2bd90a3
         return $this;
53e0de50
     }
 
c6173b1c
     public function addAfterAction($event_handler){
         $this->routes[count($this->routes)-1]->addAfterAction($event_handler);
a2bd90a3
         return $this;
53e0de50
     }
 
c6173b1c
     public function addAfterResult($event_handler){
         $this->routes[count($this->routes)-1]->addAfterAction($event_handler);
a2bd90a3
         return $this;
53e0de50
     }
 
c6173b1c
     public function match($request){
53e0de50
 
c6173b1c
         $auri = explode('/', trim($request->Path, "/ \t\n\r\0\x0B"));
53e0de50
         $curi = count($auri);
 			
a2bd90a3
         foreach ($this->routes as $routeInfo) {
53e0de50
             
c6173b1c
             $route = $routeInfo->path;
             $method = $routeInfo->method;
             if($method=='ANY' || strpos($request->method,$method)!==false){
53e0de50
                 $aroute = explode('/', trim($route, "/ \t\n\r\0\x0B"));
                 //print_r($aroute);
                 if($curi==count($aroute)){ //compare path element count
                     //optimistic assumption :)
                     $matchResult = true;
                     for($i = 0; $i<$curi; $i++){
                         $pathPartName = trim($aroute[$i],'{}');
                         if($aroute[$i]==$pathPartName){
                             if($auri[$i]!=$pathPartName){
                                 //echo "diffrence found";
                                 $matchResult = false;
                                 break;
                             }
                         }
                         else{ // {...} found -> catch $uri variable
                             $value = $auri[$i];
                             $valueKey = explode(':', $pathPartName);
                             //validation
                             if(isset($valueKey[1]) && $valueKey[1]=='int'){
                                 $value = intval($value);
                             }
                             //value store...
a2bd90a3
                             $this->parsedParameters[$valueKey[0]] = $value;
53e0de50
                         }
                     }
                     if($matchResult){ // match found
a2bd90a3
                         $this->depth = $curi;
c6173b1c
                         $this->routeInfo = $routeInfo;
53e0de50
                         return $routeInfo->Result;
                     }
                 }
             }
         }
         return false;
     }
 
a2bd90a3
     public function link($name, $parameters){
         $route = $this->routeNameIndex[$name];
53e0de50
         $fields = array_keys($parameters);
         $values = array_values($parameters);
         array_walk($fields, function (&$item, $key){
             $item = "/\{".$item."\}/";
         });
         return preg_replace($fields, $values, $route->Path);
     }
 
c6173b1c
     public function getParameter($name){
a2bd90a3
         return $this->parsedParameters[$name];
53e0de50
     }
 
 }