src/RouteInfo.php
4db6a72b
 <?php
 
 namespace elanpl\L3;
 
 class RouteInfo{
     // URL pattern path
c6173b1c
     public $path;
4db6a72b
     // Http method (ex. GET, POST, ANY)
c6173b1c
     public $method;
4db6a72b
     // The assigned structure (string, array, function...)
c6173b1c
     public $result;
4db6a72b
     // The route name
c6173b1c
     public $name;
4db6a72b
     // BeforeAction event handlers;
c6173b1c
     public $beforeAction;
4db6a72b
     // AfterAction event handlers;
c6173b1c
     public $afterAction;
4db6a72b
     // AfterResult event handlers;
c6173b1c
     public $afterResult;
4db6a72b
        
c6173b1c
     public function __construct($method, $path, $result, $name = '')
4db6a72b
     {
c6173b1c
         $this->path = $path;
         $this->method = $method;
         $this->result = $result;
         $this->name = $name;
         $this->beforeAction = array();
         $this->afterAction = array();
         $this->afterResult = array();
4db6a72b
     }
 
c6173b1c
     public function addBeforeAction($event_handler){
         if($this->eventHandlerFormatCheck($event_handler, $match)){
             $this->beforeAction[] = $event_handler;
4db6a72b
             return $this;
         }
         else{
             throw new \Exception("Wrong event handler format: $event_handler");
         }
     }
 
c6173b1c
     public function addAfterAction($event_handler){
         if($this->eventHandlerFormatCheck($event_handler, $match)){
             $this->afterAction[] = $event_handler;
4db6a72b
             return $this;
         }
         else{
             throw new \Exception("Wrong event handler format: $event_handler");
         }
     }
 
c6173b1c
     public function addAfterResult($event_handler){
         if($this->eventHandlerFormatCheck($event_handler, $match)){
             $this->afterResult[] = $event_handler;
4db6a72b
             return $this;
         }
         else{
             throw new \Exception("Wrong event handler format: $event_handler");
         }
     }
 
c6173b1c
     public static function eventHandlerFormatCheck($event_handler, &$match){
8f95fb66
         $pattern = '#^((?<class>[a-z0-9\\\\]+)::)?(?<function>[a-z0-9]+)(\\((?<arguments>[a-z0-9;,- ]+)?\\))?$#i';
4db6a72b
         if(preg_match($pattern, $event_handler, $match)){
             if(isset($match['arguments'])){
c6173b1c
                 $result = self::eventHandlerArgumentsFormatCheck($match['arguments'],$arguments_match);
4db6a72b
                 return $result;
             }
             else{
                 return 1;
             }
         }
         else{
             return 0;
         }
     }
 
c6173b1c
     public static function eventHandlerArgumentsFormatCheck($arguments, &$match){
4db6a72b
         $match = explode(',', $arguments);
         return $match;
     }
 
  }