Development

Changeset 7576

You must first sign up to be able to contribute.

Changeset 7576

Show
Ignore:
Timestamp:
02/22/08 16:58:07 (9 months ago)
Author:
fabien
Message:

refactored sfToolkit::getArrayValueForPath() and added sfToolkit::hasArrayValueForPath()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/util/sfNamespacedParameterHolder.class.php

    r5226 r7576  
    192192    } 
    193193 
    194     if (false !== ($offset = strpos($name, '['))) 
    195     { 
    196       if (isset($this->parameters[$ns][substr($name, 0, $offset)])) 
    197       { 
    198         $array = $this->parameters[$ns][substr($name, 0, $offset)]; 
    199  
    200         while ($pos = strpos($name, '[', $offset)) 
    201         { 
    202           $end = strpos($name, ']', $pos); 
    203           if ($end == $pos + 1) 
    204           { 
    205             // reached a [] 
    206             return true; 
    207           } 
    208           else if (!isset($array[substr($name, $pos + 1, $end - $pos - 1)])) 
    209           { 
    210             return false; 
    211           } 
    212           $array = $array[substr($name, $pos + 1, $end - $pos - 1)]; 
    213           $offset = $end; 
    214         } 
    215  
    216         return true; 
    217       } 
    218     } 
    219     elseif (isset($this->parameters[$ns][$name])) 
     194    if (isset($this->parameters[$ns][$name])) 
    220195    { 
    221196      return true; 
     197    } 
     198    else if (isset($this->parameters[$ns])) 
     199    { 
     200      return sfToolkit::hasArrayValueForPath($this->parameters[$ns], $name); 
    222201    } 
    223202 
  • branches/1.1/lib/util/sfParameterHolder.class.php

    r5226 r7576  
    9292  public function has($name) 
    9393  { 
    94     if (false !== ($offset = strpos($name, '['))) 
    95     { 
    96       if (isset($this->parameters[substr($name, 0, $offset)])) 
    97       { 
    98         $array = $this->parameters[substr($name, 0, $offset)]; 
    99  
    100         while ($pos = strpos($name, '[', $offset)) 
    101         { 
    102           $end = strpos($name, ']', $pos); 
    103           if ($end == $pos + 1) 
    104           { 
    105             // reached a [] 
    106             return true; 
    107           } 
    108           else if (!isset($array[substr($name, $pos + 1, $end - $pos - 1)])) 
    109           { 
    110             return false; 
    111           } 
    112           $array = $array[substr($name, $pos + 1, $end - $pos - 1)]; 
    113           $offset = $end; 
    114         } 
    115  
    116         return true; 
    117       } 
    118     } 
    119     elseif (isset($this->parameters[$name])) 
     94    if (isset($this->parameters[$name])) 
    12095    { 
    12196      return true; 
     97    } 
     98    else 
     99    { 
     100      return sfToolkit::getArrayValueForPath($this->parameters, $name); 
    122101    } 
    123102 
  • branches/1.1/lib/util/sfToolkit.class.php

    r6867 r7576  
    497497  public static function getArrayValueForPath($values, $name, $default = null) 
    498498  { 
    499     if (false !== ($offset = strpos($name, '['))) 
    500     { 
    501       if (isset($values[substr($name, 0, $offset)])) 
    502       { 
    503         $array = $values[substr($name, 0, $offset)]; 
    504  
    505         while ($pos = strpos($name, '[', $offset)) 
    506         { 
    507           $end = strpos($name, ']', $pos); 
    508           if ($end == $pos + 1) 
    509           { 
    510             // reached a [] 
    511             break; 
    512           } 
    513           else if (!isset($array[substr($name, $pos + 1, $end - $pos - 1)])) 
    514           { 
    515             return $default; 
    516           } 
    517           $array = $array[substr($name, $pos + 1, $end - $pos - 1)]; 
    518           $offset = $end; 
    519         } 
    520  
    521         return $array; 
    522       } 
    523     } 
    524  
    525     return $default; 
     499    if (false === $offset = strpos($name, '[')) 
     500    { 
     501      return isset($values[$name]) ? $values[$name] : $default; 
     502    } 
     503 
     504    if (!isset($values[substr($name, 0, $offset)])) 
     505    { 
     506      return $default; 
     507    } 
     508 
     509    $array = $values[substr($name, 0, $offset)]; 
     510 
     511    while (false !== $pos = strpos($name, '[', $offset)) 
     512    { 
     513      $end = strpos($name, ']', $pos); 
     514      if ($end == $pos + 1) 
     515      { 
     516        // reached a [] 
     517        if (!is_array($array)) 
     518        { 
     519          return $default; 
     520        } 
     521        break; 
     522      } 
     523      else if (!isset($array[substr($name, $pos + 1, $end - $pos - 1)])) 
     524      { 
     525        return $default; 
     526      } 
     527      $array = $array[substr($name, $pos + 1, $end - $pos - 1)]; 
     528      $offset = $end; 
     529    } 
     530 
     531    return $array; 
     532  } 
     533 
     534  /** 
     535   * Returns true if the a path exists for the given array. 
     536   * 
     537   * @param array  The values to search 
     538   * @param string The token name 
     539   * 
     540   * @return Boolean 
     541   */ 
     542  public static function hasArrayValueForPath($values, $name) 
     543  { 
     544    if (false === $offset = strpos($name, '[')) 
     545    { 
     546      return array_key_exists($name, $values); 
     547    } 
     548 
     549    if (!isset($values[substr($name, 0, $offset)])) 
     550    { 
     551      return false; 
     552    } 
     553 
     554    $array = $values[substr($name, 0, $offset)]; 
     555    while (false !== $pos = strpos($name, '[', $offset)) 
     556    { 
     557      $end = strpos($name, ']', $pos); 
     558      if ($end == $pos + 1) 
     559      { 
     560        // reached a [] 
     561        return is_array($array); 
     562      } 
     563      else if (!isset($array[substr($name, $pos + 1, $end - $pos - 1)])) 
     564      { 
     565        return false; 
     566      } 
     567      $array = $array[substr($name, $pos + 1, $end - $pos - 1)]; 
     568      $offset = $end; 
     569    } 
     570 
     571    return true; 
    526572  } 
    527573 
  • branches/1.1/test/unit/util/sfNamespacedParameterHolderTest.php

    r5226 r7576  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(60, new lime_output_color()); 
     13$t = new lime_test(52, new lime_output_color()); 
    1414 
    1515// ->clear() 
     
    3737$t->is('bar', $ph->get('myfoo', null, 'symfony/mynamespace'), '->get() takes an optional namespace as its third argument'); 
    3838$t->is(null, $ph->get('myfoo'), '->get() can have the same key for several namespaces'); 
    39  
    40 $ph = new sfNamespacedParameterHolder(); 
    41 $ph->add(array('foo' => array( 
    42   'bar' => array( 
    43     'baz' => 'foo bar', 
    44   ), 
    45   'bars' => array('foo', 'bar'), 
    46 ))); 
    47 $t->is($ph->get('foo[bar][baz]'), 'foo bar', '->get() can take a multi-array key'); 
    48 $t->is($ph->get('foo[bars][1]'), 'bar', '->get() can take a multi-array key'); 
    49 $t->is($ph->get('foo[bars][2]'), null, '->get() returns null if the key does not exist'); 
    50 $t->is($ph->get('foo[bars][]'), array('foo', 'bar'), '->get() returns an array'); 
    51 $t->is($ph->get('foo[bars][]'), $ph->get('foo[bars]'), '->get() returns an array even if you omit the []'); 
    5239 
    5340// ->getNames() 
     
    10794$t->is($ph->has('myfoo', 'symfony/mynamespace'), true, '->has() returns true if the key exists in the namespace given as its second argument'); 
    10895 
    109 $ph = new sfNamespacedParameterHolder(); 
    110 $ph->add(array('foo' => array( 
    111   'bar' => array( 
    112     'baz' => 'foo bar', 
    113   ), 
    114   'bars' => array('foo', 'bar'), 
    115 ))); 
    116 $t->is($ph->has('foo[bar][baz]'), true, '->has() can takes a multi-array key'); 
    117 $t->is($ph->get('foo[bars][1]'), true, '->has() can takes a multi-array key'); 
    118 $t->is($ph->get('foo[bars][2]'), false, '->has() returns null is the key does not exist'); 
    119 $t->is($ph->has('foo[bars][]'), true, '->has() returns true if an array exists'); 
    120 $t->is($ph->get('foo[bars][]'), $ph->has('foo[bars]'), '->has() returns true for an array even if you omit the []'); 
    121  
    12296// ->hasNamespace() 
    12397$t->diag('->hasNamespace()'); 
     
    240214$t->diag('->serialize() ->unserialize()'); 
    241215$t->ok($ph == unserialize(serialize($ph)), 'sfNamespacedParameterHolder implements the Serializable interface'); 
     216 
     217// Array path as a key 
     218$t->diag('Array path as a key'); 
     219$ph = new sfNamespacedParameterHolder(); 
     220$ph->add(array('foo' => array('bar' => 'foo'))); 
     221$t->is($ph->has('foo[bar]'), true, '->has() can takes a multi-array key'); 
     222$t->is($ph->get('foo[bar]'), 'foo', '->has() can takes a multi-array key'); 
  • branches/1.1/test/unit/util/sfParameterHolderTest.php

    r5226 r7576  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(32, new lime_output_color()); 
     13$t = new lime_test(24, new lime_output_color()); 
    1414 
    1515// ->clear() 
     
    3232$ph = new sfParameterHolder(); 
    3333$t->is('default_value', $ph->get('foo1', 'default_value'), '->get() takes the default value as its second argument'); 
    34  
    35 $ph = new sfParameterHolder(); 
    36 $ph->add(array('foo' => array( 
    37   'bar' => array( 
    38     'baz' => 'foo bar', 
    39   ), 
    40   'bars' => array('foo', 'bar'), 
    41 ))); 
    42 $t->is($ph->get('foo[bar][baz]'), 'foo bar', '->get() can take a multi-array key'); 
    43 $t->is($ph->get('foo[bars][1]'), 'bar', '->get() can take a multi-array key'); 
    44 $t->is($ph->get('foo[bars][2]'), null, '->get() returns null if the key does not exist'); 
    45 $t->is($ph->get('foo[bars][]'), array('foo', 'bar'), '->get() returns an array'); 
    46 $t->is($ph->get('foo[bars][]'), $ph->get('foo[bars]'), '->get() returns an array even if you omit the []'); 
    4734 
    4835// ->getNames() 
     
    6754$t->is($ph->has('foo'), true, '->has() returns true if the key exists'); 
    6855$t->is($ph->has('bar'), false, '->has() returns false if the key does not exist'); 
    69  
    70 $ph = new sfParameterHolder(); 
    71 $ph->add(array('foo' => array( 
    72   'bar' => array( 
    73     'baz' => 'foo bar', 
    74   ), 
    75   'bars' => array('foo', 'bar'), 
    76 ))); 
    77 $t->is($ph->has('foo[bar][baz]'), true, '->has() can takes a multi-array key'); 
    78 $t->is($ph->get('foo[bars][1]'), true, '->has() can takes a multi-array key'); 
    79 $t->is($ph->get('foo[bars][2]'), false, '->has() returns null is the key does not exist'); 
    80 $t->is($ph->has('foo[bars][]'), true, '->has() returns true if an array exists'); 
    81 $t->is($ph->get('foo[bars][]'), $ph->has('foo[bars]'), '->has() returns true for an array even if you omit the []'); 
    8256 
    8357// ->remove() 
     
    150124$t->diag('->serialize() ->unserialize()'); 
    151125$t->ok($ph == unserialize(serialize($ph)), 'sfParameterHolder implements the Serializable interface'); 
     126 
     127// Array path as a key 
     128$t->diag('Array path as a key'); 
     129$ph = new sfParameterHolder(); 
     130$ph->add(array('foo' => array('bar' => 'foo'))); 
     131$t->is($ph->has('foo[bar]'), true, '->has() can takes a multi-array key'); 
     132$t->is($ph->get('foo[bar]'), 'foo', '->has() can takes a multi-array key'); 
  • branches/1.1/test/unit/util/sfToolkitTest.php

    r4319 r7576  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(70, new lime_output_color()); 
     13$t = new lime_test(92, new lime_output_color()); 
    1414 
    1515// ::stringToArray() 
     
    173173  '::arrayDeepMerge() recursively merges arrays preserving numerical keys' 
    174174); 
     175 
     176$arr = array( 
     177  'foobar' => 'foo', 
     178  'foo' => array( 
     179    'bar' => array( 
     180      'baz' => 'foo bar', 
     181    ), 
     182  ), 
     183  'bar' => array( 
     184    'foo', 
     185    'bar', 
     186  ), 
     187); 
     188 
     189// ::hasArrayValueForPath() 
     190$t->diag('::hasArrayValueForPath()'); 
     191 
     192$t->is(sfToolkit::hasArrayValueForPath($arr, 'foobar'), true, '::hasArrayValueForPath() returns true if the path exists'); 
     193$t->is(sfToolkit::hasArrayValueForPath($arr, 'barfoo'), false, '::hasArrayValueForPath() returns false if the path does not exist'); 
     194 
     195$t->is(sfToolkit::hasArrayValueForPath($arr, 'foo[bar][baz]'), true, '::hasArrayValueForPath() works with deep paths'); 
     196$t->is(sfToolkit::hasArrayValueForPath($arr, 'foo[bar][bar]'), false, '::hasArrayValueForPath() works with deep paths'); 
     197 
     198$t->is(sfToolkit::hasArrayValueForPath($arr, 'foo[]'), true, '::hasArrayValueForPath() accepts a [] at the end to check for an array'); 
     199$t->is(sfToolkit::hasArrayValueForPath($arr, 'foobar[]'), false, '::hasArrayValueForPath() accepts a [] at the end to check for an array'); 
     200$t->is(sfToolkit::hasArrayValueForPath($arr, 'barfoo[]'), false, '::hasArrayValueForPath() accepts a [] at the end to check for an array'); 
     201 
     202$t->is(sfToolkit::hasArrayValueForPath($arr, 'bar[1]'), true, '::hasArrayValueForPath() can take an array indexed by integer'); 
     203$t->is(sfToolkit::hasArrayValueForPath($arr, 'bar[2]'), false, '::hasArrayValueForPath() can take an array indexed by integer'); 
     204 
     205// ::getArrayValueForPath() 
     206$t->diag('::getArrayValueForPath()'); 
     207 
     208$t->is(sfToolkit::getArrayValueForPath($arr, 'foobar'), 'foo', '::getArrayValueForPath() returns the value of the path if it exists'); 
     209$t->is(sfToolkit::getArrayValueForPath($arr, 'barfoo'), null, '::getArrayValueForPath() returns null if the path does not exist'); 
     210$t->is(sfToolkit::getArrayValueForPath($arr, 'barfoo', 'bar'), 'bar', '::getArrayValueForPath() takes a default value as its third argument'); 
     211 
     212$t->is(sfToolkit::getArrayValueForPath($arr, 'foo[bar][baz]'), 'foo bar', '::getArrayValueForPath() works with deep paths'); 
     213$t->is(sfToolkit::getArrayValueForPath($arr, 'foo[bar][bar]'), false, '::getArrayValueForPath() works with deep paths'); 
     214$t->is(sfToolkit::getArrayValueForPath($arr, 'foo[bar][bar]', 'bar'), 'bar', '::getArrayValueForPath() works with deep paths'); 
     215 
     216$t->is(sfToolkit::getArrayValueForPath($arr, 'foo[]'), array('bar' => array('baz' => 'foo bar')), '::getArrayValueForPath() accepts a [] at the end to check for an array'); 
     217$t->is(sfToolkit::getArrayValueForPath($arr, 'foobar[]'), null, '::getArrayValueForPath() accepts a [] at the end to check for an array'); 
     218$t->is(sfToolkit::getArrayValueForPath($arr, 'barfoo[]'), null, '::getArrayValueForPath() accepts a [] at the end to check for an array'); 
     219$t->is(sfToolkit::getArrayValueForPath($arr, 'foobar[]', 'foo'), 'foo', '::getArrayValueForPath() accepts a [] at the end to check for an array'); 
     220 
     221$t->is(sfToolkit::getArrayValueForPath($arr, 'bar[1]'), 'bar', '::getArrayValueForPath() can take an array indexed by integer'); 
     222$t->is(sfToolkit::getArrayValueForPath($arr, 'bar[2]'), null, '::getArrayValueForPath() can take an array indexed by integer'); 
     223$t->is(sfToolkit::getArrayValueForPath($arr, 'bar[2]', 'foo'), 'foo', '::getArrayValueForPath() can take an array indexed by integer');