123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
-
- namespace Knp\Menu\Renderer;
-
- if (!defined('ENT_SUBSTITUTE')) {
- define('ENT_SUBSTITUTE', 8);
- }
-
- abstract class Renderer
- {
- protected $charset = 'UTF-8';
-
- /**
- * @param string $charset
- */
- public function __construct($charset = null)
- {
- if (null !== $charset) {
- $this->charset = (string) $charset;
- }
- }
-
- /**
- * Renders a HTML attribute
- *
- * @param string $name
- * @param string $value
- * @return string
- */
- protected function renderHtmlAttribute($name, $value)
- {
- if (true === $value) {
- return sprintf('%s="%s"', $name, $this->escape($name));
- }
-
- return sprintf('%s="%s"', $name, $this->escape($value));
- }
-
- /**
- * Renders HTML attributes
- *
- * @param array $attributes
- * @return string
- */
- protected function renderHtmlAttributes(array $attributes)
- {
- return implode('', array_map(array($this, 'htmlAttributesCallback'), array_keys($attributes), array_values($attributes)));
- }
-
- /**
- * Prepares an attribute key and value for HTML representation.
- *
- * It removes empty attributes.
- *
- * @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 htmlAttributesCallback($name, $value)
- {
- if (false === $value || null === $value) {
- return '';
- }
-
- return ' '.$this->renderHtmlAttribute($name, $value);
- }
-
- /**
- * Escapes an HTML value
- *
- * @param string $value
- * @return string
- */
- protected function escape($value)
- {
- return $this->fixDoubleEscape(htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset));
- }
-
- /**
- * Fixes double escaped strings.
- *
- * @param string $escaped string to fix
- * @return string A single escaped string
- */
- protected function fixDoubleEscape($escaped)
- {
- return preg_replace('/&([a-z]+|(#\d+)|(#x[\da-f]+));/i', '&$1;', $escaped);
- }
-
- /**
- * Get the HTML charset
- *
- * @return string
- */
- public function getCharset()
- {
- return $this->charset;
- }
-
- /**
- * Set the HTML charset
- *
- * @param string $charset
- */
- public function setCharset($charset)
- {
- $this->charset = (string) $charset;
- }
- }
|