PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-form/src/View/Helper/Form.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 119 lines | 66 code | 15 blank | 38 comment | 10 complexity | 03134d25346e937c2d4d440455bcdc58 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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Form\View\Helper;
  10. use Zend\Form\FieldsetInterface;
  11. use Zend\Form\FormInterface;
  12. use Zend\View\Helper\Doctype;
  13. /**
  14. * View helper for rendering Form objects
  15. */
  16. class Form extends AbstractHelper
  17. {
  18. /**
  19. * Attributes valid for this tag (form)
  20. *
  21. * @var array
  22. */
  23. protected $validTagAttributes = array(
  24. 'accept-charset' => true,
  25. 'action' => true,
  26. 'autocomplete' => true,
  27. 'enctype' => true,
  28. 'method' => true,
  29. 'name' => true,
  30. 'novalidate' => true,
  31. 'target' => true,
  32. );
  33. /**
  34. * Invoke as function
  35. *
  36. * @param null|FormInterface $form
  37. * @return Form|string
  38. */
  39. public function __invoke(FormInterface $form = null)
  40. {
  41. if (!$form) {
  42. return $this;
  43. }
  44. return $this->render($form);
  45. }
  46. /**
  47. * Render a form from the provided $form,
  48. *
  49. * @param FormInterface $form
  50. * @return string
  51. */
  52. public function render(FormInterface $form)
  53. {
  54. if (method_exists($form, 'prepare')) {
  55. $form->prepare();
  56. }
  57. $formContent = '';
  58. foreach ($form as $element) {
  59. if ($element instanceof FieldsetInterface) {
  60. $formContent.= $this->getView()->formCollection($element);
  61. } else {
  62. $formContent.= $this->getView()->formRow($element);
  63. }
  64. }
  65. return $this->openTag($form) . $formContent . $this->closeTag();
  66. }
  67. /**
  68. * Generate an opening form tag
  69. *
  70. * @param null|FormInterface $form
  71. * @return string
  72. */
  73. public function openTag(FormInterface $form = null)
  74. {
  75. $doctype = $this->getDoctype();
  76. $attributes = array();
  77. if (! (Doctype::HTML5 === $doctype || Doctype::XHTML5 === $doctype)) {
  78. $attributes = array(
  79. 'action' => '',
  80. 'method' => 'get',
  81. );
  82. }
  83. if ($form instanceof FormInterface) {
  84. $formAttributes = $form->getAttributes();
  85. if (!array_key_exists('id', $formAttributes) && array_key_exists('name', $formAttributes)) {
  86. $formAttributes['id'] = $formAttributes['name'];
  87. }
  88. $attributes = array_merge($attributes, $formAttributes);
  89. }
  90. if ($attributes) {
  91. return sprintf('<form %s>', $this->createAttributesString($attributes));
  92. }
  93. return '<form>';
  94. }
  95. /**
  96. * Generate a closing form tag
  97. *
  98. * @return string
  99. */
  100. public function closeTag()
  101. {
  102. return '</form>';
  103. }
  104. }