1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Form;
13:
14: /**
15: * Text - a simple text field
16: *
17: * @category Xoops\Form\Text
18: * @package Xoops\Form
19: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
20: * @copyright 2001-2015 XOOPS Project (http://xoops.org)
21: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22: * @link http://xoops.org
23: */
24: class Text extends Element
25: {
26: /**
27: * __construct
28: *
29: * @param string|array $caption Caption or array of all attributes
30: * @param string $name name attribute
31: * @param integer $size Size
32: * @param integer $maxlength Maximum length of text
33: * @param string $value Initial text
34: * @param string $placeholder placeholder for this element.
35: */
36: public function __construct($caption, $name = null, $size = 10, $maxlength = 64, $value = '', $placeholder = '')
37: {
38: if (is_array($caption)) {
39: parent::__construct($caption);
40: } else {
41: parent::__construct([]);
42: $this->setWithDefaults('caption', $caption, '');
43: $this->setWithDefaults('name', $name, 'name_error');
44: $this->setWithDefaults('size', $size, 10);
45: $this->setWithDefaults('maxlength', $maxlength, 64);
46: $this->set('value', $value);
47: $this->setIfNotEmpty('placeholder', $placeholder);
48: }
49: $this->setIfNotSet('type', 'text');
50: $this->setIfNotSet('value', '');
51: }
52:
53: /**
54: * Get size
55: *
56: * @return int
57: */
58: public function getSize()
59: {
60: return (int) $this->get('size');
61: }
62:
63: /**
64: * Get maximum text length
65: *
66: * @return int
67: */
68: public function getMaxlength()
69: {
70: return (int) $this->get('maxlength');
71: }
72:
73: /**
74: * Get placeholder for this element
75: *
76: * @return string
77: */
78: public function getPlaceholder()
79: {
80: return (string) $this->get('placeholder');
81: }
82:
83: /**
84: * Prepare HTML for output
85: *
86: * @return string HTML
87: */
88: public function render()
89: {
90: $this->themeDecorateElement();
91: $dataList = $this->isDatalist();
92: if (!empty($dataList)) {
93: $this->add('list', 'list_' . $this->getName());
94: }
95:
96: $attributes = $this->renderAttributeString();
97: return '<input ' . $attributes . ' ' . $this->getExtra() .' >';
98: }
99: }
100: