Skip to content

Commit

Permalink
[OutputEscaper] refactored the component
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 26, 2010
1 parent e111652 commit c065be8
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 357 deletions.
21 changes: 10 additions & 11 deletions src/Symfony/Component/OutputEscaper/ArrayDecorator.php
Expand Up @@ -18,7 +18,7 @@
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Mike Squire <mike@somosis.co.uk>
*/
class ArrayDecorator extends GetterDecorator implements \Iterator, \ArrayAccess, \Countable
class ArrayDecorator extends BaseEscaper implements \Iterator, \ArrayAccess, \Countable
{
/**
* Used by the iterator to know if the current element is valid.
Expand Down Expand Up @@ -142,24 +142,23 @@ public function offsetUnset($offset)
}

/**
* Returns the size of the array (are required by the Countable interface).
* Escapes a key from the array using the specified escaper.
*
* @return int The size of the array
* @param string $key The array key
* @param mixed $escaper The escaping method (a PHP callable or a named escaper)
*/
public function count()
public function getEscapedKey($key, $escaper)
{
return count($this->value);
return Escaper::escape($escaper, $this->value[$key]);
}

/**
* Returns the (unescaped) value from the array associated with the key supplied.
*
* @param string $key The key into the array to use
* Returns the size of the array (are required by the Countable interface).
*
* @return mixed The value
* @return int The size of the array
*/
public function getRaw($key)
public function count()
{
return $this->value[$key];
return count($this->value);
}
}
69 changes: 69 additions & 0 deletions src/Symfony/Component/OutputEscaper/BaseEscaper.php
@@ -0,0 +1,69 @@
<?php

namespace Symfony\Component\OutputEscaper;

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Abstract class that provides an interface for output escaping.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Mike Squire <mike@somosis.co.uk>
*/
abstract class BaseEscaper
{
/**
* The value that is to be escaped.
*/
protected $value;

/**
* The escaper (a PHP callable or a named escaper) that is going to be applied to the value and its children.
*/
protected $escaper;

/**
* Constructor.
*
* Since BaseEscaper is an abstract class, instances cannot be created
* directly but the constructor will be inherited by sub-classes.
*
* @param mixed $escaper The escaping method (a PHP callable or a named escaper)
* @param string $value Escaping value
*/
public function __construct($escaper, $value)
{
$this->escaper = $escaper;
$this->value = $value;
}

/**
* Sets the default escaper to use.
*
* @param mixed $escaper The escaping method (a PHP callable or a named escaper)
*/
public function setEscaper($escaper)
{
$this->escaper = $escaper;
}

/**
* Returns the raw value associated with this instance.
*
* Concrete instances of BaseEscaper classes decorate a value which is
* stored by the constructor. This returns that original, unescaped, value.
*
* @return mixed The original value used to construct the decorator
*/
public function getRawValue()
{
return $this->value;
}
}
78 changes: 23 additions & 55 deletions src/Symfony/Component/OutputEscaper/Escaper.php
Expand Up @@ -12,51 +12,17 @@
*/

/**
* Abstract class that provides an interface for escaping of output.
* Escaper provides output escaping features.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Mike Squire <mike@somosis.co.uk>
*/
abstract class Escaper
class Escaper
{
/**
* The value that is to be escaped.
*
* @var mixed
*/
protected $value;

/**
* The escaper (a PHP callable) that is going to be applied to the value and its
* children.
*
* @var string
*/
protected $escaper;

static protected $charset = 'UTF-8';
static protected $safeClasses = array();
static protected $escapers;

/**
* Constructor.
*
* Since Escaper is an abstract class, instances cannot be created
* directly but the constructor will be inherited by sub-classes.
*
* @param string $callable A PHP callable
* @param string $value Escaping value
*/
public function __construct($escaper, $value)
{
if (null === self::$escapers) {
self::initializeEscapers();
}

$this->escaper = is_string($escaper) && isset(self::$escapers[$escaper]) ? self::$escapers[$escaper] : $escaper;
$this->value = $value;
}

/**
* Decorates a PHP variable with something that will escape any data obtained
* from it.
Expand All @@ -78,7 +44,7 @@ public function __construct($escaper, $value)
* The escaping method is actually a PHP callable. This class hosts a set
* of standard escaping strategies.
*
* @param string $escaper The escaping method (a PHP callable) to apply to the value
* @param mixed $escaper The escaping method (a PHP callable or a named escaper) to apply to the value
* @param mixed $value The value to escape
*
* @return mixed Escaped value
Expand Down Expand Up @@ -109,19 +75,18 @@ static public function escape($escaper, $value)
}

if (is_object($value)) {
if ($value instanceof Escaper) {
if ($value instanceof BaseEscaper) {
// avoid double decoration
$copy = clone $value;

$copy->escaper = $escaper;
$copy->setEscaper($escaper);

return $copy;
}

if ($value instanceof SafeDecorator) {
// do not escape objects marked as safe
// return the original object
return $value->getValue();
return $value->getRawValue();
}

if (self::isClassMarkedAsSafe(get_class($value)) || $value instanceof SafeDecoratorInterface) {
Expand Down Expand Up @@ -170,7 +135,7 @@ static public function unescape($value)
}

if (is_object($value)) {
return $value instanceof Escaper ? $value->getRawValue() : $value;
return $value instanceof BaseEscaper ? $value->getRawValue() : $value;
}

return $value;
Expand Down Expand Up @@ -218,19 +183,6 @@ static public function markClassAsSafe($class)
self::markClassesAsSafe(array($class));
}

/**
* Returns the raw value associated with this instance.
*
* Concrete instances of Escaper classes decorate a value which is
* stored by the constructor. This returns that original, unescaped, value.
*
* @return mixed The original value used to construct the decorator
*/
public function getRawValue()
{
return $this->value;
}

/**
* Sets the current charset.
*
Expand Down Expand Up @@ -262,6 +214,22 @@ static public function setEscaper($name, $escaper)
self::$escapers[$name] = $escaper;
}

/**
* Gets a named escaper.
*
* @param string $name The escaper name
*
* @return mixed $escaper A PHP callable
*/
static public function getEscaper($escaper)
{
if (null === self::$escapers) {
self::initializeEscapers();
}

return is_string($escaper) && isset(self::$escapers[$escaper]) ? self::$escapers[$escaper] : $escaper;
}

/**
* Initializes the built-in escapers.
*
Expand Down
54 changes: 0 additions & 54 deletions src/Symfony/Component/OutputEscaper/GetterDecorator.php

This file was deleted.

69 changes: 1 addition & 68 deletions src/Symfony/Component/OutputEscaper/IteratorDecorator.php
Expand Up @@ -21,7 +21,7 @@
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Mike Squire <mike@somosis.co.uk>
*/
class IteratorDecorator extends ObjectDecorator implements \Iterator, \Countable, \ArrayAccess
class IteratorDecorator extends ObjectDecorator implements \Iterator
{
/**
* The iterator to be used.
Expand Down Expand Up @@ -94,71 +94,4 @@ public function valid()
{
return $this->iterator->valid();
}

/**
* 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.');
}

/**
* Returns the size of the array (are required by the Countable interface).
*
* @return int The size of the array
*/
public function count()
{
return count($this->value);
}
}

0 comments on commit c065be8

Please sign in to comment.