Development

/branches/1.1/lib/view/escaper/sfOutputEscaperObjectDecorator.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/view/escaper/sfOutputEscaperObjectDecorator.class.php

Revision 9047, 3.0 kB (checked in by FabianLange, 7 years ago)

1.1: fixed @param phpdoc to fit specs in view and escaper (refs #2991)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
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  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Output escaping object decorator that intercepts all method calls and escapes
13  * their return values.
14  *
15  * @see        sfOutputEscaper
16  * @package    symfony
17  * @subpackage view
18  * @author     Mike Squire <mike@somosis.co.uk>
19  * @version    SVN: $Id$
20  */
21 class sfOutputEscaperObjectDecorator extends sfOutputEscaperGetterDecorator
22 {
23   /**
24    * Magic PHP method that intercepts method calls, calls them on the objects
25    * that is being escaped and escapes the result.
26    *
27    * The calling of the method is changed slightly to accommodate passing a
28    * specific escaping strategy. An additional parameter is appended to the
29    * argument list which is the escaping strategy. The decorator will remove
30    * and use this parameter as the escaping strategy if it begins with 'esc_'
31    * (the prefix all escaping helper functions have).
32    *
33    * For example if an object, $o, implements methods a() and b($arg):
34    *
35    *   $o->a()                // Escapes the return value of a()
36    *   $o->a(ESC_RAW)         // Uses the escaping method ESC_RAW with a()
37    *   $o->b('a')             // Escapes the return value of b('a')
38    *   $o->b('a', ESC_RAW);   // Uses the escaping method ESC_RAW with b('a')
39    *
40    * @param  string $method  The method on the object to be called
41    * @param  array  $args    An array of arguments to be passed to the method
42    *
43    * @return mixed The escaped value returned by the method
44    */
45   public function __call($method, $args)
46   {
47     if (count($args) > 0)
48     {
49       $escapingMethod = $args[count($args) - 1];
50       if (is_string($escapingMethod) && substr($escapingMethod, 0, 4) === 'esc_')
51       {
52         array_pop($args);
53       }
54       else
55       {
56         $escapingMethod = $this->escapingMethod;
57       }
58     }
59     else
60     {
61       $escapingMethod = $this->escapingMethod;
62     }
63
64     $value = call_user_func_array(array($this->value, $method), $args);
65
66     return sfOutputEscaper::escape($escapingMethod, $value);
67   }
68
69   /**
70    * Returns the result of calling the get() method on the object, bypassing
71    * any escaping, if that method exists.
72    *
73    * If there is not a callable get() method this will throw an exception.
74    *
75    * @param  string $key  The parameter to be passed to the get() get method
76    *
77    * @return mixed The unescaped value returned
78    *
79    * @throws sfException if the object does not have a callable get() method
80    */
81   public function getRaw($key)
82   {
83     if (!is_callable(array($this->value, 'get')))
84     {
85       throw new sfException('Object does not have a callable get() method.');
86     }
87
88     return $this->value->get($key);
89   }
90
91   /**
92    * Try to call decorated object __toString() method if exists.
93    *
94    * @return string
95    */
96   public function __toString()
97   {
98     return $this->escape($this->escapingMethod, $this->value->__toString());
99   }
100 }
101
Note: See TracBrowser for help on using the browser.