Arcanedev\Localization\Utilities\Url::substituteAttributes PHP Method

substituteAttributes() public static method

Change uri attributes (wildcards) for the ones in the $attributes array.
public static substituteAttributes ( array $attributes, string $uri ) : string
$attributes array
$uri string
return string
    public static function substituteAttributes(array $attributes, $uri)
    {
        foreach ($attributes as $key => $value) {
            if ($value instanceof RouteBindable) {
                $value = $value->getWildcardValue();
            }
            $uri = str_replace(['{' . $key . '?}', '{' . $key . '}'], $value, $uri);
        }
        // delete empty optional arguments that are not in the $attributes array
        return preg_replace('/\\/{[^)]+\\?}/', '', $uri);
    }

Usage Example

Example #1
0
 /**
  * Get URL from route name.
  *
  * @param  string      $locale
  * @param  string      $defaultLocale
  * @param  string      $transKey
  * @param  array       $attributes
  * @param  bool|false  $defaultHidden
  *
  * @return string
  */
 public function getUrlFromRouteName($locale, $defaultLocale, $transKey, $attributes = [], $defaultHidden = false)
 {
     if (!is_string($locale)) {
         $locale = $defaultLocale;
     }
     $route = '';
     if (!($locale === $defaultLocale && $defaultHidden)) {
         $route = '/' . $locale;
     }
     if ($this->hasTranslation($transKey, $locale)) {
         $translation = $this->trans($transKey, $locale);
         $route = Url::substituteAttributes($attributes, $route . '/' . $translation);
     }
     return $route;
 }