Blame view

sources/3rdparty/kriswallsmith/assetic/src/Assetic/Util/VarUtils.php 2.09 KB
6d9380f96   Cédric Dupont   Update sources OC...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  <?php
  
  /*
   * This file is part of the Assetic package, an OpenSky project.
   *
   * (c) 2010-2014 OpenSky Project Inc
   *
   * For the full copyright and license information, please view the LICENSE
   * file that was distributed with this source code.
   */
  
  namespace Assetic\Util;
  
  /**
   * Variable utilities.
   *
   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
   */
  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 template "%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;
      }
  
      final private function __construct() { }
  }