Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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