sfParameterHolder.class.php 4.8 KB

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