Simple route validation

Submitted by eugene_santos - 10 years ago

Will validate a route pattern against the current route.

    /**
     * isValidL4Route
     * 
     * @param  string  $route_url  url that comes from Route::getRoutes() (user/{id})
     * @param  string  $currentUrl url i.e. from Request::path()
     * @return boolean
     */
    function isValidL4Route($route_url, $currentUrl) {

        $route_segments = explode('/', $route_url);
        $currnt_segments = explode('/', $currentUrl);

        if (count($route_segments) != count($currnt_segments)) {
            return FALSE;
        }

        $route_url = rtrim($route_url, '/');
        $currentUrl = rtrim($currentUrl, '/');

        $num_segments = count($route_segments);

        $errors = array();
        for ($i=0; $i < $num_segments; $i++) { 
            if (preg_match('|^\{(:?.+)\}$|', $route_segments[$i])) {
                if ($currnt_segments[$i] == '' ) {
                    $error[] = 'segment empty';
                }
            } else {
                if ($route_segments[$i] != $currnt_segments[$i]) {
                    $errors[] = 'non curly brace not matched';
                }
            }
        }

        return empty($errors); 
    }