abstract class VarUtils

Variable utilities.

@author Johannes M. Schmitt <schmittjoh@gmail.com>

Hierarchy

Expanded class hierarchy of VarUtils

4 files declare their use of VarUtils
AssetWriter.php in drupal/core/vendor/kriswallsmith/assetic/src/Assetic/AssetWriter.php
FileAsset.php in drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Asset/FileAsset.php
GlobAsset.php in drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Asset/GlobAsset.php
HttpAsset.php in drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Asset/HttpAsset.php

File

drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Util/VarUtils.php, line 19

Namespace

Assetic\Util
View source
abstract class VarUtils {

  /**
   * Resolves variable placeholders.
   *
   * @param string $template A template string
   * @param array  $vars     Variable names
   * @param array  $values   Variable values
   *
   * @return string The resolved string
   *
   * @throws \InvalidArgumentException If there is a variable with no value
   */
  public static function resolve($template, array $vars, array $values) {
    $map = array();
    foreach ($vars as $var) {
      if (false === strpos($template, '{' . $var . '}')) {
        continue;
      }
      if (!isset($values[$var])) {
        throw new \InvalidArgumentException(sprintf('The path "%s" contains the variable "%s", but was not given any value for it.', $template, $var));
      }
      $map['{' . $var . '}'] = $values[$var];
    }
    return strtr($template, $map);
  }
  public static function getCombinations(array $vars, array $values) {
    if (!$vars) {
      return array(
        array(),
      );
    }
    $combinations = array();
    $nbValues = array();
    foreach ($values as $var => $vals) {
      if (!in_array($var, $vars, true)) {
        continue;
      }
      $nbValues[$var] = count($vals);
    }
    for ($i = array_product($nbValues), $c = $i * 2; $i < $c; $i++) {
      $k = $i;
      $combination = array();
      foreach ($vars as $var) {
        $combination[$var] = $values[$var][$k % $nbValues[$var]];
        $k = intval($k / $nbValues[$var]);
      }
      $combinations[] = $combination;
    }
    return $combinations;
  }
  private final function __construct() {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
VarUtils::getCombinations public static function
VarUtils::resolve public static function Resolves variable placeholders.
VarUtils::__construct final private function