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: * Password - a password entry element
16: *
17: * @category Xoops\Form\Password
18: * @package Xoops\Form
19: * @author Kazumi Ono <onokazu@xoops.org>
20: * @author Taiwen Jiang <phppp@users.sourceforge.net>
21: * @copyright 2001-2015 XOOPS Project (http://xoops.org)
22: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
23: * @link http://xoops.org
24: */
25: class Password extends Element
26: {
27: /**
28: * __construct
29: *
30: * @param string|array $caption Caption or array of all attributes
31: * @param string $name name attribute
32: * @param integer $size Size of the field
33: * @param integer $maxlength Maximum length of the text
34: * @param string $value Initial value of the field - *Warning:* readable in cleartext in the page!
35: * @param string $autoComplete Turn autoComplete in browser 'on' or 'off'
36: * @param string $placeholder placeholder for this element.
37: */
38: public function __construct(
39: $caption,
40: $name = null,
41: $size = 32,
42: $maxlength = 64,
43: $value = '',
44: $autoComplete = 'off',
45: $placeholder = ''
46: ) {
47: if (is_array($caption)) {
48: parent::__construct($caption);
49: $this->setIfNotSet('size', 32);
50: $this->setIfNotSet('maxlength', 64);
51: $this->setIfNotSet('autocomplete', 'off');
52: } else {
53: parent::__construct([]);
54: $this->setCaption($caption);
55: $this->setWithDefaults('name', $name, 'name_error');
56: $this->set('size', (int)($size));
57: $this->set('maxlength', (int)($maxlength));
58: $this->setValue($value);
59: $this->setWithDefaults('autocomplete', $autoComplete, 'off', ['on', 'off']);
60: if (!empty($placeholder)) {
61: $this->set('placeholder', $placeholder);
62: }
63: }
64: $this->set('type', 'password');
65: }
66:
67: /**
68: * Get the field size
69: *
70: * @return int
71: */
72: public function getSize()
73: {
74: return (int) $this->get('size', 32);
75: }
76:
77: /**
78: * Get the max length
79: *
80: * @return int
81: */
82: public function getMaxlength()
83: {
84: return (int) $this->get('maxlength', 64);
85: }
86:
87: /**
88: * Prepare HTML for output
89: *
90: * @return string HTML
91: */
92: public function render()
93: {
94: $this->themeDecorateElement();
95:
96: $attributes = $this->renderAttributeString();
97: return '<input ' . $attributes . 'value="'
98: . $this->getValue() . '" ' . $this->getExtra() .' >';
99: }
100: }
101: