Hidden.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ qk/6J+GPF1Pt28aLNyv9zsYTG46MYjjBSsTUc7l7TFs=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. /**
  13. * hidden form element generator class
  14. */
  15. namespace Cube\Form\Element;
  16. use Cube\Form\Element;
  17. class Hidden extends Element
  18. {
  19. /**
  20. * new line character
  21. */
  22. const NL = "\n";
  23. /**
  24. *
  25. * type of element - override the variable from the parent class
  26. *
  27. * @var string
  28. */
  29. protected $_element = 'hidden';
  30. /**
  31. *
  32. * whether to add keys to multiple elements or not
  33. *
  34. * @var bool
  35. */
  36. protected $_forceCountMultiple = false;
  37. /**
  38. *
  39. * class constructor
  40. *
  41. * @param string $name
  42. */
  43. public function __construct($name)
  44. {
  45. parent::__construct($this->_element, $name);
  46. }
  47. /**
  48. *
  49. * set force count multiple flag
  50. *
  51. * @param boolean $forceCountMultiple
  52. *
  53. * @return $this
  54. */
  55. public function setForceCountMultiple($forceCountMultiple)
  56. {
  57. $this->_forceCountMultiple = (bool)$forceCountMultiple;
  58. return $this;
  59. }
  60. /**
  61. *
  62. * get force count multiple flag
  63. *
  64. * @return boolean
  65. */
  66. public function getForceCountMultiple()
  67. {
  68. return $this->_forceCountMultiple;
  69. }
  70. /**
  71. *
  72. * renders the html form element
  73. * the method is run by all subclasses who don't have it overridden
  74. * if a post value is an array, we will render a multiple element
  75. * for serialized data, we change the brackets to ' rather than "
  76. *
  77. * @return string the html code of the element
  78. */
  79. public function render()
  80. {
  81. $render = array();
  82. $value = $this->getValue();
  83. if (is_array($value)) {
  84. $this->setMultiple(true);
  85. }
  86. else {
  87. $value = array($value);
  88. }
  89. foreach ($value as $key => $val) {
  90. $forceCountMultiple = $this->getForceCountMultiple();
  91. $multiple = ($this->getMultiple() === true) ?
  92. ((is_string($key) || $forceCountMultiple) ? '[' . $key . ']' : $this->_brackets) : '';
  93. if (is_array($val)) {
  94. $element = new Hidden($this->_name . $multiple);
  95. $element->setForceCountMultiple($forceCountMultiple)
  96. ->setValue($val);
  97. $render[] = $element->render();
  98. }
  99. else {
  100. $render[] = '<input type="' . $this->_type . '" '
  101. . 'name="' . $this->_name . $multiple . '" '
  102. . $this->renderAttributes()
  103. . "value='" . $val . "' "
  104. . $this->_endTag;
  105. }
  106. }
  107. return implode(self::NL, $render);
  108. }
  109. }