Development

#2669: ParameterHolderArray.patch

You must first sign up to be able to contribute.

Ticket #2669: ParameterHolderArray.patch

File ParameterHolderArray.patch, 10.7 kB (added by Carl.Vondrick, 1 year ago)

Patch for to implement Iterator, Countable, ArrayAccess? interfaces + unit tests.

  • test/unit/util/sfParameterHolderTest.php

    old new  
    33/* 
    44 * This file is part of the symfony package. 
    55 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 
    6  *  
     6 * 
    77 * For the full copyright and license information, please view the LICENSE 
    88 * file that was distributed with this source code. 
    99 */ 
    1010 
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(32, new lime_output_color()); 
     13$t = new lime_test(38, new lime_output_color()); 
    1414 
    1515// ->clear() 
    1616$t->diag('->clear()'); 
     
    149149// ->serialize() ->unserialize() 
    150150$t->diag('->serialize() ->unserialize()'); 
    151151$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 
     168foreach ($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'); 
     182unset($ph['foo']); 
     183$t->ok(!$ph->has('foo'), 'sfParameterHolder implements ArrayAccess interface for unset()'); 
  • test/unit/util/sfNamespacedParameterHolderTest.php

    old new  
    33/* 
    44 * This file is part of the symfony package. 
    55 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 
    6  *  
     6 * 
    77 * For the full copyright and license information, please view the LICENSE 
    88 * file that was distributed with this source code. 
    99 */ 
    1010 
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(60, new lime_output_color()); 
     13$t = new lime_test(66, new lime_output_color()); 
    1414 
    1515// ->clear() 
    1616$t->diag('->clear()'); 
     
    239239// ->serialize() ->unserialize() 
    240240$t->diag('->serialize() ->unserialize()'); 
    241241$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 
     258foreach ($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'); 
     272unset($ph['foo']); 
     273$t->ok(!$ph->has('foo'), 'sfNamespacedParameterHolder implements ArrayAccess interface for unset()'); 
  • lib/util/sfNamespacedParameterHolder.class.php

    old new  
    44 * This file is part of the symfony package. 
    55 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 
    66 * (c) 2004-2006 Sean Kerr. 
    7  *  
     7 * 
    88 * For the full copyright and license information, please view the LICENSE 
    99 * file that was distributed with this source code. 
    1010 */ 
     
    2121 * @author     Sean Kerr <skerr@mojavi.org> 
    2222 * @version    SVN: $Id$ 
    2323 */ 
    24 class sfNamespacedParameterHolder implements Serializable 
     24class sfNamespacedParameterHolder implements Serializable, Iterator, Countable, ArrayAccess 
    2525{ 
    2626  protected $default_namespace = null; 
    2727  protected $parameters = array(); 
    2828 
    2929  /** 
    3030   * The constructor for sfParameterHolder. 
    31    *  
     31   * 
    3232   * The default namespace may be overridden at initialization as follows: 
    3333   * <code> 
    3434   * <?php 
     
    5252    if ($move) 
    5353    { 
    5454      $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      } 
    5661    } 
    5762 
    5863    $this->default_namespace = $namespace; 
     
    151156   * Retrieve an array of parameters, within a namespace. 
    152157   * 
    153158   * 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 
    155160   * namespace is passed as an argument, only the parameters of the 
    156161   * specified namespace are returned. 
    157162   * 
     
    410415    $this->default_namespace = $data[0]; 
    411416    $this->parameters = $data[1]; 
    412417  } 
     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  } 
    413508} 
  • lib/util/sfParameterHolder.class.php

    old new  
    44 * This file is part of the symfony package. 
    55 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 
    66 * (c) 2004-2006 Sean Kerr. 
    7  *  
     7 * 
    88 * For the full copyright and license information, please view the LICENSE 
    99 * file that was distributed with this source code. 
    1010 */ 
     
    2121 * @author     Sean Kerr <skerr@mojavi.org> 
    2222 * @version    SVN: $Id$ 
    2323 */ 
    24 class sfParameterHolder implements Serializable 
     24class sfParameterHolder implements Serializable, Iterator, Countable, ArrayAccess 
    2525{ 
    2626  protected $parameters = array(); 
    2727 
     
    225225  { 
    226226    $this->parameters = unserialize($serialized); 
    227227  } 
     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  } 
    228313}