Changeset 8532
- Timestamp:
- 04/18/08 19:25:28 (6 months ago)
- Files:
-
- branches/1.1/lib/util/sfNamespacedParameterHolder.class.php (modified) (1 diff)
- branches/1.1/lib/util/sfParameterHolder.class.php (modified) (1 diff)
- branches/1.1/lib/util/sfToolkit.class.php (modified) (1 diff)
- branches/1.1/test/unit/util/sfNamespacedParameterHolderTest.php (modified) (2 diffs)
- branches/1.1/test/unit/util/sfParameterHolderTest.php (modified) (2 diffs)
- branches/1.1/test/unit/util/sfToolkitTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/util/sfNamespacedParameterHolder.class.php
r7975 r8532 240 240 unset($this->parameters[$ns][$name]); 241 241 } 242 else 243 { 244 $retval = sfToolkit::removeArrayValueForPath($this->parameters[$ns], $name, $default); 245 } 242 246 243 247 return $retval; branches/1.1/lib/util/sfParameterHolder.class.php
r8528 r8532 121 121 unset($this->parameters[$name]); 122 122 } 123 else 124 { 125 $retval = sfToolkit::removeArrayValueForPath($this->parameters, $name, $default); 126 } 123 127 124 128 return $retval; branches/1.1/lib/util/sfToolkit.class.php
r7965 r8532 564 564 565 565 /** 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 /** 566 628 * Get path to php cli. 567 629 * branches/1.1/test/unit/util/sfNamespacedParameterHolderTest.php
r7576 r8532 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(5 2, new lime_output_color());13 $t = new lime_test(54, new lime_output_color()); 14 14 15 15 // ->clear() … … 221 221 $t->is($ph->has('foo[bar]'), true, '->has() can takes a multi-array key'); 222 222 $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 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(2 4, new lime_output_color());13 $t = new lime_test(26, new lime_output_color()); 14 14 15 15 // ->clear() … … 131 131 $t->is($ph->has('foo[bar]'), true, '->has() can takes a multi-array key'); 132 132 $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 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(9 3, new lime_output_color());13 $t = new lime_test(99, new lime_output_color()); 14 14 15 15 // ::stringToArray() … … 234 234 $t->is(sfToolkit::getArrayValueForPath($arr, 'bar[2]'), null, '::getArrayValueForPath() can take an array indexed by integer'); 235 235 $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');