Development

/branches/1.1/lib/request/sfRequest.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/request/sfRequest.class.php

Revision 9098, 10.5 kB (checked in by FabianLange, 6 years ago)

1.1: fixed @param phpdoc to fit specs in request (refs #2991)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
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  * sfRequest provides methods for manipulating client request information such
14  * as attributes, and parameters. It is also possible to manipulate the
15  * request method originally sent by the user.
16  *
17  * @package    symfony
18  * @subpackage request
19  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20  * @author     Sean Kerr <sean@code-box.org>
21  * @version    SVN: $Id$
22  */
23 abstract class sfRequest
24 {
25   /**
26    * Process validation and execution for only GET requests.
27    *
28    */
29   const GET = 2;
30
31   /**
32    * Skip validation and execution for any request method.
33    *
34    */
35   const NONE = 1;
36
37   /**
38    * Process validation and execution for only POST requests.
39    *
40    */
41   const POST = 4;
42
43   /**
44    * Process validation and execution for only PUT requests.
45    *
46    */
47   const PUT = 5;
48
49   /**
50    * Process validation and execution for only DELETE requests.
51    *
52    */
53   const DELETE = 6;
54
55   /**
56    * Process validation and execution for only HEAD requests.
57    *
58    */
59   const HEAD = 7;
60
61   protected
62     $errors          = array(),
63     $dispatcher      = null,
64     $method          = null,
65     $parameterHolder = null,
66     $config          = null,
67     $attributeHolder = null;
68
69   /**
70    * Class constructor.
71    *
72    * @see initialize()
73    */
74   public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array())
75   {
76     $this->initialize($dispatcher, $parameters, $attributes);
77   }
78
79   /**
80    * Initializes this sfRequest.
81    *
82    * @param  sfEventDispatcher $dispatcher  An sfEventDispatcher instance
83    * @param  array             $parameters  An associative array of initialization parameters
84    * @param  array             $attributes  An associative array of initialization attributes
85    *
86    * @return bool true, if initialization completes successfully, otherwise false
87    *
88    * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfRequest
89    */
90   public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array())
91   {
92     $this->dispatcher = $dispatcher;
93
94     // initialize parameter and attribute holders
95     $this->parameterHolder = new sfParameterHolder();
96     $this->attributeHolder = new sfParameterHolder();
97
98     $this->parameterHolder->add($parameters);
99     $this->attributeHolder->add($attributes);
100   }
101
102   /**
103    * Extracts parameter values from the request.
104    *
105    * @param  array $names  An indexed array of parameter names to extract
106    *
107    * @return array An associative array of parameters and their values. If
108    *               a specified parameter doesn't exist an empty string will
109    *               be returned for its value
110    */
111   public function extractParameters($names)
112   {
113     $array = array();
114
115     $parameters = $this->parameterHolder->getAll();
116     foreach ($parameters as $key => $value)
117     {
118       if (in_array($key, $names))
119       {
120         $array[$key] = $value;
121       }
122     }
123
124     return $array;
125   }
126
127   /**
128    * Retrieves an error message.
129    *
130    * @param  string $name  An error name
131    *
132    * @return string An error message, if the error exists, otherwise null
133    */
134   public function getError($name)
135   {
136     if (!sfConfig::get('sf_compat_10'))
137     {
138       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
139     }
140
141     return isset($this->errors[$name]) ? $this->errors[$name] : null;
142   }
143
144   /**
145    * Retrieves an array of error names.
146    *
147    * @return array An indexed array of error names
148    */
149   public function getErrorNames()
150   {
151     if (!sfConfig::get('sf_compat_10'))
152     {
153       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
154     }
155
156     return array_keys($this->errors);
157   }
158
159   /**
160    * Retrieves an array of errors.
161    *
162    * @return array An associative array of errors
163    */
164   public function getErrors()
165   {
166     if (!sfConfig::get('sf_compat_10'))
167     {
168       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
169     }
170
171     return $this->errors;
172   }
173
174   /**
175    * Retrieves this request's method.
176    *
177    * @return int One of the following constants:
178    *             - sfRequest::GET
179    *             - sfRequest::POST
180    */
181   public function getMethod()
182   {
183     return $this->method;
184   }
185
186   /**
187    * Indicates whether or not an error exists.
188    *
189    * @param  string $name  An error name
190    *
191    * @return bool true, if the error exists, otherwise false
192    */
193   public function hasError($name)
194   {
195     if (!sfConfig::get('sf_compat_10'))
196     {
197       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
198     }
199
200     return array_key_exists($name, $this->errors);
201   }
202
203   /**
204    * Indicates whether or not any errors exist.
205    *
206    * @return bool true, if any error exist, otherwise false
207    */
208   public function hasErrors()
209   {
210     if (!sfConfig::get('sf_compat_10'))
211     {
212       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
213     }
214
215     return count($this->errors) > 0;
216   }
217
218   /**
219    * Removes an error.
220    *
221    * @param  string $name  An error name
222    *
223    * @return string An error message, if the error was removed, otherwise null
224    */
225   public function removeError($name)
226   {
227     if (!sfConfig::get('sf_compat_10'))
228     {
229       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
230     }
231
232     $retval = null;
233
234     if (isset($this->errors[$name]))
235     {
236       $retval = $this->errors[$name];
237
238       unset($this->errors[$name]);
239     }
240
241     return $retval;
242   }
243
244   /**
245    * Sets an error.
246    *
247    * @param string $name     An error name
248    * @param string $message  An error message
249    *
250    */
251   public function setError($name, $message)
252   {
253     if (!sfConfig::get('sf_compat_10'))
254     {
255       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
256     }
257
258     if (sfConfig::get('sf_logging_enabled'))
259     {
260       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Error in form for parameter "%s" (with message "%s")', $name, $message))));
261     }
262
263     $this->errors[$name] = $message;
264   }
265
266   /**
267    * Sets an array of errors
268    *
269    * If an existing error name matches any of the keys in the supplied
270    * array, the associated message will be overridden.
271    *
272    * @param array $erros An associative array of errors and their associated messages
273    *
274    */
275   public function setErrors($errors)
276   {
277     if (!sfConfig::get('sf_compat_10'))
278     {
279       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
280     }
281
282     $this->errors = array_merge($this->errors, $errors);
283   }
284
285   /**
286    * Sets the request method.
287    *
288    * @param int $methodCode  One of the following constants:
289    *
290    * - sfRequest::GET
291    * - sfRequest::POST
292    * - sfRequest::PUT
293    * - sfRequest::DELETE
294    * - sfRequest::HEAD
295    *
296    * @throws <b>sfException</b> - If the specified request method is invalid
297    */
298   public function setMethod($methodCode)
299   {
300     $available_methods = array(self::GET, self::POST, self::PUT, self::DELETE, self::HEAD, self::NONE);
301     if (in_array($methodCode, $available_methods))
302     {
303       $this->method = $methodCode;
304
305       return;
306     }
307
308     // invalid method type
309     throw new sfException(sprintf('Invalid request method: %s.', $methodCode));
310   }
311
312   /**
313    * Retrieves the parameters for the current request.
314    *
315    * @return sfParameterHolder The parameter holder
316    */
317   public function getParameterHolder()
318   {
319     return $this->parameterHolder;
320   }
321
322   /**
323    * Retrieves the attributes holder.
324    *
325    * @return sfParameterHolder The attribute holder
326    */
327   public function getAttributeHolder()
328   {
329     return $this->attributeHolder;
330   }
331
332   /**
333    * Retrieves an attribute from the current request.
334    *
335    * @param  string $name     Attribute name
336    * @param  string $default  Default attribute value
337    *
338    * @return mixed An attribute value
339    */
340   public function getAttribute($name, $default = null)
341   {
342     return $this->attributeHolder->get($name, $default);
343   }
344
345   /**
346    * Indicates whether or not an attribute exist for the current request.
347    *
348    * @param  string $name  Attribute name
349    *
350    * @return bool true, if the attribute exists otherwise false
351    */
352   public function hasAttribute($name)
353   {
354     return $this->attributeHolder->has($name);
355   }
356
357   /**
358    * Sets an attribute for the request.
359    *
360    * @param string $name   Attribute name
361    * @param string $value  Value for the attribute
362    *
363    */
364   public function setAttribute($name, $value)
365   {
366     $this->attributeHolder->set($name, $value);
367   }
368
369   /**
370    * Retrieves a paramater for the current request.
371    *
372    * @param string $name     Parameter name
373    * @param string $default  Parameter default value
374    *
375    */
376   public function getParameter($name, $default = null)
377   {
378     return $this->parameterHolder->get($name, $default);
379   }
380
381   /**
382    * Indicates whether or not a parameter exist for the current request.
383    *
384    * @param  string $name  Parameter name
385    *
386    * @return bool true, if the paramater exists otherwise false
387    */
388   public function hasParameter($name)
389   {
390     return $this->parameterHolder->has($name);
391   }
392
393   /**
394    * Sets a parameter for the current request.
395    *
396    * @param string $name   Parameter name
397    * @param string $value  Parameter value
398    *
399    */
400   public function setParameter($name, $value)
401   {
402     $this->parameterHolder->set($name, $value);
403   }
404
405   /**
406    * Calls methods defined via sfEventDispatcher.
407    *
408    * @param  string $method     The method name
409    * @param  array  $arguments  The method arguments
410    *
411    * @return mixed The returned value of the called method
412    *
413    * @throws <b>sfException</b> if call fails
414    */
415   public function __call($method, $arguments)
416   {
417     $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'request.method_not_found', array('method' => $method, 'arguments' => $arguments)));
418     if (!$event->isProcessed())
419     {
420       throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
421     }
422
423     return $event->getReturnValue();
424   }
425 }
426
Note: See TracBrowser for help on using the browser.