Text.php
Go to the documentation of this file.
1<?php
11
13
38class Text extends Abstracts\Input
39{
43 protected $placeholder;
44
48 protected $normalize;
49
53 protected $maxlength = false;
54
58 protected $list = false;
59
68 public function __construct($name, $parameters, $form)
69 {
70 parent::__construct($name, $parameters, $form);
71
72 $this->list = (isset($parameters['list']) && is_array($parameters['list'])) ? $parameters['list'] : false;
73 }
74
84 protected function setDefaults()
85 {
86 parent::setDefaults();
87
88 // textClass elements have values of type string
89 $this->defaults['defaultValue'] = '';
90 $this->defaults['placeholder'] = false;
91 $this->defaults['maxlength'] = false;
92 $this->defaults['normalize'] = true;
93 }
94
100 public function __toString()
101 {
102 $value = $this->htmlValue();
103 $type = strtolower($this->type);
104 $inputAttributes = $this->htmlInputAttributes();
105 $marker = $this->htmlMarker();
106 $label = $this->htmlLabel();
107 $list = $this->htmlList();
108 $wrapperAttributes = $this->htmlWrapperAttributes();
110 $helpMessage = $this->htmlHelpMessage();
111
112 return "<p {$wrapperAttributes}>" .
113 "<label>" .
114 "<span class=\"depage-label\">{$label}{$marker}</span>" .
115 "<input name=\"{$this->name}\" type=\"{$type}\"{$inputAttributes} value=\"{$value}\">" .
116 $list .
117 "</label>" .
120 "</p>\n";
121 }
122
129 protected function htmlList($options = null)
130 {
131 if ($this->list && is_array($this->list)) {
132 $formName = $this->htmlFormName();
133 $options = $this->htmlEscape($this->list);
134
135 $htmlList = "<datalist id=\"{$formName}-{$this->name}-list\">";
136
137 foreach ($options as $index => $option) {
138 // associative arrays have index as value
139 if (is_int($index)) {
140 $htmlList .= "<option value=\"{$option}\">";
141 } else {
142 $htmlList .= "<option value=\"{$index}\" label=\"{$option}\">";
143 }
144 }
145
146 $htmlList .= "</datalist>";
147 } else {
148 $htmlList = "";
149 }
150
151 return $htmlList;
152 }
153
159 protected function htmlInputAttributes()
160 {
161 $attributes = parent::htmlInputAttributes();
162
163 if ($this->maxlength) $attributes .= " maxlength=\"{$this->maxlength}\"";
164 if ($this->placeholder) $attributes .= " placeholder=\"{$this->placeholder}\"";
165 if ($this->list) $attributes .= " list=\"{$this->formName}-{$this->name}-list\"";
166
167 $attributes .= $this->validator->getPatternAttribute();
168
169 return $attributes;
170 }
171
177 protected function typeCastValue()
178 {
179 // strip control characters
180 $this->value = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', (string) $this->value);
181
182 if ($this->normalize && class_exists("\\Normalizer")) {
183 $this->value = \Normalizer::normalize($this->value);
184 }
185 }
186}
187
188/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition Element.php:267
input element base class
Definition Input.php:21
$value
Input elements's value.
Definition Input.php:123
$formName
Name of the parent HTML form.
Definition Input.php:118
$label
Input element - HTML label.
Definition Input.php:36
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:217
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:676
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:627
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:565
$type
Input element type - HTML input type attribute.
Definition Input.php:25
$helpMessage
Extra help message.
Definition Input.php:229
htmlValue()
Returns HTML-rendered element value.
Definition Input.php:647
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:91
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:657
HTML text input type.
Definition Text.php:39
htmlInputAttributes()
renders text element specific HTML attributes
Definition Text.php:159
$normalize
wether to normalize unicode strings or not
Definition Text.php:48
__construct($name, $parameters, $form)
text class constructor
Definition Text.php:68
typeCastValue()
Converts value to element specific type.
Definition Text.php:177
__toString()
Renders element to HTML.
Definition Text.php:100
$placeholder
HTML placeholder attribute.
Definition Text.php:43
setDefaults()
collects initial values across subclasses
Definition Text.php:84
htmlList($options=null)
Renders HTML datalist.
Definition Text.php:129
$maxlength
HTML maxlength attribute.
Definition Text.php:53
Abstract element classes.
Definition Container.php:10
Classes for HTML input-elements.
Definition Address.php:9