Ticket #2669: ParameterHolderArray.patch
| File ParameterHolderArray.patch, 10.7 kB (added by Carl.Vondrick, 1 year ago) |
|---|
-
test/unit/util/sfParameterHolderTest.php
old new 3 3 /* 4 4 * This file is part of the symfony package. 5 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 * 6 * 7 7 * For the full copyright and license information, please view the LICENSE 8 8 * file that was distributed with this source code. 9 9 */ 10 10 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(3 2, new lime_output_color());13 $t = new lime_test(38, new lime_output_color()); 14 14 15 15 // ->clear() 16 16 $t->diag('->clear()'); … … 149 149 // ->serialize() ->unserialize() 150 150 $t->diag('->serialize() ->unserialize()'); 151 151 $t->ok($ph == unserialize(serialize($ph)), 'sfParameterHolder implements the Serializable interface'); 152 153 // ->count() 154 $t->diag('->count()'); 155 $ph = new sfParameterHolder(); 156 $ph->add(array('a' => 'b', 'c' => 'd')); 157 $t->is(count($ph), 2, 'sfParameterHolder implements the Count interface'); 158 159 // ->current() ->key() ->next() ->rewind() ->valid() 160 $t->diag('->current() ->key() ->next() ->rewind() ->valid()'); 161 162 $ph = new sfParameterHolder(); 163 $parameters = array('foo' => 'bar', 'baz' => 'foobar', 'barfoo' => 'bazbar', 'bazfoo' => 'foobarbaz', false => false, 0 => 2); 164 $ph->add($parameters); 165 166 $testparameters = array(); 167 168 foreach ($ph as $key => $value) 169 { 170 $testparameters[$key] = $value; 171 } 172 173 $t->is_deeply($testparameters, $parameters, 'sfParameterHolder implements the Iterator interface'); 174 175 // '->offsetExists() ->offsetGet() ->offsetSet() ->offsetUnset() 176 $t->diag('->offsetExists() ->offsetGet() ->offsetSet() ->offsetUnset()'); 177 178 $t->ok(isset($ph['foo']), 'sfParameterHolder implements ArrayAccess interface for isset()'); 179 $t->is($ph['foo'], 'bar', 'sfParameterHolder implements ArrayAccess interface for get'); 180 $ph['foo'] = 'moo'; 181 $t->is($ph->get('foo'), 'moo', 'sfParameterHolder implements ArrayAccess interface for set'); 182 unset($ph['foo']); 183 $t->ok(!$ph->has('foo'), 'sfParameterHolder implements ArrayAccess interface for unset()'); -
test/unit/util/sfNamespacedParameterHolderTest.php
old new 3 3 /* 4 4 * This file is part of the symfony package. 5 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 * 6 * 7 7 * For the full copyright and license information, please view the LICENSE 8 8 * file that was distributed with this source code. 9 9 */ 10 10 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(6 0, new lime_output_color());13 $t = new lime_test(66, new lime_output_color()); 14 14 15 15 // ->clear() 16 16 $t->diag('->clear()'); … … 239 239 // ->serialize() ->unserialize() 240 240 $t->diag('->serialize() ->unserialize()'); 241 241 $t->ok($ph == unserialize(serialize($ph)), 'sfNamespacedParameterHolder implements the Serializable interface'); 242 243 // ->count() 244 $t->diag('->count()'); 245 $ph = new sfNamespacedParameterHolder(); 246 $ph->add(array('a' => 'b', 'c' => 'd')); 247 $t->is(count($ph), 2, 'sfNamespacedParameterHolder implements the Count interface'); 248 249 // ->current() ->key() ->next() ->rewind() ->valid() 250 $t->diag('->current() ->key() ->next() ->rewind() ->valid()'); 251 252 $ph = new sfNamespacedParameterHolder(); 253 $parameters = array('foo' => 'bar', 'baz' => 'foobar', 'barfoo' => 'bazbar', 'bazfoo' => 'foobarbaz', false => false, 0 => 2); 254 $ph->add($parameters); 255 256 $testparameters = array(); 257 258 foreach ($ph as $key => $value) 259 { 260 $testparameters[$key] = $value; 261 } 262 263 $t->is_deeply($testparameters, $parameters, 'sfNamespacedParameterHolder implements the Iterator interface'); 264 265 // '->offsetExists() ->offsetGet() ->offsetSet() ->offsetUnset() 266 $t->diag('->offsetExists() ->offsetGet() ->offsetSet() ->offsetUnset()'); 267 268 $t->ok(isset($ph['foo']), 'sfNamespacedParameterHolder implements ArrayAccess interface for isset()'); 269 $t->is($ph['foo'], 'bar', 'sfNamespacedParameterHolder implements ArrayAccess interface for get'); 270 $ph['foo'] = 'moo'; 271 $t->is($ph->get('foo'), 'moo', 'sfNamespacedParameterHolder implements ArrayAccess interface for set'); 272 unset($ph['foo']); 273 $t->ok(!$ph->has('foo'), 'sfNamespacedParameterHolder implements ArrayAccess interface for unset()'); -
lib/util/sfNamespacedParameterHolder.class.php
old new 4 4 * This file is part of the symfony package. 5 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 6 * (c) 2004-2006 Sean Kerr. 7 * 7 * 8 8 * For the full copyright and license information, please view the LICENSE 9 9 * file that was distributed with this source code. 10 10 */ … … 21 21 * @author Sean Kerr <skerr@mojavi.org> 22 22 * @version SVN: $Id$ 23 23 */ 24 class sfNamespacedParameterHolder implements Serializable 24 class sfNamespacedParameterHolder implements Serializable, Iterator, Countable, ArrayAccess 25 25 { 26 26 protected $default_namespace = null; 27 27 protected $parameters = array(); 28 28 29 29 /** 30 30 * The constructor for sfParameterHolder. 31 * 31 * 32 32 * The default namespace may be overridden at initialization as follows: 33 33 * <code> 34 34 * <?php … … 52 52 if ($move) 53 53 { 54 54 $values = $this->removeNamespace(); 55 $this->addByRef($values, $namespace); 55 56 // ->removeNamespace() can return NULL, so this will suppress the warning 57 if ($values) 58 { 59 $this->addByRef($values, $namespace); 60 } 56 61 } 57 62 58 63 $this->default_namespace = $namespace; … … 151 156 * Retrieve an array of parameters, within a namespace. 152 157 * 153 158 * This method is limited to a namespace. Without any argument, 154 * it returns the parameters of the default namespace. If a 159 * it returns the parameters of the default namespace. If a 155 160 * namespace is passed as an argument, only the parameters of the 156 161 * specified namespace are returned. 157 162 * … … 410 415 $this->default_namespace = $data[0]; 411 416 $this->parameters = $data[1]; 412 417 } 418 419 /** 420 * Implements the Iterator interface: Returns the current parameter 421 */ 422 public function current() 423 { 424 return current($this->parameters[$this->default_namespace]); 425 } 426 427 /** 428 * Implements the Iterator interface: Returns the current parameter name 429 */ 430 public function key() 431 { 432 return key($this->parameters[$this->default_namespace]); 433 } 434 435 /** 436 * Implements the Iterator interface: Moves to the next parameter 437 */ 438 public function next() 439 { 440 next($this->parameters[$this->default_namespace]); 441 } 442 443 /** 444 * Implements the Iterator interface: Moves to the first parameter 445 */ 446 public function rewind() 447 { 448 reset($this->parameters[$this->default_namespace]); 449 } 450 451 /** 452 * Implements the Iterator interface: Checks if parameter is valid. 453 */ 454 public function valid() 455 { 456 if ($this->hasNamespace($this->default_namespace)) 457 { 458 return false; 459 } 460 461 prev($this->parameters[$this->default_namespace]); 462 return each($this->parameters[$this->default_namespace]) !== false; 463 } 464 465 /** 466 * Counts the number of parameters 467 */ 468 public function count() 469 { 470 return count($this->parameters[$this->default_namespace]); 471 } 472 473 /** 474 * Wrapper for ArrayAccess interface for ->has() 475 * @see has() 476 */ 477 public function offsetExists($offset) 478 { 479 return $this->has($offset); 480 } 481 482 /** 483 * Wrapper for ArrayAccess interface for ->get() 484 * @see get() 485 */ 486 public function offsetGet($offset) 487 { 488 return $this->get($offset); 489 } 490 491 /** 492 * Wrapper for ArrayAccess interface for ->set() 493 * @see set() 494 */ 495 public function offsetSet($offset, $set) 496 { 497 $this->set($offset, $set); 498 } 499 500 /** 501 * Wrapper for ArrayAccess interface for ->remove() 502 * @see remove() 503 */ 504 public function offsetUnset($offset) 505 { 506 $this->remove($offset); 507 } 413 508 } -
lib/util/sfParameterHolder.class.php
old new 4 4 * This file is part of the symfony package. 5 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 6 * (c) 2004-2006 Sean Kerr. 7 * 7 * 8 8 * For the full copyright and license information, please view the LICENSE 9 9 * file that was distributed with this source code. 10 10 */ … … 21 21 * @author Sean Kerr <skerr@mojavi.org> 22 22 * @version SVN: $Id$ 23 23 */ 24 class sfParameterHolder implements Serializable 24 class sfParameterHolder implements Serializable, Iterator, Countable, ArrayAccess 25 25 { 26 26 protected $parameters = array(); 27 27 … … 225 225 { 226 226 $this->parameters = unserialize($serialized); 227 227 } 228 229 /** 230 * Implements the Iterator interface: Returns the current parameter 231 */ 232 public function current() 233 { 234 return current($this->parameters); 235 } 236 237 /** 238 * Implements the Iterator interface: Returns the current parameter name 239 */ 240 public function key() 241 { 242 return key($this->parameters); 243 } 244 245 /** 246 * Implements the Iterator interface: Moves to the next parameter 247 */ 248 public function next() 249 { 250 next($this->parameters); 251 } 252 253 /** 254 * Implements the Iterator interface: Moves to the first parameter 255 */ 256 public function rewind() 257 { 258 reset($this->parameters); 259 } 260 261 /** 262 * Implements the Iterator interface: Checks if parameter is valid. 263 */ 264 public function valid() 265 { 266 prev($this->parameters); 267 return each($this->parameters) !== false; 268 } 269 270 /** 271 * Counts the number of parameters 272 */ 273 public function count() 274 { 275 return count($this->parameters); 276 } 277 278 /** 279 * Wrapper for ArrayAccess interface for ->has() 280 * @see has() 281 */ 282 public function offsetExists($offset) 283 { 284 return $this->has($offset); 285 } 286 287 /** 288 * Wrapper for ArrayAccess interface for ->get() 289 * @see get() 290 */ 291 public function offsetGet($offset) 292 { 293 return $this->get($offset); 294 } 295 296 /** 297 * Wrapper for ArrayAccess interface for ->set() 298 * @see set() 299 */ 300 public function offsetSet($offset, $set) 301 { 302 $this->set($offset, $set); 303 } 304 305 /** 306 * Wrapper for ArrayAccess interface for ->remove() 307 * @see remove() 308 */ 309 public function offsetUnset($offset) 310 { 311 $this->remove($offset); 312 } 228 313 }