/
HtmlGenerator.php
125 lines (111 loc) · 3.07 KB
/
HtmlGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Symfony\Component\Form;
/**
* An implementation of HtmlGeneratorInterface
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class HtmlGenerator implements HtmlGeneratorInterface
{
/**
* Whether to produce XHTML compliant code
* @var boolean
*/
protected static $xhtml = true;
/**
* The charset used during generating
* @var string
*/
protected $charset;
/**
* Sets the charset used for rendering
*
* @param string $charset
*/
public function __construct($charset = 'UTF-8')
{
$this->charset = $charset;
}
/**
* Sets the XHTML generation flag.
*
* @param bool $boolean true if renderers must be generated as XHTML, false otherwise
*/
static public function setXhtml($boolean)
{
self::$xhtml = (boolean) $boolean;
}
/**
* Returns whether to generate XHTML tags or not.
*
* @return bool true if renderers must be generated as XHTML, false otherwise
*/
static public function isXhtml()
{
return self::$xhtml;
}
/**
* {@inheritDoc}
*/
public function tag($tag, $attributes = array())
{
if (empty($tag)) {
return '';
}
return sprintf('<%s%s%s', $tag, $this->attributes($attributes), self::$xhtml ? ' />' : (strtolower($tag) == 'input' ? '>' : sprintf('></%s>', $tag)));
}
/**
* {@inheritDoc}
*/
public function contentTag($tag, $content = null, $attributes = array())
{
if (empty($tag)) {
return '';
}
return sprintf('<%s%s>%s</%s>', $tag, $this->attributes($attributes), $content, $tag);
}
/**
* {@inheritDoc}
*/
public function attribute($name, $value)
{
if (true === $value) {
return self::$xhtml ? sprintf('%s="%s"', $name, $this->escape($name)) : $this->escape($name);
} else {
return sprintf('%s="%s"', $name, $this->escape($value));
}
}
/**
* {@inheritDoc}
*/
public function attributes(array $attributes)
{
return implode('', array_map(array($this, 'attributesCallback'), array_keys($attributes), array_values($attributes)));
}
/**
* Prepares an attribute key and value for HTML representation.
*
* It removes empty attributes, except for the value one.
*
* @param string $name The attribute name
* @param string $value The attribute value
*
* @return string The HTML representation of the HTML key attribute pair.
*/
private function attributesCallback($name, $value)
{
if (false === $value || null === $value || ('' === $value && 'value' != $name)) {
return '';
} else {
return ' '.$this->attribute($name, $value);
}
}
/**
* {@inheritDoc}
*/
public function escape($value)
{
return htmlspecialchars((string) $value, ENT_QUOTES, $this->charset, false);
}
}