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