Development

Changeset 8532

You must first sign up to be able to contribute.

Changeset 8532

Show
Ignore:
Timestamp:
04/18/08 19:25:28 (6 months ago)
Author:
fabien
Message:

added support for multi-array keys in sf*ParameterHolder::remove

Files:

Legend:

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

    r7975 r8532  
    240240      unset($this->parameters[$ns][$name]); 
    241241    } 
     242    else 
     243    { 
     244      $retval = sfToolkit::removeArrayValueForPath($this->parameters[$ns], $name, $default); 
     245    } 
    242246 
    243247    return $retval; 
  • branches/1.1/lib/util/sfParameterHolder.class.php

    r8528 r8532  
    121121      unset($this->parameters[$name]); 
    122122    } 
     123    else 
     124    { 
     125      $retval = sfToolkit::removeArrayValueForPath($this->parameters, $name, $default); 
     126    } 
    123127 
    124128    return $retval; 
  • branches/1.1/lib/util/sfToolkit.class.php

    r7965 r8532  
    564564 
    565565  /** 
     566   * Removes a path for the given array. 
     567   * 
     568   * @param array  The values to search 
     569   * @param string The token name 
     570   * @param mixed  The default value to return if the name does not exist 
     571   */ 
     572  public static function removeArrayValueForPath(&$values, $name, $default = null) 
     573  { 
     574    if (false === $offset = strpos($name, '[')) 
     575    { 
     576      if (isset($values[$name])) 
     577      { 
     578        $value = $values[$name]; 
     579        unset($values[$name]); 
     580 
     581        return $value; 
     582      } 
     583      else 
     584      { 
     585        return $default; 
     586      } 
     587    } 
     588 
     589    if (!isset($values[substr($name, 0, $offset)])) 
     590    { 
     591      return $default; 
     592    } 
     593 
     594    $value = &$values[substr($name, 0, $offset)]; 
     595 
     596    while (false !== $pos = strpos($name, '[', $offset)) 
     597    { 
     598      $end = strpos($name, ']', $pos); 
     599      if ($end == $pos + 1) 
     600      { 
     601        // reached a [] 
     602        if (!is_array($value)) 
     603        { 
     604          return $default; 
     605        } 
     606        break; 
     607      } 
     608      else if (!isset($value[substr($name, $pos + 1, $end - $pos - 1)])) 
     609      { 
     610        return $default; 
     611      } 
     612 
     613      $parent = &$value; 
     614      $key = substr($name, $pos + 1, $end - $pos - 1); 
     615      $value = &$value[$key]; 
     616      $offset = $end; 
     617    } 
     618 
     619    if ($key) 
     620    { 
     621      unset($parent[$key]); 
     622    } 
     623 
     624    return $value; 
     625  } 
     626 
     627  /** 
    566628   * Get path to php cli. 
    567629   * 
  • branches/1.1/test/unit/util/sfNamespacedParameterHolderTest.php

    r7576 r8532  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(52, new lime_output_color()); 
     13$t = new lime_test(54, new lime_output_color()); 
    1414 
    1515// ->clear() 
     
    221221$t->is($ph->has('foo[bar]'), true, '->has() can takes a multi-array key'); 
    222222$t->is($ph->get('foo[bar]'), 'foo', '->has() can takes a multi-array key'); 
     223$t->is($ph->remove('foo[bar]'), 'foo', '->remove() can takes a multi-array key'); 
     224$t->is($ph->getAll(), array('foo' => array()), '->remove() can takes a multi-array key'); 
  • branches/1.1/test/unit/util/sfParameterHolderTest.php

    r7576 r8532  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(24, new lime_output_color()); 
     13$t = new lime_test(26, new lime_output_color()); 
    1414 
    1515// ->clear() 
     
    131131$t->is($ph->has('foo[bar]'), true, '->has() can takes a multi-array key'); 
    132132$t->is($ph->get('foo[bar]'), 'foo', '->has() can takes a multi-array key'); 
     133$t->is($ph->remove('foo[bar]'), 'foo', '->remove() can takes a multi-array key'); 
     134$t->is($ph->getAll(), array('foo' => array()), '->remove() can takes a multi-array key'); 
  • branches/1.1/test/unit/util/sfToolkitTest.php

    r7936 r8532  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(93, new lime_output_color()); 
     13$t = new lime_test(99, new lime_output_color()); 
    1414 
    1515// ::stringToArray() 
     
    234234$t->is(sfToolkit::getArrayValueForPath($arr, 'bar[2]'), null, '::getArrayValueForPath() can take an array indexed by integer'); 
    235235$t->is(sfToolkit::getArrayValueForPath($arr, 'bar[2]', 'foo'), 'foo', '::getArrayValueForPath() can take an array indexed by integer'); 
     236 
     237// ::removeArrayValueForPath() 
     238$t->diag('::removeArrayValueForPath()'); 
     239$t->is(sfToolkit::removeArrayValueForPath($arr, 'foobar'), 'foo', '::removeArrayValueForPath() returns the removed value'); 
     240$t->is($arr, array( 
     241  'foo' => array( 
     242    'bar' => array( 
     243      'baz' => 'foo bar', 
     244    ), 
     245  ), 
     246  'bar' => array( 
     247    'foo', 
     248    'bar', 
     249  ), 
     250), '::removeArrayValueForPath() removes a key'); 
     251$t->is(sfToolkit::removeArrayValueForPath($arr, 'barfoo'), null, '::removeArrayValueForPath() returns null if the key does not exist'); 
     252$t->is(sfToolkit::removeArrayValueForPath($arr, 'barfoo', 'bar'), 'bar', '::removeArrayValueForPath() takes the default value as a third argument'); 
     253 
     254$t->is(sfToolkit::removeArrayValueForPath($arr, 'foo[bar][baz]'), 'foo bar', '::removeArrayValueForPath() works with deep paths'); 
     255$t->is($arr, array( 
     256  'foo' => array( 
     257    'bar' => array( 
     258    ), 
     259  ), 
     260  'bar' => array( 
     261    'foo', 
     262    'bar', 
     263  ), 
     264), '::removeArrayValueForPath() works with deep paths');