/library/Zend/View/Helper/AbstractHtmlElement.php

https://github.com/zucchi/zf2 · PHP · 128 lines · 63 code · 12 blank · 53 comment · 17 complexity · c165ebe6bb3dd5b6bf3db36adecc2c70 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_View
  9. */
  10. namespace Zend\View\Helper;
  11. /**
  12. * @category Zend
  13. * @package Zend_View
  14. * @subpackage Helper
  15. */
  16. abstract class AbstractHtmlElement extends AbstractHelper
  17. {
  18. /**
  19. * EOL character
  20. */
  21. const EOL = PHP_EOL;
  22. /**
  23. * The tag closing bracket
  24. *
  25. * @var string
  26. */
  27. protected $closingBracket = null;
  28. /**
  29. * Get the tag closing bracket
  30. *
  31. * @return string
  32. */
  33. public function getClosingBracket()
  34. {
  35. if (!$this->closingBracket) {
  36. if ($this->isXhtml()) {
  37. $this->closingBracket = ' />';
  38. } else {
  39. $this->closingBracket = '>';
  40. }
  41. }
  42. return $this->closingBracket;
  43. }
  44. /**
  45. * Is doctype XHTML?
  46. *
  47. * @return boolean
  48. */
  49. protected function isXhtml()
  50. {
  51. $doctype = $this->view->plugin('doctype');
  52. return $doctype->isXhtml();
  53. }
  54. /**
  55. * Converts an associative array to a string of tag attributes.
  56. *
  57. * @access public
  58. *
  59. * @param array $attribs From this array, each key-value pair is
  60. * converted to an attribute name and value.
  61. *
  62. * @return string The XHTML for the attributes.
  63. */
  64. protected function htmlAttribs($attribs)
  65. {
  66. $xhtml = '';
  67. $escaper = $this->view->plugin('escapehtml');
  68. foreach ((array) $attribs as $key => $val) {
  69. $key = $escaper($key);
  70. if (('on' == substr($key, 0, 2)) || ('constraints' == $key)) {
  71. // Don't escape event attributes; _do_ substitute double quotes with singles
  72. if (!is_scalar($val)) {
  73. // non-scalar data should be cast to JSON first
  74. $val = \Zend\Json\Json::encode($val);
  75. }
  76. // Escape single quotes inside event attribute values.
  77. // This will create html, where the attribute value has
  78. // single quotes around it, and escaped single quotes or
  79. // non-escaped double quotes inside of it
  80. $val = str_replace('\'', '&#39;', $val);
  81. } else {
  82. if (is_array($val)) {
  83. $val = implode(' ', $val);
  84. }
  85. $val = $escaper($val);
  86. }
  87. if ('id' == $key) {
  88. $val = $this->normalizeId($val);
  89. }
  90. if (strpos($val, '"') !== false) {
  91. $xhtml .= " $key='$val'";
  92. } else {
  93. $xhtml .= " $key=\"$val\"";
  94. }
  95. }
  96. return $xhtml;
  97. }
  98. /**
  99. * Normalize an ID
  100. *
  101. * @param string $value
  102. * @return string
  103. */
  104. protected function normalizeId($value)
  105. {
  106. if (strstr($value, '[')) {
  107. if ('[]' == substr($value, -2)) {
  108. $value = substr($value, 0, strlen($value) - 2);
  109. }
  110. $value = trim($value, ']');
  111. $value = str_replace('][', '-', $value);
  112. $value = str_replace('[', '-', $value);
  113. }
  114. return $value;
  115. }
  116. }