1. sfParameterHolder.class.php
  2. /** * sfParameterHolder provides a base class for managing parameters. * * Parameters, in this case, are used to extend classes with additional data * that requires no additional logic to manage. * * @package symfony * @subpackage util * @author Fabien Potencier * @author Sean Kerr * @version SVN: $Id: sfParameterHolder.class.php 23922 2009-11-14 14:58:38Z fabien $ */
  3. class sfParameterHolder implements Serializable
  4. {
  5. protected $parameters = array();
  6. /**
  7. * The constructor for sfParameterHolder.
  8. */
  9. public function __construct()
  10. {
  11. }
  12. /**
  13. * Clears all parameters associated with this request.
  14. */
  15. public function clear()
  16. {
  17. $this->parameters = array();
  18. }
  19. /**
  20. * Retrieves a parameter.
  21. *
  22. * @param string $name A parameter name
  23. * @param mixed $default A default parameter value
  24. *
  25. * @return mixed A parameter value, if the parameter exists, otherwise null
  26. */
  27. public function & get($name, $default = null)
  28. {
  29. if (array_key_exists($name, $this->parameters))
  30. {
  31. $value = & $this->parameters[$name];
  32. }
  33. else
  34. {
  35. $value = $default;
  36. }
  37. return $value;
  38. }
  39. /**
  40. * Retrieves an array of parameter names.
  41. *
  42. * @return array An indexed array of parameter names
  43. */
  44. public function getNames()
  45. {
  46. return array_keys($this->parameters);
  47. }
  48. /**
  49. * Retrieves an array of parameters.
  50. *
  51. * @return array An associative array of parameters
  52. */
  53. public function & getAll()
  54. {
  55. return $this->parameters;
  56. }
  57. /**
  58. * Indicates whether or not a parameter exists.
  59. *
  60. * @param string $name A parameter name
  61. *
  62. * @return bool true, if the parameter exists, otherwise false
  63. */
  64. public function has($name)
  65. {
  66. return array_key_exists($name, $this->parameters);
  67. }
  68. /**
  69. * Remove a parameter.
  70. *
  71. * @param string $name A parameter name
  72. * @param mixed $default A default parameter value
  73. *
  74. * @return string A parameter value, if the parameter was removed, otherwise null
  75. */
  76. public function remove($name, $default = null)
  77. {
  78. $retval = $default;
  79. if (array_key_exists($name, $this->parameters))
  80. {
  81. $retval = $this->parameters[$name];
  82. unset($this->parameters[$name]);
  83. }
  84. return $retval;
  85. }
  86. /**
  87. * Sets a parameter.
  88. *
  89. * If a parameter with the name already exists the value will be overridden.
  90. *
  91. * @param string $name A parameter name
  92. * @param mixed $value A parameter value
  93. */
  94. public function set($name, $value)
  95. {
  96. $this->parameters[$name] = $value;
  97. }
  98. /**
  99. * Sets a parameter by reference.
  100. *
  101. * If a parameter with the name already exists the value will be overridden.
  102. *
  103. * @param string $name A parameter name
  104. * @param mixed $value A reference to a parameter value
  105. */
  106. public function setByRef($name, & $value)
  107. {
  108. $this->parameters[$name] =& $value;
  109. }
  110. /**
  111. * Sets an array of parameters.
  112. *
  113. * If an existing parameter name matches any of the keys in the supplied
  114. * array, the associated value will be overridden.
  115. *
  116. * @param array $parameters An associative array of parameters and their associated values
  117. */
  118. public function add($parameters)
  119. {
  120. if (null === $parameters)
  121. {
  122. return;
  123. }
  124. foreach ($parameters as $key => $value)
  125. {
  126. $this->parameters[$key] = $value;
  127. }
  128. }
  129. /**
  130. * Sets an array of parameters by reference.
  131. *
  132. * If an existing parameter name matches any of the keys in the supplied
  133. * array, the associated value will be overridden.
  134. *
  135. * @param array $parameters An associative array of parameters and references to their associated values
  136. */
  137. public function addByRef(& $parameters)
  138. {
  139. foreach ($parameters as $key => &$value)
  140. {
  141. $this->parameters[$key] =& $value;
  142. }
  143. }
  144. /**
  145. * Serializes the current instance.
  146. *
  147. * @return array Objects instance
  148. */
  149. public function serialize()
  150. {
  151. return serialize($this->parameters);
  152. }
  153. /**
  154. * Unserializes a sfParameterHolder instance.
  155. *
  156. * @param string $serialized A serialized sfParameterHolder instance
  157. */
  158. public function unserialize($serialized)
  159. {
  160. $this->parameters = unserialize($serialized);
  161. }
  162. }

Debug toolbar