# RegexEngine

trait RegexEngine

## Methods

`public` | `` | `validate($regex)` | No description |
`public` | `` | `work($regex, $rota)` | No description |
`public` | `` | `build($routes, $url)` | No description |

## Details

at line 10

### `validate()`

`public validate($regex)`

#### Parameters

| `$regex` |

at line 14

### `work()`

`public work($regex, $rota)`

#### Parameters

| `$regex` |
| `$rota` |

at line 37

### `build()`

`public build($routes, $url)`

#### Parameters

| `$routes` |
| `$url` |

## Source code

    <?php

          namespace App\Khan\Component\Router\RegexEngine;

          trait RegexEngine {

                private static $instance = null,
                               $prefix = "/[{}]/";

                public function validate($regex){
                  return preg_match(self::$prefix, $regex);
                }

                public function work($regex, $rota){
                    if($this->validate($regex)){
                        $m = []; $param = [];
                        $rex = explode('/', $regex);
                        $repleced = $regex;
                        foreach($rex as $key => $r){
                            if($this->validate($r)){
                              $m[] = $r;
                              $param[] = str_replace(['{','}'], '', $r);
                            }
                        }
                        $repleced = str_replace($m, '(.*)', $repleced);
                        $repleced = "/^".str_replace("/", "\/", $repleced)."$/";
                        $getters = [];
                        $rt = preg_match($repleced, $rota, $getters);
                        if($rt){
                          unset($getters[0]);
                          return array_combine($param, $getters);
                        }
                        return false;
                    }
                }

                public function build($routes, $url){
                  //$routes = self::$routess["params"];
                  $routerActive = [];
                  foreach($routes as $rota => $fn){
                    $lengRoute = explode("/", substr($rota, 1));
                      $lengUri = explode("/", substr($url, 1));
                    if($this->validate($rota)):
                      if(count($lengRoute) === count($lengUri)){
                        $routerActive[] = $rota;
                      }
                    endif;
                  }
                  if(count($routerActive) > 0){
                    foreach ($routerActive as $key => $rota) {
                      list($lengRoute, $lengUri) = [
                        explode("/", substr($rota, 1)),
                        explode("/", substr($url, 1))
                      ];
                        $validate = $this->work($rota, $url);
                        if(count($lengUri) == count($lengRoute) && $validate){
                        return ["rota" => $rota,"params" => $validate];
                        break;
                        }
                    }
                  }
                  return false;
                }

        }