|
@@ -19,7 +19,7 @@ namespace Symfony\Component\OutputEscaper;
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
* @author Mike Squire <mike@somosis.co.uk>
|
|
|
*/
|
|
|
-class ObjectDecorator extends GetterDecorator
|
|
|
+class ObjectDecorator extends BaseEscaper implements \ArrayAccess, \Countable
|
|
|
{
|
|
|
/**
|
|
|
* Magic PHP method that intercepts method calls, calls them on the objects
|
|
@@ -62,27 +62,6 @@ class ObjectDecorator extends GetterDecorator
|
|
|
return Escaper::escape($escaper, $value);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Returns the result of calling the get() method on the object, bypassing
|
|
|
- * any escaping, if that method exists.
|
|
|
- *
|
|
|
- * If there is not a callable get() method this will throw an exception.
|
|
|
- *
|
|
|
- * @param string $key The parameter to be passed to the get() get method
|
|
|
- *
|
|
|
- * @return mixed The unescaped value returned
|
|
|
- *
|
|
|
- * @throws \LogicException if the object does not have a callable get() method
|
|
|
- */
|
|
|
- public function getRaw($key)
|
|
|
- {
|
|
|
- if (!is_callable(array($this->value, 'get'))) {
|
|
|
- throw new \LogicException('Object does not have a callable get() method.');
|
|
|
- }
|
|
|
-
|
|
|
- return $this->value->get($key);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Try to call decorated object __toString() method if exists.
|
|
|
*
|
|
@@ -90,7 +69,7 @@ class ObjectDecorator extends GetterDecorator
|
|
|
*/
|
|
|
public function __toString()
|
|
|
{
|
|
|
- return $this->escape($this->escaper, (string) $this->value);
|
|
|
+ return Escaper::escape($this->escaper, (string) $this->value);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -102,7 +81,7 @@ class ObjectDecorator extends GetterDecorator
|
|
|
*/
|
|
|
public function __get($key)
|
|
|
{
|
|
|
- return $this->escape($this->escaper, $this->value->$key);
|
|
|
+ return Escaper::escape($this->escaper, $this->value->$key);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -116,4 +95,97 @@ class ObjectDecorator extends GetterDecorator
|
|
|
{
|
|
|
return isset($this->value->$key);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Escapes an object property using the specified escaper.
|
|
|
+ *
|
|
|
+ * @param string $key The object property name
|
|
|
+ * @param mixed $escaper The escaping method (a PHP callable or a named escaper)
|
|
|
+ */
|
|
|
+ public function getEscapedProperty($key, $escaper)
|
|
|
+ {
|
|
|
+ return Escaper::escape($escaper, $this->value->$key);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns true if the supplied offset isset in the array (as required by the ArrayAccess interface).
|
|
|
+ *
|
|
|
+ * @param string $offset The offset of the value to check existence of
|
|
|
+ *
|
|
|
+ * @return bool true if the offset isset; false otherwise
|
|
|
+ */
|
|
|
+ public function offsetExists($offset)
|
|
|
+ {
|
|
|
+ return isset($this->value[$offset]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns the element associated with the offset supplied (as required by the ArrayAccess interface).
|
|
|
+ *
|
|
|
+ * @param string $offset The offset of the value to get
|
|
|
+ *
|
|
|
+ * @return mixed The escaped value
|
|
|
+ */
|
|
|
+ public function offsetGet($offset)
|
|
|
+ {
|
|
|
+ return Escaper::escape($this->escaper, $this->value[$offset]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Throws an exception saying that values cannot be set (this method is
|
|
|
+ * required for the ArrayAccess interface).
|
|
|
+ *
|
|
|
+ * This (and the other Escaper classes) are designed to be read only
|
|
|
+ * so this is an illegal operation.
|
|
|
+ *
|
|
|
+ * @param string $offset (ignored)
|
|
|
+ * @param string $value (ignored)
|
|
|
+ *
|
|
|
+ * @throws \LogicException When trying to set values
|
|
|
+ */
|
|
|
+ public function offsetSet($offset, $value)
|
|
|
+ {
|
|
|
+ throw new \LogicException('Cannot set values.');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Throws an exception saying that values cannot be unset (this method is
|
|
|
+ * required for the ArrayAccess interface).
|
|
|
+ *
|
|
|
+ * This (and the other Escaper classes) are designed to be read only
|
|
|
+ * so this is an illegal operation.
|
|
|
+ *
|
|
|
+ * @param string $offset (ignored)
|
|
|
+ *
|
|
|
+ * @throws \LogicException When trying to unset values
|
|
|
+ */
|
|
|
+ public function offsetUnset($offset)
|
|
|
+ {
|
|
|
+ throw new \LogicException('Cannot unset values.');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Escapes a key from the array using the specified escaper.
|
|
|
+ *
|
|
|
+ * @param string $key The array key
|
|
|
+ * @param mixed $escaper The escaping method (a PHP callable or a named escaper)
|
|
|
+ */
|
|
|
+ public function getEscapedKey($key, $escaper)
|
|
|
+ {
|
|
|
+ return Escaper::escape($escaper, $this->value[$key]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns the size of the array (are required by the Countable interface).
|
|
|
+ *
|
|
|
+ * @return int The size of the array
|
|
|
+ */
|
|
|
+ public function count()
|
|
|
+ {
|
|
|
+ if ($this->value instanceof \Countable) {
|
|
|
+ return count($this->value);
|
|
|
+ }
|
|
|
+
|
|
|
+ return call_user_func_array(array($this->value, 'count'), func_get_args());
|
|
|
+ }
|
|
|
}
|