Skip to content

Commit c065be8

Browse files
committed
[OutputEscaper] refactored the component
1 parent e111652 commit c065be8

File tree

11 files changed

+208
-357
lines changed

11 files changed

+208
-357
lines changed

src/Symfony/Component/OutputEscaper/ArrayDecorator.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
1919
* @author Mike Squire <mike@somosis.co.uk>
2020
*/
21-
class ArrayDecorator extends GetterDecorator implements \Iterator, \ArrayAccess, \Countable
21+
class ArrayDecorator extends BaseEscaper implements \Iterator, \ArrayAccess, \Countable
2222
{
2323
/**
2424
* Used by the iterator to know if the current element is valid.
@@ -142,24 +142,23 @@ public function offsetUnset($offset)
142142
}
143143

144144
/**
145-
* Returns the size of the array (are required by the Countable interface).
145+
* Escapes a key from the array using the specified escaper.
146146
*
147-
* @return int The size of the array
147+
* @param string $key The array key
148+
* @param mixed $escaper The escaping method (a PHP callable or a named escaper)
148149
*/
149-
public function count()
150+
public function getEscapedKey($key, $escaper)
150151
{
151-
return count($this->value);
152+
return Escaper::escape($escaper, $this->value[$key]);
152153
}
153154

154155
/**
155-
* Returns the (unescaped) value from the array associated with the key supplied.
156-
*
157-
* @param string $key The key into the array to use
156+
* Returns the size of the array (are required by the Countable interface).
158157
*
159-
* @return mixed The value
158+
* @return int The size of the array
160159
*/
161-
public function getRaw($key)
160+
public function count()
162161
{
163-
return $this->value[$key];
162+
return count($this->value);
164163
}
165164
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Symfony\Component\OutputEscaper;
4+
5+
/*
6+
* This file is part of the Symfony package.
7+
*
8+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
/**
15+
* Abstract class that provides an interface for output escaping.
16+
*
17+
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
18+
* @author Mike Squire <mike@somosis.co.uk>
19+
*/
20+
abstract class BaseEscaper
21+
{
22+
/**
23+
* The value that is to be escaped.
24+
*/
25+
protected $value;
26+
27+
/**
28+
* The escaper (a PHP callable or a named escaper) that is going to be applied to the value and its children.
29+
*/
30+
protected $escaper;
31+
32+
/**
33+
* Constructor.
34+
*
35+
* Since BaseEscaper is an abstract class, instances cannot be created
36+
* directly but the constructor will be inherited by sub-classes.
37+
*
38+
* @param mixed $escaper The escaping method (a PHP callable or a named escaper)
39+
* @param string $value Escaping value
40+
*/
41+
public function __construct($escaper, $value)
42+
{
43+
$this->escaper = $escaper;
44+
$this->value = $value;
45+
}
46+
47+
/**
48+
* Sets the default escaper to use.
49+
*
50+
* @param mixed $escaper The escaping method (a PHP callable or a named escaper)
51+
*/
52+
public function setEscaper($escaper)
53+
{
54+
$this->escaper = $escaper;
55+
}
56+
57+
/**
58+
* Returns the raw value associated with this instance.
59+
*
60+
* Concrete instances of BaseEscaper classes decorate a value which is
61+
* stored by the constructor. This returns that original, unescaped, value.
62+
*
63+
* @return mixed The original value used to construct the decorator
64+
*/
65+
public function getRawValue()
66+
{
67+
return $this->value;
68+
}
69+
}

src/Symfony/Component/OutputEscaper/Escaper.php

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,17 @@
1212
*/
1313

1414
/**
15-
* Abstract class that provides an interface for escaping of output.
15+
* Escaper provides output escaping features.
1616
*
1717
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
1818
* @author Mike Squire <mike@somosis.co.uk>
1919
*/
20-
abstract class Escaper
20+
class Escaper
2121
{
22-
/**
23-
* The value that is to be escaped.
24-
*
25-
* @var mixed
26-
*/
27-
protected $value;
28-
29-
/**
30-
* The escaper (a PHP callable) that is going to be applied to the value and its
31-
* children.
32-
*
33-
* @var string
34-
*/
35-
protected $escaper;
36-
3722
static protected $charset = 'UTF-8';
3823
static protected $safeClasses = array();
3924
static protected $escapers;
4025

41-
/**
42-
* Constructor.
43-
*
44-
* Since Escaper is an abstract class, instances cannot be created
45-
* directly but the constructor will be inherited by sub-classes.
46-
*
47-
* @param string $callable A PHP callable
48-
* @param string $value Escaping value
49-
*/
50-
public function __construct($escaper, $value)
51-
{
52-
if (null === self::$escapers) {
53-
self::initializeEscapers();
54-
}
55-
56-
$this->escaper = is_string($escaper) && isset(self::$escapers[$escaper]) ? self::$escapers[$escaper] : $escaper;
57-
$this->value = $value;
58-
}
59-
6026
/**
6127
* Decorates a PHP variable with something that will escape any data obtained
6228
* from it.
@@ -78,7 +44,7 @@ public function __construct($escaper, $value)
7844
* The escaping method is actually a PHP callable. This class hosts a set
7945
* of standard escaping strategies.
8046
*
81-
* @param string $escaper The escaping method (a PHP callable) to apply to the value
47+
* @param mixed $escaper The escaping method (a PHP callable or a named escaper) to apply to the value
8248
* @param mixed $value The value to escape
8349
*
8450
* @return mixed Escaped value
@@ -109,19 +75,18 @@ static public function escape($escaper, $value)
10975
}
11076

11177
if (is_object($value)) {
112-
if ($value instanceof Escaper) {
78+
if ($value instanceof BaseEscaper) {
11379
// avoid double decoration
11480
$copy = clone $value;
115-
116-
$copy->escaper = $escaper;
81+
$copy->setEscaper($escaper);
11782

11883
return $copy;
11984
}
12085

12186
if ($value instanceof SafeDecorator) {
12287
// do not escape objects marked as safe
12388
// return the original object
124-
return $value->getValue();
89+
return $value->getRawValue();
12590
}
12691

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

172137
if (is_object($value)) {
173-
return $value instanceof Escaper ? $value->getRawValue() : $value;
138+
return $value instanceof BaseEscaper ? $value->getRawValue() : $value;
174139
}
175140

176141
return $value;
@@ -218,19 +183,6 @@ static public function markClassAsSafe($class)
218183
self::markClassesAsSafe(array($class));
219184
}
220185

221-
/**
222-
* Returns the raw value associated with this instance.
223-
*
224-
* Concrete instances of Escaper classes decorate a value which is
225-
* stored by the constructor. This returns that original, unescaped, value.
226-
*
227-
* @return mixed The original value used to construct the decorator
228-
*/
229-
public function getRawValue()
230-
{
231-
return $this->value;
232-
}
233-
234186
/**
235187
* Sets the current charset.
236188
*
@@ -262,6 +214,22 @@ static public function setEscaper($name, $escaper)
262214
self::$escapers[$name] = $escaper;
263215
}
264216

217+
/**
218+
* Gets a named escaper.
219+
*
220+
* @param string $name The escaper name
221+
*
222+
* @return mixed $escaper A PHP callable
223+
*/
224+
static public function getEscaper($escaper)
225+
{
226+
if (null === self::$escapers) {
227+
self::initializeEscapers();
228+
}
229+
230+
return is_string($escaper) && isset(self::$escapers[$escaper]) ? self::$escapers[$escaper] : $escaper;
231+
}
232+
265233
/**
266234
* Initializes the built-in escapers.
267235
*

src/Symfony/Component/OutputEscaper/GetterDecorator.php

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/Symfony/Component/OutputEscaper/IteratorDecorator.php

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2222
* @author Mike Squire <mike@somosis.co.uk>
2323
*/
24-
class IteratorDecorator extends ObjectDecorator implements \Iterator, \Countable, \ArrayAccess
24+
class IteratorDecorator extends ObjectDecorator implements \Iterator
2525
{
2626
/**
2727
* The iterator to be used.
@@ -94,71 +94,4 @@ public function valid()
9494
{
9595
return $this->iterator->valid();
9696
}
97-
98-
/**
99-
* Returns true if the supplied offset isset in the array (as required by the ArrayAccess interface).
100-
*
101-
* @param string $offset The offset of the value to check existence of
102-
*
103-
* @return bool true if the offset isset; false otherwise
104-
*/
105-
public function offsetExists($offset)
106-
{
107-
return isset($this->value[$offset]);
108-
}
109-
110-
/**
111-
* Returns the element associated with the offset supplied (as required by the ArrayAccess interface).
112-
*
113-
* @param string $offset The offset of the value to get
114-
*
115-
* @return mixed The escaped value
116-
*/
117-
public function offsetGet($offset)
118-
{
119-
return Escaper::escape($this->escaper, $this->value[$offset]);
120-
}
121-
122-
/**
123-
* Throws an exception saying that values cannot be set (this method is
124-
* required for the ArrayAccess interface).
125-
*
126-
* This (and the other Escaper classes) are designed to be read only
127-
* so this is an illegal operation.
128-
*
129-
* @param string $offset (ignored)
130-
* @param string $value (ignored)
131-
*
132-
* @throws \LogicException When trying to set values
133-
*/
134-
public function offsetSet($offset, $value)
135-
{
136-
throw new \LogicException('Cannot set values.');
137-
}
138-
139-
/**
140-
* Throws an exception saying that values cannot be unset (this method is
141-
* required for the ArrayAccess interface).
142-
*
143-
* This (and the other Escaper classes) are designed to be read only
144-
* so this is an illegal operation.
145-
*
146-
* @param string $offset (ignored)
147-
*
148-
* @throws \LogicException When trying to unset values
149-
*/
150-
public function offsetUnset($offset)
151-
{
152-
throw new \LogicException('Cannot unset values.');
153-
}
154-
155-
/**
156-
* Returns the size of the array (are required by the Countable interface).
157-
*
158-
* @return int The size of the array
159-
*/
160-
public function count()
161-
{
162-
return count($this->value);
163-
}
16497
}

0 commit comments

Comments
 (0)