1: <?php
2:
3: /**
4: * Factory for token generation.
5: *
6: * @note Doing some benchmarking indicates that the new operator is much
7: * slower than the clone operator (even discounting the cost of the
8: * constructor). This class is for that optimization.
9: * Other then that, there's not much point as we don't
10: * maintain parallel HTMLPurifier_Token hierarchies (the main reason why
11: * you'd want to use an abstract factory).
12: * @todo Port DirectLex to use this
13: */
14: class HTMLPurifier_TokenFactory
15: {
16: // p stands for prototype
17:
18: /**
19: * @type HTMLPurifier_Token_Start
20: */
21: private $p_start;
22:
23: /**
24: * @type HTMLPurifier_Token_End
25: */
26: private $p_end;
27:
28: /**
29: * @type HTMLPurifier_Token_Empty
30: */
31: private $p_empty;
32:
33: /**
34: * @type HTMLPurifier_Token_Text
35: */
36: private $p_text;
37:
38: /**
39: * @type HTMLPurifier_Token_Comment
40: */
41: private $p_comment;
42:
43: /**
44: * Generates blank prototypes for cloning.
45: */
46: public function __construct()
47: {
48: $this->p_start = new HTMLPurifier_Token_Start('', array());
49: $this->p_end = new HTMLPurifier_Token_End('');
50: $this->p_empty = new HTMLPurifier_Token_Empty('', array());
51: $this->p_text = new HTMLPurifier_Token_Text('');
52: $this->p_comment = new HTMLPurifier_Token_Comment('');
53: }
54:
55: /**
56: * Creates a HTMLPurifier_Token_Start.
57: * @param string $name Tag name
58: * @param array $attr Associative array of attributes
59: * @return HTMLPurifier_Token_Start Generated HTMLPurifier_Token_Start
60: */
61: public function createStart($name, $attr = array())
62: {
63: $p = clone $this->p_start;
64: $p->__construct($name, $attr);
65: return $p;
66: }
67:
68: /**
69: * Creates a HTMLPurifier_Token_End.
70: * @param string $name Tag name
71: * @return HTMLPurifier_Token_End Generated HTMLPurifier_Token_End
72: */
73: public function createEnd($name)
74: {
75: $p = clone $this->p_end;
76: $p->__construct($name);
77: return $p;
78: }
79:
80: /**
81: * Creates a HTMLPurifier_Token_Empty.
82: * @param string $name Tag name
83: * @param array $attr Associative array of attributes
84: * @return HTMLPurifier_Token_Empty Generated HTMLPurifier_Token_Empty
85: */
86: public function createEmpty($name, $attr = array())
87: {
88: $p = clone $this->p_empty;
89: $p->__construct($name, $attr);
90: return $p;
91: }
92:
93: /**
94: * Creates a HTMLPurifier_Token_Text.
95: * @param string $data Data of text token
96: * @return HTMLPurifier_Token_Text Generated HTMLPurifier_Token_Text
97: */
98: public function createText($data)
99: {
100: $p = clone $this->p_text;
101: $p->__construct($data);
102: return $p;
103: }
104:
105: /**
106: * Creates a HTMLPurifier_Token_Comment.
107: * @param string $data Data of comment token
108: * @return HTMLPurifier_Token_Comment Generated HTMLPurifier_Token_Comment
109: */
110: public function createComment($data)
111: {
112: $p = clone $this->p_comment;
113: $p->__construct($data);
114: return $p;
115: }
116: }
117:
118: // vim: et sw=4 sts=4
119: