forked from zendframework/zendframework
/
FormButton.php
181 lines (160 loc) · 5.19 KB
/
FormButton.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Form\View\Helper;
use Zend\Form\ElementInterface;
use Zend\Form\Exception;
use Zend\Form\LabelAwareInterface;
class FormButton extends FormInput
{
/**
* Attributes valid for the button tag
*
* @var array
*/
protected $validTagAttributes = array(
'name' => true,
'autofocus' => true,
'disabled' => true,
'form' => true,
'formaction' => true,
'formenctype' => true,
'formmethod' => true,
'formnovalidate' => true,
'formtarget' => true,
'type' => true,
'value' => true,
);
/**
* Valid values for the button type
*
* @var array
*/
protected $validTypes = array(
'button' => true,
'reset' => true,
'submit' => true,
);
/**
* Invoke helper as functor
*
* Proxies to {@link render()}.
*
* @param ElementInterface|null $element
* @param null|string $buttonContent
* @return string|FormButton
*/
public function __invoke(ElementInterface $element = null, $buttonContent = null)
{
if (!$element) {
return $this;
}
return $this->render($element, $buttonContent);
}
/**
* Render a form <button> element from the provided $element,
* using content from $buttonContent or the element's "label" attribute
*
* @param ElementInterface $element
* @param null|string $buttonContent
* @throws Exception\DomainException
* @return string
*/
public function render(ElementInterface $element, $buttonContent = null)
{
$openTag = $this->openTag($element);
if (null === $buttonContent) {
$buttonContent = $element->getLabel();
if (null === $buttonContent) {
throw new Exception\DomainException(sprintf(
'%s expects either button content as the second argument, ' .
'or that the element provided has a label value; neither found',
__METHOD__
));
}
if (null !== ($translator = $this->getTranslator())) {
$buttonContent = $translator->translate(
$buttonContent, $this->getTranslatorTextDomain()
);
}
}
if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$buttonContent = $escapeHtmlHelper($buttonContent);
}
return $openTag . $buttonContent . $this->closeTag();
}
/**
* Generate an opening button tag
*
* @param null|array|ElementInterface $attributesOrElement
* @throws Exception\InvalidArgumentException
* @throws Exception\DomainException
* @return string
*/
public function openTag($attributesOrElement = null)
{
if (null === $attributesOrElement) {
return '<button>';
}
if (is_array($attributesOrElement)) {
$attributes = $this->createAttributesString($attributesOrElement);
return sprintf('<button %s>', $attributes);
}
if (!$attributesOrElement instanceof ElementInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an array or Zend\Form\ElementInterface instance; received "%s"',
__METHOD__,
(is_object($attributesOrElement) ? get_class($attributesOrElement) : gettype($attributesOrElement))
));
}
$element = $attributesOrElement;
$name = $element->getName();
if (empty($name) && $name !== 0) {
throw new Exception\DomainException(sprintf(
'%s requires that the element has an assigned name; none discovered',
__METHOD__
));
}
$attributes = $element->getAttributes();
$attributes['name'] = $name;
$attributes['type'] = $this->getType($element);
$attributes['value'] = $element->getValue();
return sprintf(
'<button %s>',
$this->createAttributesString($attributes)
);
}
/**
* Return a closing button tag
*
* @return string
*/
public function closeTag()
{
return '</button>';
}
/**
* Determine button type to use
*
* @param ElementInterface $element
* @return string
*/
protected function getType(ElementInterface $element)
{
$type = $element->getAttribute('type');
if (empty($type)) {
return 'submit';
}
$type = strtolower($type);
if (!isset($this->validTypes[$type])) {
return 'submit';
}
return $type;
}
}