PageRenderTime 48ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/macro/src/compiler/lmbMacroHtmlTagWidget.class.php

http://github.com/limb-php-framework/limb
PHP | 135 lines | 96 code | 20 blank | 19 comment | 10 complexity | fec03485c26a5f7775e9e0ccd201af80 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. /**
  10. * Base class for runtime components that output XML(html) tags
  11. * @package macro
  12. * @version $Id$
  13. */
  14. class lmbMacroHtmlTagWidget
  15. {
  16. protected $attributes = array();
  17. protected $runtime_id;
  18. protected $skip_render = array();
  19. function __construct($id)
  20. {
  21. $this->runtime_id = $id;
  22. $this->skip_render = array_merge($this->skip_render, array('runtime_id'));
  23. }
  24. function getRuntimeId()
  25. {
  26. return $this->runtime_id;
  27. }
  28. function setAttributes($attributes)
  29. {
  30. if(is_array($attributes))
  31. $this->attributes = $attributes;
  32. }
  33. function setAttribute($attrib, $value)
  34. {
  35. $attrib = $this->_getCanonicalAttributeName($attrib);
  36. $this->attributes[$attrib] = $value;
  37. }
  38. function getAttribute($attrib)
  39. {
  40. $attrib = $this->_getCanonicalAttributeName($attrib);
  41. if (isset($this->attributes[$attrib]))
  42. return $this->attributes[$attrib];
  43. }
  44. function getBoolAttribute($attrib, $default = FALSE)
  45. {
  46. if (!isset($this->attributes[strtolower($attrib)]))
  47. return $default;
  48. return self :: getBooleanValue($this->attributes[strtolower($attrib)]);
  49. }
  50. static function getBooleanValue($value)
  51. {
  52. if(is_bool($value))
  53. return $value;
  54. if(is_string($value))
  55. {
  56. switch(strtoupper($value))
  57. {
  58. case false:
  59. case 'FALSE':
  60. case 'N':
  61. case 'NO':
  62. case 'NONE':
  63. case 'NA':
  64. case '0':
  65. return false;
  66. default:
  67. return true;
  68. }
  69. }
  70. }
  71. function removeAttribute($attrib)
  72. {
  73. $attrib = $this->_getCanonicalAttributeName($attrib);
  74. unset($this->attributes[$attrib]);
  75. }
  76. function hasAttribute($attrib)
  77. {
  78. $attrib = $this->_getCanonicalAttributeName($attrib);
  79. return array_key_exists($attrib, $this->attributes);
  80. }
  81. /**
  82. * Writes the contents of the attributes to the screen, using
  83. * htmlspecialchars to convert entities in values. Called by
  84. * a compiled template
  85. */
  86. function renderAttributes()
  87. {
  88. foreach ($this->attributes as $name => $value)
  89. {
  90. if(in_array($name, $this->skip_render))
  91. continue;
  92. echo ' ';
  93. echo $name;
  94. if (!is_null($value))
  95. {
  96. echo '="';
  97. echo htmlspecialchars($value, ENT_QUOTES);
  98. echo '"';
  99. }
  100. }
  101. }
  102. protected function _getCanonicalAttributeName($attrib)
  103. {
  104. // quick check if they happen to use the same case.
  105. if (array_key_exists($attrib, $this->attributes))
  106. return $attrib;
  107. // slow check
  108. foreach(array_keys($this->attributes) as $key)
  109. {
  110. if (strcasecmp($attrib, $key) == 0)
  111. return $key;
  112. }
  113. return $attrib;
  114. }
  115. }