| 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 |
*
|
|
|
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 29990 2010-06-25 17:06:20Z Kris.Wallsmith $
|
|
|
20 |
*/
|
|
|
21 |
class sfOutputEscaperObjectDecorator extends sfOutputEscaperGetterDecorator implements Countable
|
|
|
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, (string) $this->value);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Asks the wrapped object whether a property is set.
|
|
|
103 |
*
|
|
|
104 |
* @return boolean
|
|
|
105 |
*/
|
|
|
106 |
public function __isset($key)
|
|
|
107 |
{
|
|
|
108 |
return isset($this->value->$key);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Returns the size of the object if it implements Countable (is required by the Countable interface).
|
|
|
113 |
*
|
|
|
114 |
* It returns 1 if other cases (which is the default PHP behavior in such a case).
|
|
|
115 |
*
|
|
|
116 |
* @return int The size of the object
|
|
|
117 |
*/
|
|
|
118 |
public function count()
|
|
|
119 |
{
|
|
|
120 |
return count($this->value);
|
|
|
121 |
}
|
|
|
122 |
}
|