[ Index ] |
|
Code source de Symfony 1.0.0 |
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: sfOutputEscaperObjectDecorator.class.php 3232 2007-01-11 20:51:54Z fabien $ 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 The method on the object to be called 41 * @param array 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 The parameter to be passed to the get() get method 76 * 77 * @return mixed The unescaped value returned 78 * 79 * @throws <b>sfException</b> 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 * @throws <b>sfException</b> 97 */ 98 public function __toString() 99 { 100 if (method_exists($this->value, '__toString')) 101 { 102 return $this->value->__toString(); 103 } 104 else 105 { 106 throw new sfException(sprintf('Object of class "%s" cannot be converted to string (Please create a __toString() method)', get_class($this->value))); 107 } 108 } 109 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |