src/UriTemplate/UriTemplate.php
public static function expand($template, $variables, array $options = array())
{
    if (is_object($variables)) {
        $variables = get_object_vars($variables);
    } elseif (!is_array($variables)) {
        throw new UriTemplateException('$variables must be an array or object');
    }

    $result = $template;
    $errors = array();
    $expressions = self::getExpressions($template, $options);
    $i = count($expressions);
    while ($i-- > 0) {
        try {
            $expression = $expressions[$i];
            if (!$expression['valid']) {
                throw new UriTemplateException('Malformed expression: "%s" at offset %d', array($expression['complete'], $expression['offset']));
            }
            $sub = UriTemplate::expandExpression($expression['expression'], $variables, isset($options['keySort']) ? (bool)$options['keySort'] : false);
            $result = substr_replace($result, $sub, $expression['offset'], strlen($expression['complete']));
        } catch (UriTemplateException $e) {
            $errors[] = $e->getMessage();
            continue;
        }
    }

    if ($errors) {
        throw new UriTemplateException('Invalid URI Template string: "%s"', array($template), $errors);
    }
    return $result;
}
public static function expand($template, $variables, array $options = array())
{
    if (is_object($variables)) {
        $variables = get_object_vars($variables);
    } elseif (!is_array($variables)) {
        throw new UriTemplateException('$variables must be an array or object');
    }

    $result = $template;
    $errors = array();
    $expressions = self::getExpressions($template, $options);
    $i = count($expressions);
    while ($i-- > 0) {
        try {
            $expression = $expressions[$i];
            if (!$expression['valid']) {
                throw new UriTemplateException('Malformed expression: "%s" at offset %d', array($expression['complete'], $expression['offset']));
            }
            $sub = UriTemplate::expandExpression($expression['expression'], $variables, isset($options['keySort']) ? (bool)$options['keySort'] : false);
            $result = substr_replace($result, $sub, $expression['offset'], strlen($expression['complete']));
        } catch (UriTemplateException $e) {
            $errors[] = $e->getMessage();
            continue;
        }
    }

    if ($errors) {
        throw new UriTemplateException('Invalid URI Template string: "%s"', array($template), $errors);
    }
    return $result;
}
public static function expand ( $ template , $ variables , array $ options = array ( ) ) { if ( is_object ( $ variables ) ) { $ variables = get_object_vars ( $ variables ) ; } elseif ( ! is_array ( $ variables ) ) { throw new UriTemplateException ( '$variables must be an array or object' ) ; } $ result = $ template ; $ errors = array ( ) ; $ expressions = self :: getExpressions ( $ template , $ options ) ; $ i = count ( $ expressions ) ; while ( $ i -- > 0 ) { try { $ expression = $ expressions [ $ i ] ; if ( ! $ expression [ 'valid' ] ) { throw new UriTemplateException ( 'Malformed expression: "%s" at offset %d' , array ( $ expression [ 'complete' ] , $ expression [ 'offset' ] ) ) ; } $ sub = UriTemplate :: expandExpression ( $ expression [ 'expression' ] , $ variables , isset ( $ options [ 'keySort' ] ) ? ( bool ) $ options [ 'keySort' ] : false ) ; $ result = substr_replace ( $ result , $ sub , $ expression [ 'offset' ] , strlen ( $ expression [ 'complete' ] ) ) ; } catch ( UriTemplateException $ e ) { $ errors [ ] = $ e -> getMessage ( ) ; continue ; } } if ( $ errors ) { throw new UriTemplateException ( 'Invalid URI Template string: "%s"' , array ( $ template ) , $ errors ) ; } return $ result ; }
Expand template as a URI Template using variables.

Throws a UriTemplate exception if the template is invalid.
The exception object has a getErrors() method that returns a list of individual errors.

@param  string $template  URI template
@param  mixed  $variables Array or object containing values to insert
@param  array  $options   Optional settings - open, close, sortKeys
open - string to mark the beginning of a substitution expression
close - string to mark the end of a substitution expression
sortKeys - sort arrays by key in output
@return string            Expanded URI
Expand template as a URI Template using variables .